| 1 |
import os |
| 2 |
import re |
| 3 |
import tempfile |
| 4 |
import threading |
| 5 |
import unittest |
| 6 |
from contextlib import contextmanager |
| 7 |
from pathlib import Path |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
from django.core.exceptions import ImproperlyConfigured |
| 11 |
from django.db import ( |
| 12 |
DEFAULT_DB_ALIAS, |
| 13 |
NotSupportedError, |
| 14 |
connection, |
| 15 |
connections, |
| 16 |
transaction, |
| 17 |
) |
| 18 |
from django.db.models import Aggregate, Avg, StdDev, Sum, Variance |
| 19 |
from django.db.utils import ConnectionHandler |
| 20 |
from django.test import SimpleTestCase, TestCase, TransactionTestCase, override_settings |
| 21 |
from django.test.utils import CaptureQueriesContext, isolate_apps |
| 22 |
|
| 23 |
from ..models import Item, Object, Square |
| 24 |
|
| 25 |
|
| 26 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests") |
| 27 |
class Tests(TestCase): |
| 28 |
longMessage = True |
| 29 |
|
| 30 |
def test_aggregation(self): |
| 31 |
"""Raise NotSupportedError when aggregating on date/time fields.""" |
| 32 |
for aggregate in (Sum, Avg, Variance, StdDev): |
| 33 |
with self.assertRaises(NotSupportedError): |
| 34 |
Item.objects.aggregate(aggregate("time")) |
| 35 |
with self.assertRaises(NotSupportedError): |
| 36 |
Item.objects.aggregate(aggregate("date")) |
| 37 |
with self.assertRaises(NotSupportedError): |
| 38 |
Item.objects.aggregate(aggregate("last_modified")) |
| 39 |
with self.assertRaises(NotSupportedError): |
| 40 |
Item.objects.aggregate( |
| 41 |
**{ |
| 42 |
"complex": aggregate("last_modified") |
| 43 |
+ aggregate("last_modified") |
| 44 |
} |
| 45 |
) |
| 46 |
|
| 47 |
def test_distinct_aggregation(self): |
| 48 |
class DistinctAggregate(Aggregate): |
| 49 |
allow_distinct = True |
| 50 |
|
| 51 |
aggregate = DistinctAggregate("first", "second", distinct=True) |
| 52 |
msg = ( |
| 53 |
"SQLite doesn't support DISTINCT on aggregate functions accepting " |
| 54 |
"multiple arguments." |
| 55 |
) |
| 56 |
with self.assertRaisesMessage(NotSupportedError, msg): |
| 57 |
connection.ops.check_expression_support(aggregate) |
| 58 |
|
| 59 |
def test_distinct_aggregation_multiple_args_no_distinct(self): |
| 60 |
# Aggregate functions accept multiple arguments when DISTINCT isn't |
| 61 |
# used, e.g. GROUP_CONCAT(). |
| 62 |
class DistinctAggregate(Aggregate): |
| 63 |
allow_distinct = True |
| 64 |
|
| 65 |
aggregate = DistinctAggregate("first", "second", distinct=False) |
| 66 |
connection.ops.check_expression_support(aggregate) |
| 67 |
|
| 68 |
def test_memory_db_test_name(self): |
| 69 |
"""A named in-memory db should be allowed where supported.""" |
| 70 |
from django.db.backends.sqlite3.base import DatabaseWrapper |
| 71 |
|
| 72 |
settings_dict = { |
| 73 |
"TEST": { |
| 74 |
"NAME": "file:memorydb_test?mode=memory&cache=shared", |
| 75 |
} |
| 76 |
} |
| 77 |
creation = DatabaseWrapper(settings_dict).creation |
| 78 |
self.assertEqual( |
| 79 |
creation._get_test_db_name(), |
| 80 |
creation.connection.settings_dict["TEST"]["NAME"], |
| 81 |
) |
| 82 |
|
| 83 |
def test_regexp_function(self): |
| 84 |
tests = ( |
| 85 |
("test", r"[0-9]+", False), |
| 86 |
("test", r"[a-z]+", True), |
| 87 |
("test", None, None), |
| 88 |
(None, r"[a-z]+", None), |
| 89 |
(None, None, None), |
| 90 |
) |
| 91 |
for string, pattern, expected in tests: |
| 92 |
with self.subTest((string, pattern)): |
| 93 |
with connection.cursor() as cursor: |
| 94 |
cursor.execute("SELECT %s REGEXP %s", [string, pattern]) |
| 95 |
value = cursor.fetchone()[0] |
| 96 |
value = bool(value) if value in {0, 1} else value |
| 97 |
self.assertIs(value, expected) |
| 98 |
|
| 99 |
def test_pathlib_name(self): |
| 100 |
with tempfile.TemporaryDirectory() as tmp: |
| 101 |
settings_dict = { |
| 102 |
"default": { |
| 103 |
"ENGINE": "django.db.backends.sqlite3", |
| 104 |
"NAME": Path(tmp) / "test.db", |
| 105 |
}, |
| 106 |
} |
| 107 |
connections = ConnectionHandler(settings_dict) |
| 108 |
connections["default"].ensure_connection() |
| 109 |
connections["default"].close() |
| 110 |
self.assertTrue(os.path.isfile(os.path.join(tmp, "test.db"))) |
| 111 |
|
| 112 |
@mock.patch.object(connection, "get_database_version", return_value=(3, 30)) |
| 113 |
def test_check_database_version_supported(self, mocked_get_database_version): |
| 114 |
msg = "SQLite 3.31 or later is required (found 3.30)." |
| 115 |
with self.assertRaisesMessage(NotSupportedError, msg): |
| 116 |
connection.check_database_version_supported() |
| 117 |
self.assertTrue(mocked_get_database_version.called) |
| 118 |
|
| 119 |
def test_init_command(self): |
| 120 |
settings_dict = { |
| 121 |
"default": { |
| 122 |
"ENGINE": "django.db.backends.sqlite3", |
| 123 |
"NAME": ":memory:", |
| 124 |
"OPTIONS": { |
| 125 |
"init_command": "PRAGMA synchronous=3; PRAGMA cache_size=2000;", |
| 126 |
}, |
| 127 |
} |
| 128 |
} |
| 129 |
connections = ConnectionHandler(settings_dict) |
| 130 |
connections["default"].ensure_connection() |
| 131 |
try: |
| 132 |
with connections["default"].cursor() as cursor: |
| 133 |
cursor.execute("PRAGMA synchronous") |
| 134 |
value = cursor.fetchone()[0] |
| 135 |
self.assertEqual(value, 3) |
| 136 |
cursor.execute("PRAGMA cache_size") |
| 137 |
value = cursor.fetchone()[0] |
| 138 |
self.assertEqual(value, 2000) |
| 139 |
finally: |
| 140 |
connections["default"]._close() |
| 141 |
|
| 142 |
|
| 143 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests") |
| 144 |
@isolate_apps("backends") |
| 145 |
class SchemaTests(TransactionTestCase): |
| 146 |
available_apps = ["backends"] |
| 147 |
|
| 148 |
def test_autoincrement(self): |
| 149 |
""" |
| 150 |
auto_increment fields are created with the AUTOINCREMENT keyword |
| 151 |
in order to be monotonically increasing (#10164). |
| 152 |
""" |
| 153 |
with connection.schema_editor(collect_sql=True) as editor: |
| 154 |
editor.create_model(Square) |
| 155 |
statements = editor.collected_sql |
| 156 |
match = re.search('"id" ([^,]+),', statements[0]) |
| 157 |
self.assertIsNotNone(match) |
| 158 |
self.assertEqual( |
| 159 |
"integer NOT NULL PRIMARY KEY AUTOINCREMENT", |
| 160 |
match[1], |
| 161 |
"Wrong SQL used to create an auto-increment column on SQLite", |
| 162 |
) |
| 163 |
|
| 164 |
def test_disable_constraint_checking_failure_disallowed(self): |
| 165 |
""" |
| 166 |
SQLite schema editor is not usable within an outer transaction if |
| 167 |
foreign key constraint checks are not disabled beforehand. |
| 168 |
""" |
| 169 |
msg = ( |
| 170 |
"SQLite schema editor cannot be used while foreign key " |
| 171 |
"constraint checks are enabled. Make sure to disable them " |
| 172 |
"before entering a transaction.atomic() context because " |
| 173 |
"SQLite does not support disabling them in the middle of " |
| 174 |
"a multi-statement transaction." |
| 175 |
) |
| 176 |
with self.assertRaisesMessage(NotSupportedError, msg): |
| 177 |
with transaction.atomic(), connection.schema_editor(atomic=True): |
| 178 |
pass |
| 179 |
|
| 180 |
def test_constraint_checks_disabled_atomic_allowed(self): |
| 181 |
""" |
| 182 |
SQLite schema editor is usable within an outer transaction as long as |
| 183 |
foreign key constraints checks are disabled beforehand. |
| 184 |
""" |
| 185 |
|
| 186 |
def constraint_checks_enabled(): |
| 187 |
with connection.cursor() as cursor: |
| 188 |
return bool(cursor.execute("PRAGMA foreign_keys").fetchone()[0]) |
| 189 |
|
| 190 |
with connection.constraint_checks_disabled(), transaction.atomic(): |
| 191 |
with connection.schema_editor(atomic=True): |
| 192 |
self.assertFalse(constraint_checks_enabled()) |
| 193 |
self.assertFalse(constraint_checks_enabled()) |
| 194 |
self.assertTrue(constraint_checks_enabled()) |
| 195 |
|
| 196 |
|
| 197 |
@unittest.skipUnless(connection.vendor == "sqlite", "Test only for SQLite") |
| 198 |
@override_settings(DEBUG=True) |
| 199 |
class LastExecutedQueryTest(TestCase): |
| 200 |
def test_no_interpolation(self): |
| 201 |
# This shouldn't raise an exception (#17158) |
| 202 |
query = "SELECT strftime('%Y', 'now');" |
| 203 |
with connection.cursor() as cursor: |
| 204 |
cursor.execute(query) |
| 205 |
self.assertEqual(connection.queries[-1]["sql"], query) |
| 206 |
|
| 207 |
def test_parameter_quoting(self): |
| 208 |
# The implementation of last_executed_queries isn't optimal. It's |
| 209 |
# worth testing that parameters are quoted (#14091). |
| 210 |
query = "SELECT %s" |
| 211 |
params = ["\"'\\"] |
| 212 |
with connection.cursor() as cursor: |
| 213 |
cursor.execute(query, params) |
| 214 |
# Note that the single quote is repeated |
| 215 |
substituted = "SELECT '\"''\\'" |
| 216 |
self.assertEqual(connection.queries[-1]["sql"], substituted) |
| 217 |
|
| 218 |
def test_large_number_of_parameters(self): |
| 219 |
# If SQLITE_MAX_VARIABLE_NUMBER (default = 999) has been changed to be |
| 220 |
# greater than SQLITE_MAX_COLUMN (default = 2000), last_executed_query |
| 221 |
# can hit the SQLITE_MAX_COLUMN limit (#26063). |
| 222 |
with connection.cursor() as cursor: |
| 223 |
sql = "SELECT MAX(%s)" % ", ".join(["%s"] * 2001) |
| 224 |
params = list(range(2001)) |
| 225 |
# This should not raise an exception. |
| 226 |
cursor.db.ops.last_executed_query(cursor.cursor, sql, params) |
| 227 |
|
| 228 |
|
| 229 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests") |
| 230 |
class EscapingChecks(TestCase): |
| 231 |
""" |
| 232 |
All tests in this test case are also run with settings.DEBUG=True in |
| 233 |
EscapingChecksDebug test case, to also test CursorDebugWrapper. |
| 234 |
""" |
| 235 |
|
| 236 |
def test_parameter_escaping(self): |
| 237 |
# '%s' escaping support for sqlite3 (#13648). |
| 238 |
with connection.cursor() as cursor: |
| 239 |
cursor.execute("select strftime('%s', date('now'))") |
| 240 |
response = cursor.fetchall()[0][0] |
| 241 |
# response should be an non-zero integer |
| 242 |
self.assertTrue(int(response)) |
| 243 |
|
| 244 |
|
| 245 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests") |
| 246 |
@override_settings(DEBUG=True) |
| 247 |
class EscapingChecksDebug(EscapingChecks): |
| 248 |
pass |
| 249 |
|
| 250 |
|
| 251 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests") |
| 252 |
class ThreadSharing(TransactionTestCase): |
| 253 |
available_apps = ["backends"] |
| 254 |
|
| 255 |
def test_database_sharing_in_threads(self): |
| 256 |
thread_connections = [] |
| 257 |
|
| 258 |
def create_object(): |
| 259 |
Object.objects.create() |
| 260 |
thread_connections.append(connections[DEFAULT_DB_ALIAS].connection) |
| 261 |
|
| 262 |
main_connection = connections[DEFAULT_DB_ALIAS].connection |
| 263 |
try: |
| 264 |
create_object() |
| 265 |
thread = threading.Thread(target=create_object) |
| 266 |
thread.start() |
| 267 |
thread.join() |
| 268 |
self.assertEqual(Object.objects.count(), 2) |
| 269 |
finally: |
| 270 |
for conn in thread_connections: |
| 271 |
if conn is not main_connection: |
| 272 |
conn.close() |
| 273 |
|
| 274 |
|
| 275 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests") |
| 276 |
class TestTransactionMode(SimpleTestCase): |
| 277 |
databases = {"default"} |
| 278 |
|
| 279 |
def test_default_transaction_mode(self): |
| 280 |
with CaptureQueriesContext(connection) as captured_queries: |
| 281 |
with transaction.atomic(): |
| 282 |
pass |
| 283 |
|
| 284 |
begin_query, commit_query = captured_queries |
| 285 |
self.assertEqual(begin_query["sql"], "BEGIN") |
| 286 |
self.assertEqual(commit_query["sql"], "COMMIT") |
| 287 |
|
| 288 |
def test_invalid_transaction_mode(self): |
| 289 |
msg = ( |
| 290 |
"settings.DATABASES['default']['OPTIONS']['transaction_mode'] is " |
| 291 |
"improperly configured to 'invalid'. Use one of 'DEFERRED', 'EXCLUSIVE', " |
| 292 |
"'IMMEDIATE', or None." |
| 293 |
) |
| 294 |
with self.change_transaction_mode("invalid") as new_connection: |
| 295 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 296 |
new_connection.ensure_connection() |
| 297 |
|
| 298 |
def test_valid_transaction_modes(self): |
| 299 |
valid_transaction_modes = ("deferred", "immediate", "exclusive") |
| 300 |
for transaction_mode in valid_transaction_modes: |
| 301 |
with ( |
| 302 |
self.subTest(transaction_mode=transaction_mode), |
| 303 |
self.change_transaction_mode(transaction_mode) as new_connection, |
| 304 |
CaptureQueriesContext(new_connection) as captured_queries, |
| 305 |
): |
| 306 |
new_connection.set_autocommit( |
| 307 |
False, force_begin_transaction_with_broken_autocommit=True |
| 308 |
) |
| 309 |
new_connection.commit() |
| 310 |
expected_transaction_mode = transaction_mode.upper() |
| 311 |
begin_sql = captured_queries[0]["sql"] |
| 312 |
self.assertEqual(begin_sql, f"BEGIN {expected_transaction_mode}") |
| 313 |
|
| 314 |
@contextmanager |
| 315 |
def change_transaction_mode(self, transaction_mode): |
| 316 |
new_connection = connection.copy() |
| 317 |
new_connection.settings_dict["OPTIONS"] = { |
| 318 |
**new_connection.settings_dict["OPTIONS"], |
| 319 |
"transaction_mode": transaction_mode, |
| 320 |
} |
| 321 |
try: |
| 322 |
yield new_connection |
| 323 |
finally: |
| 324 |
new_connection._close() |