Debian Codegraph @

tests/backends/tests.py

raw
1 """Tests related to django.db.backends that haven't been organized."""
2
3 import datetime
4 import threading
5 import unittest
6 import warnings
7 from unittest import mock
8
9 from django.core.management.color import no_style
10 from django.db import (
11 DEFAULT_DB_ALIAS,
12 DatabaseError,
13 IntegrityError,
14 connection,
15 connections,
16 reset_queries,
17 transaction,
18 )
19 from django.db.backends.base.base import BaseDatabaseWrapper
20 from django.db.backends.signals import connection_created
21 from django.db.backends.utils import CursorWrapper
22 from django.db.models.sql.constants import CURSOR
23 from django.test import (
24 TestCase,
25 TransactionTestCase,
26 override_settings,
27 skipIfDBFeature,
28 skipUnlessDBFeature,
29 )
30
31 from .models import (
32 Article,
33 Object,
34 ObjectReference,
35 Person,
36 Post,
37 RawData,
38 Reporter,
39 ReporterProxy,
40 SchoolClass,
41 SQLKeywordsModel,
42 Square,
43 VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ,
44 )
45
46
47 class DateQuotingTest(TestCase):
48 def test_django_date_trunc(self):
49 """
50 Test the custom ``django_date_trunc method``, in particular against
51 fields which clash with strings passed to it (e.g. 'year') (#12818).
52 """
53 updated = datetime.datetime(2010, 2, 20)
54 SchoolClass.objects.create(year=2009, last_updated=updated)
55 years = SchoolClass.objects.dates("last_updated", "year")
56 self.assertEqual(list(years), [datetime.date(2010, 1, 1)])
57
58 def test_django_date_extract(self):
59 """
60 Test the custom ``django_date_extract method``, in particular against fields
61 which clash with strings passed to it (e.g. 'day') (#12818).
62 """
63 updated = datetime.datetime(2010, 2, 20)
64 SchoolClass.objects.create(year=2009, last_updated=updated)
65 classes = SchoolClass.objects.filter(last_updated__day=20)
66 self.assertEqual(len(classes), 1)
67
68
69 @override_settings(DEBUG=True)
70 class LastExecutedQueryTest(TestCase):
71 def test_last_executed_query_without_previous_query(self):
72 """
73 last_executed_query should not raise an exception even if no previous
74 query has been run.
75 """
76 suffix = connection.features.bare_select_suffix
77 with connection.cursor() as cursor:
78 if connection.vendor == "oracle":
79 cursor.statement = None
80 # No previous query has been run.
81 connection.ops.last_executed_query(cursor, "", ())
82 # Previous query crashed.
83 connection.ops.last_executed_query(cursor, "SELECT %s" + suffix, (1,))
84
85 def test_debug_sql(self):
86 list(Reporter.objects.filter(first_name="test"))
87 sql = connection.queries[-1]["sql"].lower()
88 self.assertIn("select", sql)
89 self.assertIn(Reporter._meta.db_table, sql)
90
91 def test_query_encoding(self):
92 """last_executed_query() returns a string."""
93 data = RawData.objects.filter(raw_data=b"\x00\x46 \xfe").extra(
94 select={"föö": 1}
95 )
96 sql, params = data.query.sql_with_params()
97 with data.query.get_compiler("default").execute_sql(CURSOR) as cursor:
98 last_sql = cursor.db.ops.last_executed_query(cursor, sql, params)
99 self.assertIsInstance(last_sql, str)
100
101 def test_last_executed_query(self):
102 # last_executed_query() interpolate all parameters, in most cases it is
103 # not equal to QuerySet.query.
104 for qs in (
105 Article.objects.filter(pk=1),
106 Article.objects.filter(pk__in=(1, 2), reporter__pk=3),
107 Article.objects.filter(
108 pk=1,
109 reporter__pk=9,
110 ).exclude(reporter__pk__in=[2, 1]),
111 Article.objects.filter(pk__in=list(range(20, 31))),
112 ):
113 sql, params = qs.query.sql_with_params()
114 with qs.query.get_compiler(DEFAULT_DB_ALIAS).execute_sql(CURSOR) as cursor:
115 self.assertEqual(
116 cursor.db.ops.last_executed_query(cursor, sql, params),
117 str(qs.query),
118 )
119
120 @skipUnlessDBFeature("supports_paramstyle_pyformat")
121 def test_last_executed_query_dict(self):
122 square_opts = Square._meta
123 sql = "INSERT INTO %s (%s, %s) VALUES (%%(root)s, %%(square)s)" % (
124 connection.introspection.identifier_converter(square_opts.db_table),
125 connection.ops.quote_name(square_opts.get_field("root").column),
126 connection.ops.quote_name(square_opts.get_field("square").column),
127 )
128 with connection.cursor() as cursor:
129 params = {"root": 2, "square": 4}
130 cursor.execute(sql, params)
131 self.assertEqual(
132 cursor.db.ops.last_executed_query(cursor, sql, params),
133 sql % params,
134 )
135
136 @skipUnlessDBFeature("supports_paramstyle_pyformat")
137 def test_last_executed_query_dict_overlap_keys(self):
138 square_opts = Square._meta
139 sql = "INSERT INTO %s (%s, %s) VALUES (%%(root)s, %%(root2)s)" % (
140 connection.introspection.identifier_converter(square_opts.db_table),
141 connection.ops.quote_name(square_opts.get_field("root").column),
142 connection.ops.quote_name(square_opts.get_field("square").column),
143 )
144 with connection.cursor() as cursor:
145 params = {"root": 2, "root2": 4}
146 cursor.execute(sql, params)
147 self.assertEqual(
148 cursor.db.ops.last_executed_query(cursor, sql, params),
149 sql % params,
150 )
151
152 def test_last_executed_query_with_duplicate_params(self):
153 square_opts = Square._meta
154 table = connection.introspection.identifier_converter(square_opts.db_table)
155 id_column = connection.ops.quote_name(square_opts.get_field("id").column)
156 root_column = connection.ops.quote_name(square_opts.get_field("root").column)
157 sql = f"UPDATE {table} SET {root_column} = %s + %s WHERE {id_column} = %s"
158 with connection.cursor() as cursor:
159 params = [42, 42, 1]
160 cursor.execute(sql, params)
161 last_executed_query = connection.ops.last_executed_query(
162 cursor, sql, params
163 )
164 self.assertEqual(
165 last_executed_query,
166 f"UPDATE {table} SET {root_column} = 42 + 42 WHERE {id_column} = 1",
167 )
168
169
170 class ParameterHandlingTest(TestCase):
171 def test_bad_parameter_count(self):
172 """
173 An executemany call with too many/not enough parameters will raise an
174 exception.
175 """
176 with connection.cursor() as cursor:
177 query = "INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % (
178 connection.introspection.identifier_converter("backends_square"),
179 connection.ops.quote_name("root"),
180 connection.ops.quote_name("square"),
181 )
182 with self.assertRaises(Exception):
183 cursor.executemany(query, [(1, 2, 3)])
184 with self.assertRaises(Exception):
185 cursor.executemany(query, [(1,)])
186
187
188 class LongNameTest(TransactionTestCase):
189 """Long primary keys and model names can result in a sequence name
190 that exceeds the database limits, which will result in truncation
191 on certain databases (e.g., Postgres). The backend needs to use
192 the correct sequence name in last_insert_id and other places, so
193 check it is. Refs #8901.
194 """
195
196 available_apps = ["backends"]
197
198 def test_sequence_name_length_limits_create(self):
199 """Creation of model with long name and long pk name doesn't error."""
200 VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
201
202 def test_sequence_name_length_limits_m2m(self):
203 """
204 An m2m save of a model with a long name and a long m2m field name
205 doesn't error (#8901).
206 """
207 obj = (
208 VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
209 )
210 rel_obj = Person.objects.create(first_name="Django", last_name="Reinhardt")
211 obj.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.add(rel_obj)
212
213 def test_sequence_name_length_limits_flush(self):
214 """
215 Sequence resetting as part of a flush with model with long name and
216 long pk name doesn't error (#8901).
217 """
218 # A full flush is expensive to the full test, so we dig into the
219 # internals to generate the likely offending SQL and run it manually
220
221 # Some convenience aliases
222 VLM = VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
223 VLM_m2m = (
224 VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
225 )
226 tables = [
227 VLM._meta.db_table,
228 VLM_m2m._meta.db_table,
229 ]
230 sql_list = connection.ops.sql_flush(no_style(), tables, reset_sequences=True)
231 connection.ops.execute_sql_flush(sql_list)
232
233
234 @skipUnlessDBFeature("supports_sequence_reset")
235 class SequenceResetTest(TestCase):
236 def test_generic_relation(self):
237 "Sequence names are correct when resetting generic relations (Ref #13941)"
238 # Create an object with a manually specified PK
239 Post.objects.create(id=10, name="1st post", text="hello world")
240
241 # Reset the sequences for the database
242 commands = connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(
243 no_style(), [Post]
244 )
245 with connection.cursor() as cursor:
246 for sql in commands:
247 cursor.execute(sql)
248
249 # If we create a new object now, it should have a PK greater
250 # than the PK we specified manually.
251 obj = Post.objects.create(name="New post", text="goodbye world")
252 self.assertGreater(obj.pk, 10)
253
254
255 # This test needs to run outside of a transaction, otherwise closing the
256 # connection would implicitly rollback and cause problems during teardown.
257 class ConnectionCreatedSignalTest(TransactionTestCase):
258 available_apps = []
259
260 # Unfortunately with sqlite3 the in-memory test database cannot be closed,
261 # and so it cannot be re-opened during testing.
262 @skipUnlessDBFeature("test_db_allows_multiple_connections")
263 def test_signal(self):
264 data = {}
265
266 def receiver(sender, connection, **kwargs):
267 data["connection"] = connection
268
269 connection_created.connect(receiver)
270 connection.close()
271 with connection.cursor():
272 pass
273 self.assertIs(data["connection"].connection, connection.connection)
274
275 connection_created.disconnect(receiver)
276 data.clear()
277 with connection.cursor():
278 pass
279 self.assertEqual(data, {})
280
281
282 class EscapingChecks(TestCase):
283 """
284 All tests in this test case are also run with settings.DEBUG=True in
285 EscapingChecksDebug test case, to also test CursorDebugWrapper.
286 """
287
288 bare_select_suffix = connection.features.bare_select_suffix
289
290 def test_paramless_no_escaping(self):
291 with connection.cursor() as cursor:
292 cursor.execute("SELECT '%s'" + self.bare_select_suffix)
293 self.assertEqual(cursor.fetchall()[0][0], "%s")
294
295 def test_parameter_escaping(self):
296 with connection.cursor() as cursor:
297 cursor.execute("SELECT '%%', %s" + self.bare_select_suffix, ("%d",))
298 self.assertEqual(cursor.fetchall()[0], ("%", "%d"))
299
300
301 @override_settings(DEBUG=True)
302 class EscapingChecksDebug(EscapingChecks):
303 pass
304
305
306 class BackendTestCase(TransactionTestCase):
307 available_apps = ["backends"]
308
309 def create_squares_with_executemany(self, args):
310 self.create_squares(args, "format", True)
311
312 def create_squares(self, args, paramstyle, multiple):
313 opts = Square._meta
314 tbl = connection.introspection.identifier_converter(opts.db_table)
315 f1 = connection.ops.quote_name(opts.get_field("root").column)
316 f2 = connection.ops.quote_name(opts.get_field("square").column)
317 if paramstyle == "format":
318 query = "INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % (tbl, f1, f2)
319 elif paramstyle == "pyformat":
320 query = "INSERT INTO %s (%s, %s) VALUES (%%(root)s, %%(square)s)" % (
321 tbl,
322 f1,
323 f2,
324 )
325 else:
326 raise ValueError("unsupported paramstyle in test")
327 with connection.cursor() as cursor:
328 if multiple:
329 cursor.executemany(query, args)
330 else:
331 cursor.execute(query, args)
332
333 def test_cursor_executemany(self):
334 # Test cursor.executemany #4896
335 args = [(i, i**2) for i in range(-5, 6)]
336 self.create_squares_with_executemany(args)
337 self.assertEqual(Square.objects.count(), 11)
338 for i in range(-5, 6):
339 square = Square.objects.get(root=i)
340 self.assertEqual(square.square, i**2)
341
342 def test_cursor_executemany_with_empty_params_list(self):
343 # Test executemany with params=[] does nothing #4765
344 args = []
345 self.create_squares_with_executemany(args)
346 self.assertEqual(Square.objects.count(), 0)
347
348 def test_cursor_executemany_with_iterator(self):
349 # Test executemany accepts iterators #10320
350 args = ((i, i**2) for i in range(-3, 2))
351 self.create_squares_with_executemany(args)
352 self.assertEqual(Square.objects.count(), 5)
353
354 args = ((i, i**2) for i in range(3, 7))
355 with override_settings(DEBUG=True):
356 # same test for DebugCursorWrapper
357 self.create_squares_with_executemany(args)
358 self.assertEqual(Square.objects.count(), 9)
359
360 @skipUnlessDBFeature("supports_paramstyle_pyformat")
361 def test_cursor_execute_with_pyformat(self):
362 # Support pyformat style passing of parameters #10070
363 args = {"root": 3, "square": 9}
364 self.create_squares(args, "pyformat", multiple=False)
365 self.assertEqual(Square.objects.count(), 1)
366
367 @skipUnlessDBFeature("supports_paramstyle_pyformat")
368 def test_cursor_executemany_with_pyformat(self):
369 # Support pyformat style passing of parameters #10070
370 args = [{"root": i, "square": i**2} for i in range(-5, 6)]
371 self.create_squares(args, "pyformat", multiple=True)
372 self.assertEqual(Square.objects.count(), 11)
373 for i in range(-5, 6):
374 square = Square.objects.get(root=i)
375 self.assertEqual(square.square, i**2)
376
377 @skipUnlessDBFeature("supports_paramstyle_pyformat")
378 def test_cursor_executemany_with_pyformat_iterator(self):
379 args = ({"root": i, "square": i**2} for i in range(-3, 2))
380 self.create_squares(args, "pyformat", multiple=True)
381 self.assertEqual(Square.objects.count(), 5)
382
383 args = ({"root": i, "square": i**2} for i in range(3, 7))
384 with override_settings(DEBUG=True):
385 # same test for DebugCursorWrapper
386 self.create_squares(args, "pyformat", multiple=True)
387 self.assertEqual(Square.objects.count(), 9)
388
389 def test_unicode_fetches(self):
390 # fetchone, fetchmany, fetchall return strings as Unicode objects.
391 qn = connection.ops.quote_name
392 Person(first_name="John", last_name="Doe").save()
393 Person(first_name="Jane", last_name="Doe").save()
394 Person(first_name="Mary", last_name="Agnelline").save()
395 Person(first_name="Peter", last_name="Parker").save()
396 Person(first_name="Clark", last_name="Kent").save()
397 opts2 = Person._meta
398 f3, f4 = opts2.get_field("first_name"), opts2.get_field("last_name")
399 with connection.cursor() as cursor:
400 cursor.execute(
401 "SELECT %s, %s FROM %s ORDER BY %s"
402 % (
403 qn(f3.column),
404 qn(f4.column),
405 connection.introspection.identifier_converter(opts2.db_table),
406 qn(f3.column),
407 )
408 )
409 self.assertEqual(cursor.fetchone(), ("Clark", "Kent"))
410 self.assertEqual(
411 list(cursor.fetchmany(2)), [("Jane", "Doe"), ("John", "Doe")]
412 )
413 self.assertEqual(
414 list(cursor.fetchall()), [("Mary", "Agnelline"), ("Peter", "Parker")]
415 )
416
417 def test_unicode_password(self):
418 old_password = connection.settings_dict["PASSWORD"]
419 connection.settings_dict["PASSWORD"] = "françois"
420 try:
421 with connection.cursor():
422 pass
423 except DatabaseError:
424 # As password is probably wrong, a database exception is expected
425 pass
426 except Exception as e:
427 self.fail("Unexpected error raised with Unicode password: %s" % e)
428 finally:
429 connection.settings_dict["PASSWORD"] = old_password
430
431 def test_database_operations_helper_class(self):
432 # Ticket #13630
433 self.assertTrue(hasattr(connection, "ops"))
434 self.assertTrue(hasattr(connection.ops, "connection"))
435 self.assertEqual(connection, connection.ops.connection)
436
437 def test_database_operations_init(self):
438 """
439 DatabaseOperations initialization doesn't query the database.
440 See #17656.
441 """
442 with self.assertNumQueries(0):
443 connection.ops.__class__(connection)
444
445 def test_cached_db_features(self):
446 self.assertIn(connection.features.supports_transactions, (True, False))
447 self.assertIn(connection.features.can_introspect_foreign_keys, (True, False))
448
449 def test_duplicate_table_error(self):
450 """Creating an existing table returns a DatabaseError"""
451 query = "CREATE TABLE %s (id INTEGER);" % Article._meta.db_table
452 with connection.cursor() as cursor:
453 with self.assertRaises(DatabaseError):
454 cursor.execute(query)
455
456 def test_cursor_contextmanager(self):
457 """
458 Cursors can be used as a context manager
459 """
460 with connection.cursor() as cursor:
461 self.assertIsInstance(cursor, CursorWrapper)
462 # Both InterfaceError and ProgrammingError seem to be used when
463 # accessing closed cursor (psycopg has InterfaceError, rest seem
464 # to use ProgrammingError).
465 with self.assertRaises(connection.features.closed_cursor_error_class):
466 # cursor should be closed, so no queries should be possible.
467 cursor.execute("SELECT 1" + connection.features.bare_select_suffix)
468
469 @unittest.skipUnless(
470 connection.vendor == "postgresql",
471 "Psycopg specific cursor.closed attribute needed",
472 )
473 def test_cursor_contextmanager_closing(self):
474 # There isn't a generic way to test that cursors are closed, but
475 # psycopg offers us a way to check that by closed attribute.
476 # So, run only on psycopg for that reason.
477 with connection.cursor() as cursor:
478 self.assertIsInstance(cursor, CursorWrapper)
479 self.assertTrue(cursor.closed)
480
481 # Unfortunately with sqlite3 the in-memory test database cannot be closed.
482 @skipUnlessDBFeature("test_db_allows_multiple_connections")
483 def test_is_usable_after_database_disconnects(self):
484 """
485 is_usable() doesn't crash when the database disconnects (#21553).
486 """
487 # Open a connection to the database.
488 with connection.cursor():
489 pass
490 # Emulate a connection close by the database.
491 connection._close()
492 # Even then is_usable() should not raise an exception.
493 try:
494 self.assertFalse(connection.is_usable())
495 finally:
496 # Clean up the mess created by connection._close(). Since the
497 # connection is already closed, this crashes on some backends.
498 try:
499 connection.close()
500 except Exception:
501 pass
502
503 @override_settings(DEBUG=True)
504 def test_queries(self):
505 """
506 Test the documented API of connection.queries.
507 """
508 sql = "SELECT 1" + connection.features.bare_select_suffix
509 with connection.cursor() as cursor:
510 reset_queries()
511 cursor.execute(sql)
512 self.assertEqual(1, len(connection.queries))
513 self.assertIsInstance(connection.queries, list)
514 self.assertIsInstance(connection.queries[0], dict)
515 self.assertEqual(list(connection.queries[0]), ["sql", "time"])
516 self.assertEqual(connection.queries[0]["sql"], sql)
517
518 reset_queries()
519 self.assertEqual(0, len(connection.queries))
520
521 sql = "INSERT INTO %s (%s, %s) VALUES (%%s, %%s)" % (
522 connection.introspection.identifier_converter("backends_square"),
523 connection.ops.quote_name("root"),
524 connection.ops.quote_name("square"),
525 )
526 with connection.cursor() as cursor:
527 cursor.executemany(sql, [(1, 1), (2, 4)])
528 self.assertEqual(1, len(connection.queries))
529 self.assertIsInstance(connection.queries, list)
530 self.assertIsInstance(connection.queries[0], dict)
531 self.assertEqual(list(connection.queries[0]), ["sql", "time"])
532 self.assertEqual(connection.queries[0]["sql"], "2 times: %s" % sql)
533
534 # Unfortunately with sqlite3 the in-memory test database cannot be closed.
535 @skipUnlessDBFeature("test_db_allows_multiple_connections")
536 @override_settings(DEBUG=True)
537 def test_queries_limit(self):
538 """
539 The backend doesn't store an unlimited number of queries (#12581).
540 """
541 old_queries_limit = BaseDatabaseWrapper.queries_limit
542 BaseDatabaseWrapper.queries_limit = 3
543 new_connection = connection.copy()
544
545 # Initialize the connection and clear initialization statements.
546 with new_connection.cursor():
547 pass
548 new_connection.queries_log.clear()
549
550 try:
551 with new_connection.cursor() as cursor:
552 cursor.execute("SELECT 1" + new_connection.features.bare_select_suffix)
553 cursor.execute("SELECT 2" + new_connection.features.bare_select_suffix)
554
555 with warnings.catch_warnings(record=True) as w:
556 self.assertEqual(2, len(new_connection.queries))
557 self.assertEqual(0, len(w))
558
559 with new_connection.cursor() as cursor:
560 cursor.execute("SELECT 3" + new_connection.features.bare_select_suffix)
561 cursor.execute("SELECT 4" + new_connection.features.bare_select_suffix)
562
563 msg = (
564 "Limit for query logging exceeded, only the last 3 queries will be "
565 "returned."
566 )
567 with self.assertWarnsMessage(UserWarning, msg) as ctx:
568 self.assertEqual(3, len(new_connection.queries))
569 self.assertEqual(ctx.filename, __file__)
570
571 finally:
572 BaseDatabaseWrapper.queries_limit = old_queries_limit
573 new_connection.close()
574
575 @mock.patch("django.db.backends.utils.logger")
576 @override_settings(DEBUG=True)
577 def test_queries_logger(self, mocked_logger):
578 sql = "SELECT 1" + connection.features.bare_select_suffix
579 with connection.cursor() as cursor:
580 cursor.execute(sql)
581 params, kwargs = mocked_logger.debug.call_args
582 self.assertIn("; alias=%s", params[0])
583 self.assertEqual(params[2], sql)
584 self.assertIsNone(params[3])
585 self.assertEqual(params[4], connection.alias)
586 self.assertEqual(
587 list(kwargs["extra"]),
588 ["duration", "sql", "params", "alias"],
589 )
590 self.assertEqual(tuple(kwargs["extra"].values()), params[1:])
591
592 def test_queries_bare_where(self):
593 sql = f"SELECT 1{connection.features.bare_select_suffix} WHERE 1=1"
594 with connection.cursor() as cursor:
595 cursor.execute(sql)
596 self.assertEqual(cursor.fetchone(), (1,))
597
598 def test_timezone_none_use_tz_false(self):
599 connection.ensure_connection()
600 with self.settings(TIME_ZONE=None, USE_TZ=False):
601 connection.init_connection_state()
602
603
604 # These tests aren't conditional because it would require differentiating
605 # between MySQL+InnoDB and MySQL+MYISAM (something we currently can't do).
606 class FkConstraintsTests(TransactionTestCase):
607 available_apps = ["backends"]
608
609 def setUp(self):
610 # Create a Reporter.
611 self.r = Reporter.objects.create(first_name="John", last_name="Smith")
612
613 def test_integrity_checks_on_creation(self):
614 """
615 Try to create a model instance that violates a FK constraint. If it
616 fails it should fail with IntegrityError.
617 """
618 a1 = Article(
619 headline="This is a test",
620 pub_date=datetime.datetime(2005, 7, 27),
621 reporter_id=30,
622 )
623 try:
624 a1.save()
625 except IntegrityError:
626 pass
627 else:
628 self.skipTest("This backend does not support integrity checks.")
629 # Now that we know this backend supports integrity checks we make sure
630 # constraints are also enforced for proxy Refs #17519
631 a2 = Article(
632 headline="This is another test",
633 reporter=self.r,
634 pub_date=datetime.datetime(2012, 8, 3),
635 reporter_proxy_id=30,
636 )
637 with self.assertRaises(IntegrityError):
638 a2.save()
639
640 def test_integrity_checks_on_update(self):
641 """
642 Try to update a model instance introducing a FK constraint violation.
643 If it fails it should fail with IntegrityError.
644 """
645 # Create an Article.
646 Article.objects.create(
647 headline="Test article",
648 pub_date=datetime.datetime(2010, 9, 4),
649 reporter=self.r,
650 )
651 # Retrieve it from the DB
652 a1 = Article.objects.get(headline="Test article")
653 a1.reporter_id = 30
654 try:
655 a1.save()
656 except IntegrityError:
657 pass
658 else:
659 self.skipTest("This backend does not support integrity checks.")
660 # Now that we know this backend supports integrity checks we make sure
661 # constraints are also enforced for proxy Refs #17519
662 # Create another article
663 r_proxy = ReporterProxy.objects.get(pk=self.r.pk)
664 Article.objects.create(
665 headline="Another article",
666 pub_date=datetime.datetime(1988, 5, 15),
667 reporter=self.r,
668 reporter_proxy=r_proxy,
669 )
670 # Retrieve the second article from the DB
671 a2 = Article.objects.get(headline="Another article")
672 a2.reporter_proxy_id = 30
673 with self.assertRaises(IntegrityError):
674 a2.save()
675
676 def test_disable_constraint_checks_manually(self):
677 """
678 When constraint checks are disabled, should be able to write bad data
679 without IntegrityErrors.
680 """
681 with transaction.atomic():
682 # Create an Article.
683 Article.objects.create(
684 headline="Test article",
685 pub_date=datetime.datetime(2010, 9, 4),
686 reporter=self.r,
687 )
688 # Retrieve it from the DB
689 a = Article.objects.get(headline="Test article")
690 a.reporter_id = 30
691 try:
692 connection.disable_constraint_checking()
693 a.save()
694 connection.enable_constraint_checking()
695 except IntegrityError:
696 self.fail("IntegrityError should not have occurred.")
697 transaction.set_rollback(True)
698
699 def test_disable_constraint_checks_context_manager(self):
700 """
701 When constraint checks are disabled (using context manager), should be
702 able to write bad data without IntegrityErrors.
703 """
704 with transaction.atomic():
705 # Create an Article.
706 Article.objects.create(
707 headline="Test article",
708 pub_date=datetime.datetime(2010, 9, 4),
709 reporter=self.r,
710 )
711 # Retrieve it from the DB
712 a = Article.objects.get(headline="Test article")
713 a.reporter_id = 30
714 try:
715 with connection.constraint_checks_disabled():
716 a.save()
717 except IntegrityError:
718 self.fail("IntegrityError should not have occurred.")
719 transaction.set_rollback(True)
720
721 def test_check_constraints(self):
722 """
723 Constraint checks should raise an IntegrityError when bad data is in the DB.
724 """
725 with transaction.atomic():
726 # Create an Article.
727 Article.objects.create(
728 headline="Test article",
729 pub_date=datetime.datetime(2010, 9, 4),
730 reporter=self.r,
731 )
732 # Retrieve it from the DB
733 a = Article.objects.get(headline="Test article")
734 a.reporter_id = 30
735 with connection.constraint_checks_disabled():
736 a.save()
737 try:
738 connection.check_constraints(table_names=[Article._meta.db_table])
739 except IntegrityError:
740 pass
741 else:
742 self.skipTest("This backend does not support integrity checks.")
743 transaction.set_rollback(True)
744
745 def test_check_constraints_sql_keywords(self):
746 with transaction.atomic():
747 obj = SQLKeywordsModel.objects.create(reporter=self.r)
748 obj.refresh_from_db()
749 obj.reporter_id = 30
750 with connection.constraint_checks_disabled():
751 obj.save()
752 try:
753 connection.check_constraints(table_names=["order"])
754 except IntegrityError:
755 pass
756 else:
757 self.skipTest("This backend does not support integrity checks.")
758 transaction.set_rollback(True)
759
760
761 class ThreadTests(TransactionTestCase):
762 available_apps = ["backends"]
763
764 def test_default_connection_thread_local(self):
765 """
766 The default connection (i.e. django.db.connection) is different for
767 each thread (#17258).
768 """
769 # Map connections by id because connections with identical aliases
770 # have the same hash.
771 connections_dict = {}
772 with connection.cursor():
773 pass
774 connections_dict[id(connection)] = connection
775
776 def runner():
777 # Passing django.db.connection between threads doesn't work while
778 # connections[DEFAULT_DB_ALIAS] does.
779 from django.db import connections
780
781 connection = connections[DEFAULT_DB_ALIAS]
782 # Allow thread sharing so the connection can be closed by the
783 # main thread.
784 connection.inc_thread_sharing()
785 with connection.cursor():
786 pass
787 connections_dict[id(connection)] = connection
788
789 try:
790 for x in range(2):
791 t = threading.Thread(target=runner)
792 t.start()
793 t.join()
794 # Each created connection got different inner connection.
795 self.assertEqual(
796 len({conn.connection for conn in connections_dict.values()}), 3
797 )
798 finally:
799 # Finish by closing the connections opened by the other threads
800 # (the connection opened in the main thread will automatically be
801 # closed on teardown).
802 for conn in connections_dict.values():
803 if conn is not connection and conn.allow_thread_sharing:
804 conn.validate_thread_sharing()
805 conn._close()
806 conn.dec_thread_sharing()
807
808 def test_connections_thread_local(self):
809 """
810 The connections are different for each thread (#17258).
811 """
812 # Map connections by id because connections with identical aliases
813 # have the same hash.
814 connections_dict = {}
815 for conn in connections.all():
816 connections_dict[id(conn)] = conn
817
818 def runner():
819 from django.db import connections
820
821 for conn in connections.all():
822 # Allow thread sharing so the connection can be closed by the
823 # main thread.
824 conn.inc_thread_sharing()
825 connections_dict[id(conn)] = conn
826
827 try:
828 num_new_threads = 2
829 for x in range(num_new_threads):
830 t = threading.Thread(target=runner)
831 t.start()
832 t.join()
833 self.assertEqual(
834 len(connections_dict),
835 len(connections.all()) * (num_new_threads + 1),
836 )
837 finally:
838 # Finish by closing the connections opened by the other threads
839 # (the connection opened in the main thread will automatically be
840 # closed on teardown).
841 for conn in connections_dict.values():
842 if conn is not connection and conn.allow_thread_sharing:
843 conn.close()
844 conn.dec_thread_sharing()
845
846 def test_pass_connection_between_threads(self):
847 """
848 A connection can be passed from one thread to the other (#17258).
849 """
850 Person.objects.create(first_name="John", last_name="Doe")
851
852 def do_thread():
853 def runner(main_thread_connection):
854 from django.db import connections
855
856 connections["default"] = main_thread_connection
857 try:
858 Person.objects.get(first_name="John", last_name="Doe")
859 except Exception as e:
860 exceptions.append(e)
861
862 t = threading.Thread(target=runner, args=[connections["default"]])
863 t.start()
864 t.join()
865
866 # Without touching thread sharing, which should be False by default.
867 exceptions = []
868 do_thread()
869 # Forbidden!
870 self.assertIsInstance(exceptions[0], DatabaseError)
871 connections["default"].close()
872
873 # After calling inc_thread_sharing() on the connection.
874 connections["default"].inc_thread_sharing()
875 try:
876 exceptions = []
877 do_thread()
878 # All good
879 self.assertEqual(exceptions, [])
880 finally:
881 connections["default"].dec_thread_sharing()
882
883 def test_closing_non_shared_connections(self):
884 """
885 A connection that is not explicitly shareable cannot be closed by
886 another thread (#17258).
887 """
888 # First, without explicitly enabling the connection for sharing.
889 exceptions = set()
890
891 def runner1():
892 def runner2(other_thread_connection):
893 try:
894 other_thread_connection.close()
895 except DatabaseError as e:
896 exceptions.add(e)
897
898 t2 = threading.Thread(target=runner2, args=[connections["default"]])
899 t2.start()
900 t2.join()
901
902 t1 = threading.Thread(target=runner1)
903 t1.start()
904 t1.join()
905 # The exception was raised
906 self.assertEqual(len(exceptions), 1)
907
908 # Then, with explicitly enabling the connection for sharing.
909 exceptions = set()
910
911 def runner1():
912 def runner2(other_thread_connection):
913 try:
914 other_thread_connection.close()
915 except DatabaseError as e:
916 exceptions.add(e)
917
918 # Enable thread sharing
919 connections["default"].inc_thread_sharing()
920 try:
921 t2 = threading.Thread(target=runner2, args=[connections["default"]])
922 t2.start()
923 t2.join()
924 finally:
925 connections["default"].dec_thread_sharing()
926
927 t1 = threading.Thread(target=runner1)
928 t1.start()
929 t1.join()
930 # No exception was raised
931 self.assertEqual(len(exceptions), 0)
932
933 def test_thread_sharing_count(self):
934 self.assertIs(connection.allow_thread_sharing, False)
935 connection.inc_thread_sharing()
936 self.assertIs(connection.allow_thread_sharing, True)
937 connection.inc_thread_sharing()
938 self.assertIs(connection.allow_thread_sharing, True)
939 connection.dec_thread_sharing()
940 self.assertIs(connection.allow_thread_sharing, True)
941 connection.dec_thread_sharing()
942 self.assertIs(connection.allow_thread_sharing, False)
943 msg = "Cannot decrement the thread sharing count below zero."
944 with self.assertRaisesMessage(RuntimeError, msg):
945 connection.dec_thread_sharing()
946
947
948 class MySQLPKZeroTests(TestCase):
949 """
950 Zero as id for AutoField should raise exception in MySQL, because MySQL
951 does not allow zero for autoincrement primary key if the
952 NO_AUTO_VALUE_ON_ZERO SQL mode is not enabled.
953 """
954
955 @skipIfDBFeature("allows_auto_pk_0")
956 def test_zero_as_autoval(self):
957 with self.assertRaises(ValueError):
958 Square.objects.create(id=0, root=0, square=1)
959
960
961 class DBConstraintTestCase(TestCase):
962 def test_can_reference_existent(self):
963 obj = Object.objects.create()
964 ref = ObjectReference.objects.create(obj=obj)
965 self.assertEqual(ref.obj, obj)
966
967 ref = ObjectReference.objects.get(obj=obj)
968 self.assertEqual(ref.obj, obj)
969
970 def test_can_reference_non_existent(self):
971 self.assertFalse(Object.objects.filter(id=12345).exists())
972 ref = ObjectReference.objects.create(obj_id=12345)
973 ref_new = ObjectReference.objects.get(obj_id=12345)
974 self.assertEqual(ref, ref_new)
975
976 with self.assertRaises(Object.DoesNotExist):
977 ref.obj
978
979 def test_many_to_many(self):
980 obj = Object.objects.create()
981 obj.related_objects.create()
982 self.assertEqual(Object.objects.count(), 2)
983 self.assertEqual(obj.related_objects.count(), 1)
984
985 intermediary_model = Object._meta.get_field(
986 "related_objects"
987 ).remote_field.through
988 intermediary_model.objects.create(from_object_id=obj.id, to_object_id=12345)
989 self.assertEqual(obj.related_objects.count(), 1)
990 self.assertEqual(intermediary_model.objects.count(), 2)