1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

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.
This commit is contained in:
Simon Charette
2024-12-17 23:38:23 -05:00
committed by Mariusz Felisiak
parent 99ac8e2589
commit 2d34ebe49a
8 changed files with 78 additions and 19 deletions

View File

@@ -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"]