diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index c95bd8f1069..477398da20a 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -247,6 +247,17 @@ class SQLiteInMemoryTestDbs(TransactionTestCase): @unittest.skipUnless(all(db.connections[conn].vendor == 'sqlite' for conn in db.connections), "This is an sqlite-specific issue") def test_transaction_support(self): + # Assert connections mocking is appropriately applied by preventing + # any attempts at calling create_test_db on the global connection + # objects. + for connection in db.connections.all(): + create_test_db = mock.patch.object( + connection.creation, + 'create_test_db', + side_effect=AssertionError("Global connection object shouldn't be manipulated.") + ) + create_test_db.start() + self.addCleanup(create_test_db.stop) for option_key, option_value in ( ('NAME', ':memory:'), ('TEST', {'NAME': ':memory:'})): tested_connections = db.ConnectionHandler({ @@ -259,16 +270,17 @@ class SQLiteInMemoryTestDbs(TransactionTestCase): option_key: option_value, }, }) - with mock.patch('django.db.connections', new=tested_connections): - with mock.patch('django.test.testcases.connections', new=tested_connections): - other = tested_connections['other'] - DiscoverRunner(verbosity=0).setup_databases() - msg = ("DATABASES setting '%s' option set to sqlite3's ':memory:' value " - "shouldn't interfere with transaction support detection." % option_key) - # Transaction support should be properly initialized for the 'other' DB - self.assertTrue(other.features.supports_transactions, msg) - # And all the DBs should report that they support transactions - self.assertTrue(connections_support_transactions(), msg) + with mock.patch('django.test.utils.connections', new=tested_connections): + other = tested_connections['other'] + DiscoverRunner(verbosity=0).setup_databases() + msg = ( + "DATABASES setting '%s' option set to sqlite3's ':memory:' value " + "shouldn't interfere with transaction support detection." % option_key + ) + # Transaction support is properly initialized for the 'other' DB. + self.assertTrue(other.features.supports_transactions, msg) + # And all the DBs report that they support transactions. + self.assertTrue(connections_support_transactions(), msg) class DummyBackendTest(unittest.TestCase):