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