From f6d8b0c47e5027037ed8c9926b1cc67d328091e6 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Mon, 24 Dec 2018 13:23:47 -0500 Subject: [PATCH] Refs #26840 -- Corrected SQLite connection mocking in a setup_databases() test. The test was expecting connections used by DiscoverRunner.setup_databases() to be the ones defined in django.test.runner but this doesn't hold true since this method was made a proxy of django.test.utils.setup_databases. This broke the TransactionTestCase.serialized_rollback feature in the test suite because calls to create_db_test() cleared the test data persisted on connections objects. Added an assertions to prevent this from happening again. --- tests/test_runner/tests.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) 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):