summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthijs Kooijman <matthijs@stdin.nl>2020-01-18 19:59:56 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-01-20 11:00:17 +0100
commitb64b1b2e1a17da3fd702cde4bfc2b6e0689b4348 (patch)
tree0f646d144291d553b7159036ab544a83e9fc6538
parentf34be5294d8bd9530079525fb56e661816a63e20 (diff)
Fixed #31117 -- Isolated backends.base.test_creation.TestDbCreationTests.
Previously, this test could modify global state by changing connection.settings_dict. This dict is a reference to the same dict as django.db.connections.databases['default'], which is thus also changed. The cleanup of this test would replace connection.settings_dic` with a saved copy, which would leave the dict itself modified. Additionally, create_test_db() would also modify these same dicts, as well as settings.databases['default']['NAME'] by adding a "test_" prefix, which is what can cause problems later. This patch: - makes a complete copy of the connection and work on that, to improve isolation. - calls destroy_test_db() to let that code clean up anything done by create_test_db().
-rw-r--r--tests/backends/base/test_creation.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/tests/backends/base/test_creation.py b/tests/backends/base/test_creation.py
index eb6004f898..b91466911a 100644
--- a/tests/backends/base/test_creation.py
+++ b/tests/backends/base/test_creation.py
@@ -49,23 +49,27 @@ class TestDbSignatureTests(SimpleTestCase):
@mock.patch('django.core.management.commands.migrate.Command.handle', return_value=None)
class TestDbCreationTests(SimpleTestCase):
def test_migrate_test_setting_false(self, mocked_migrate, mocked_ensure_connection):
- creation = connection.creation_class(connection)
- saved_settings = copy.deepcopy(connection.settings_dict)
+ test_connection = get_connection_copy()
+ test_connection.settings_dict['TEST']['MIGRATE'] = False
+ creation = test_connection.creation_class(test_connection)
+ old_database_name = test_connection.settings_dict['NAME']
try:
- connection.settings_dict['TEST']['MIGRATE'] = False
with mock.patch.object(creation, '_create_test_db'):
creation.create_test_db(verbosity=0, autoclobber=True, serialize=False)
mocked_migrate.assert_not_called()
finally:
- connection.settings_dict = saved_settings
+ with mock.patch.object(creation, '_destroy_test_db'):
+ creation.destroy_test_db(old_database_name, verbosity=0)
def test_migrate_test_setting_true(self, mocked_migrate, mocked_ensure_connection):
- creation = connection.creation_class(connection)
- saved_settings = copy.deepcopy(connection.settings_dict)
+ test_connection = get_connection_copy()
+ test_connection.settings_dict['TEST']['MIGRATE'] = True
+ creation = test_connection.creation_class(test_connection)
+ old_database_name = test_connection.settings_dict['NAME']
try:
- connection.settings_dict['TEST']['MIGRATE'] = True
with mock.patch.object(creation, '_create_test_db'):
creation.create_test_db(verbosity=0, autoclobber=True, serialize=False)
mocked_migrate.assert_called_once()
finally:
- connection.settings_dict = saved_settings
+ with mock.patch.object(creation, '_destroy_test_db'):
+ creation.destroy_test_db(old_database_name, verbosity=0)