diff --git a/django/db/backends/dummy/base.py b/django/db/backends/dummy/base.py index 36c6480a78..0e2edc73cf 100644 --- a/django/db/backends/dummy/base.py +++ b/django/db/backends/dummy/base.py @@ -39,6 +39,7 @@ class DatabaseClient(BaseDatabaseClient): class DatabaseCreation(BaseDatabaseCreation): create_test_db = ignore destroy_test_db = ignore + serialize_db_to_string = ignore class DatabaseIntrospection(BaseDatabaseIntrospection): diff --git a/django/test/utils.py b/django/test/utils.py index ddb85127dc..a3594bfb6d 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -189,6 +189,7 @@ def setup_databases( test_databases, mirrored_aliases = get_unique_databases_and_mirrors(aliases) old_names = [] + serialize_connections = [] for db_name, aliases in test_databases.values(): first_alias = None @@ -200,15 +201,14 @@ def setup_databases( if first_alias is None: first_alias = alias with time_keeper.timed(" Creating '%s'" % alias): - serialize_alias = ( - serialized_aliases is None or alias in serialized_aliases - ) connection.creation.create_test_db( verbosity=verbosity, autoclobber=not interactive, keepdb=keepdb, - serialize=serialize_alias, + serialize=False, ) + if serialized_aliases is None or alias in serialized_aliases: + serialize_connections.append(connection) if parallel > 1: for index in range(parallel): with time_keeper.timed(" Cloning '%s'" % alias): @@ -229,6 +229,16 @@ def setup_databases( connections[mirror_alias].settings_dict ) + # Serialize content of test databases only once all of them are setup + # to account for database mirroring and routing during serialization. + # This slightly horrific process is so people who are testing on databases + # without transactions or using TransactionTestCase still get a clean + # database on every test run. + for serialize_connection in serialize_connections: + serialize_connection._test_serialized_contents = ( + serialize_connection.creation.serialize_db_to_string() + ) + if debug_sql: for alias in connections: connections[alias].force_debug_cursor = True diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index fba8dd3b6f..d58d47b96b 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -938,14 +938,17 @@ class SetupDatabasesTests(unittest.TestCase): }, } ) - with mock.patch( - "django.db.backends.dummy.base.DatabaseWrapper.creation_class" - ) as mocked_db_creation: - with mock.patch("django.test.utils.connections", new=tested_connections): - self.runner_instance.setup_databases() + with ( + mock.patch( + "django.db.backends.dummy.base.DatabaseWrapper.creation_class" + ) as mocked_db_creation, + mock.patch("django.test.utils.connections", new=tested_connections), + ): + self.runner_instance.setup_databases() mocked_db_creation.return_value.create_test_db.assert_called_once_with( - verbosity=0, autoclobber=False, serialize=True, keepdb=False + verbosity=0, autoclobber=False, serialize=False, keepdb=False ) + mocked_db_creation.return_value.serialize_db_to_string.assert_called_once_with() @skipUnlessDBFeature("supports_sequence_reset")