summaryrefslogtreecommitdiff
path: root/tests/test_runner
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2018-12-24 13:23:47 -0500
committerTim Graham <timograham@gmail.com>2018-12-24 15:32:27 -0500
commitf6d8b0c47e5027037ed8c9926b1cc67d328091e6 (patch)
treebc833ff16070fb3f7aa444b8635fe9e03bd56ceb /tests/test_runner
parent83677faf860f1a25acd667131275307abaa23fc4 (diff)
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.
Diffstat (limited to 'tests/test_runner')
-rw-r--r--tests/test_runner/tests.py32
1 files changed, 22 insertions, 10 deletions
diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py
index c95bd8f106..477398da20 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):