diff options
| author | Simon Charette <charette.s@gmail.com> | 2024-12-17 23:38:23 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2025-02-16 08:43:42 +0100 |
| commit | 2d34ebe49a25d0974392583d5bbd954baf742a32 (patch) | |
| tree | 2d1803e68dadb701615094010310081b6ea9fd23 /tests/backends | |
| parent | 99ac8e2589ea978c1c80ff66b4536814121f77dd (diff) | |
Refs #35967 -- Deprecated BaseDatabaseCreation.create_test_db(serialize).
Given there are no longer any internal usages of serialize=True and it
poses a risk to non-test databases integrity it seems appropriate to
deprecate it.
Diffstat (limited to 'tests/backends')
| -rw-r--r-- | tests/backends/base/test_creation.py | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/tests/backends/base/test_creation.py b/tests/backends/base/test_creation.py index 7e760e8884..202d8e1b40 100644 --- a/tests/backends/base/test_creation.py +++ b/tests/backends/base/test_creation.py @@ -7,6 +7,7 @@ from django.db import DEFAULT_DB_ALIAS, connection, connections from django.db.backends.base.creation import TEST_DATABASE_PREFIX, BaseDatabaseCreation from django.test import SimpleTestCase, TransactionTestCase from django.test.utils import override_settings +from django.utils.deprecation import RemovedInDjango70Warning from ..models import ( CircularA, @@ -79,7 +80,7 @@ class TestDbCreationTests(SimpleTestCase): old_database_name = test_connection.settings_dict["NAME"] try: with mock.patch.object(creation, "_create_test_db"): - creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) + creation.create_test_db(verbosity=0, autoclobber=True) # Migrations don't run. mocked_migrate.assert_called() args, kwargs = mocked_migrate.call_args @@ -109,7 +110,7 @@ class TestDbCreationTests(SimpleTestCase): old_database_name = test_connection.settings_dict["NAME"] try: with mock.patch.object(creation, "_create_test_db"): - creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) + creation.create_test_db(verbosity=0, autoclobber=True) # The django_migrations table is not created. mocked_ensure_schema.assert_not_called() # App is synced. @@ -133,7 +134,7 @@ class TestDbCreationTests(SimpleTestCase): old_database_name = test_connection.settings_dict["NAME"] try: with mock.patch.object(creation, "_create_test_db"): - creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) + creation.create_test_db(verbosity=0, autoclobber=True) # Migrations run. mocked_migrate.assert_called() args, kwargs = mocked_migrate.call_args @@ -163,12 +164,51 @@ class TestDbCreationTests(SimpleTestCase): old_database_name = test_connection.settings_dict["NAME"] try: with mock.patch.object(creation, "_create_test_db"): - creation.create_test_db(verbosity=0, autoclobber=True, serialize=False) + creation.create_test_db(verbosity=0, autoclobber=True) self.assertIs(mark_expected_failures_and_skips.called, False) finally: with mock.patch.object(creation, "_destroy_test_db"): creation.destroy_test_db(old_database_name, verbosity=0) + @mock.patch("django.db.migrations.executor.MigrationExecutor.migrate") + @mock.patch.object(BaseDatabaseCreation, "serialize_db_to_string") + def test_serialize_deprecation(self, serialize_db_to_string, *mocked_objects): + test_connection = get_connection_copy() + creation = test_connection.creation_class(test_connection) + if connection.vendor == "oracle": + # Don't close connection on Oracle. + creation.connection.close = mock.Mock() + old_database_name = test_connection.settings_dict["NAME"] + msg = ( + "DatabaseCreation.create_test_db(serialize) is deprecated. Call " + "DatabaseCreation.serialize_test_db() once all test databases are set up " + "instead if you need fixtures persistence between tests." + ) + try: + with ( + self.assertWarnsMessage(RemovedInDjango70Warning, msg) as ctx, + mock.patch.object(creation, "_create_test_db"), + ): + creation.create_test_db(verbosity=0, serialize=True) + self.assertEqual(ctx.filename, __file__) + serialize_db_to_string.assert_called_once_with() + finally: + with mock.patch.object(creation, "_destroy_test_db"): + creation.destroy_test_db(old_database_name, verbosity=0) + # Now with `serialize` False. + serialize_db_to_string.reset_mock() + try: + with ( + self.assertWarnsMessage(RemovedInDjango70Warning, msg) as ctx, + mock.patch.object(creation, "_create_test_db"), + ): + creation.create_test_db(verbosity=0, serialize=False) + self.assertEqual(ctx.filename, __file__) + serialize_db_to_string.assert_not_called() + finally: + with mock.patch.object(creation, "_destroy_test_db"): + creation.destroy_test_db(old_database_name, verbosity=0) + class TestDeserializeDbFromString(TransactionTestCase): available_apps = ["backends"] |
