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.
This commit is contained in:
Simon Charette 2018-12-24 13:23:47 -05:00 committed by Tim Graham
parent 83677faf86
commit f6d8b0c47e
1 changed files with 22 additions and 10 deletions

View File

@ -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,15 +270,16 @@ 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):
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 should be properly initialized for the 'other' DB
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 should report that they support transactions
# And all the DBs report that they support transactions.
self.assertTrue(connections_support_transactions(), msg)