Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions django/db/backends/base/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class BaseDatabaseCreation:
destruction of the test database.
"""

# If this backend's destroy_test_db() closes the database connection, this
# attribute must be set to the name of the connection closing method (e.g.
# "close" on Oracle, "close_pool" on MongoDB) to avoid failures in some
# backends tests.
destroy_test_db_connection_close_method = None

def __init__(self, connection):
self.connection = connection

Expand Down
2 changes: 2 additions & 0 deletions django/db/backends/oracle/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@


class DatabaseCreation(BaseDatabaseCreation):
destroy_test_db_connection_close_method = "close"

@cached_property
def _maindb_connection(self):
"""
Expand Down
27 changes: 12 additions & 15 deletions tests/backends/base/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,21 @@ def test_custom_test_name_with_test_prefix(self):
class TestDbCreationTests(SimpleTestCase):
available_apps = ["backends.base.app_unmigrated"]

@staticmethod
def patch_close_connection(creation):
# If DatabaseCreation.destroy_test_db() closes the database connection,
# that behavior must be disabled to prevent each test from crashing.
if close_method_name := creation.destroy_test_db_connection_close_method:
setattr(creation.connection, close_method_name, mock.Mock())

@mock.patch("django.db.migrations.executor.MigrationExecutor.migrate")
def test_migrate_test_setting_false(
self, mocked_migrate, mocked_sync_apps, *mocked_objects
):
test_connection = get_connection_copy()
test_connection.settings_dict["TEST"]["MIGRATE"] = False
creation = test_connection.creation_class(test_connection)
if connection.vendor == "oracle":
# Don't close connection on Oracle.
creation.connection.close = mock.Mock()
self.patch_close_connection(creation)
old_database_name = test_connection.settings_dict["NAME"]
try:
with mock.patch.object(creation, "_create_test_db"):
Expand Down Expand Up @@ -104,9 +109,7 @@ def test_migrate_test_setting_false_ensure_schema(
test_connection = get_connection_copy()
test_connection.settings_dict["TEST"]["MIGRATE"] = False
creation = test_connection.creation_class(test_connection)
if connection.vendor == "oracle":
# Don't close connection on Oracle.
creation.connection.close = mock.Mock()
self.patch_close_connection(creation)
old_database_name = test_connection.settings_dict["NAME"]
try:
with mock.patch.object(creation, "_create_test_db"):
Expand All @@ -128,9 +131,7 @@ def test_migrate_test_setting_true(
test_connection = get_connection_copy()
test_connection.settings_dict["TEST"]["MIGRATE"] = True
creation = test_connection.creation_class(test_connection)
if connection.vendor == "oracle":
# Don't close connection on Oracle.
creation.connection.close = mock.Mock()
self.patch_close_connection(creation)
old_database_name = test_connection.settings_dict["NAME"]
try:
with mock.patch.object(creation, "_create_test_db"):
Expand Down Expand Up @@ -158,9 +159,7 @@ def test_mark_expected_failures_and_skips_call(
"""
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()
self.patch_close_connection(creation)
old_database_name = test_connection.settings_dict["NAME"]
try:
with mock.patch.object(creation, "_create_test_db"):
Expand All @@ -175,9 +174,7 @@ def test_mark_expected_failures_and_skips_call(
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()
self.patch_close_connection(creation)
old_database_name = test_connection.settings_dict["NAME"]
msg = (
"DatabaseCreation.create_test_db(serialize) is deprecated. Call "
Expand Down