diff options
| author | Sage Abdullah <me@laymonage.com> | 2026-02-27 22:12:07 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-27 17:12:07 -0500 |
| commit | b415cef5d21cd5beac212a833f9f0b7727899f64 (patch) | |
| tree | ca7c16390dc9050c550b66673924f1f111c459ac | |
| parent | e4372f1b5c8877bd0ed792606eb97641ca913368 (diff) | |
Fixed #36946 -- Respected test database name when running tests in parallel on SQLite.
The "spawn" and "forkserver" multiprocessing modes were affected.
| -rw-r--r-- | django/db/backends/sqlite3/creation.py | 3 | ||||
| -rw-r--r-- | tests/backends/sqlite/test_creation.py | 50 |
2 files changed, 52 insertions, 1 deletions
diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py index d57bf9ee1f..7105b73257 100644 --- a/django/db/backends/sqlite3/creation.py +++ b/django/db/backends/sqlite3/creation.py @@ -142,8 +142,9 @@ class DatabaseCreation(BaseDatabaseCreation): connection_str = ( f"file:memorydb_{alias}_{_worker_id}?mode=memory&cache=shared" ) + source_db_name = settings_dict["NAME"] source_db = self.connection.Database.connect( - f"file:{alias}_{_worker_id}.sqlite3?mode=ro", uri=True + f"file:{source_db_name}?mode=ro", uri=True ) target_db = sqlite3.connect(connection_str, uri=True) source_db.backup(target_db) diff --git a/tests/backends/sqlite/test_creation.py b/tests/backends/sqlite/test_creation.py index fe3959c85b..c38eded41d 100644 --- a/tests/backends/sqlite/test_creation.py +++ b/tests/backends/sqlite/test_creation.py @@ -1,5 +1,6 @@ import copy import multiprocessing +import sqlite3 import unittest from unittest import mock @@ -41,3 +42,52 @@ class TestDbSignatureTests(SimpleTestCase): msg = "Cloning with start method 'unsupported' is not supported." with self.assertRaisesMessage(NotSupportedError, msg): connection.creation.get_test_db_clone_settings(1) + + @mock.patch.object(multiprocessing, "get_start_method", return_value="spawn") + def test_setup_worker_connection_respects_test_database_name(self, *mocked_objects): + test_connection = copy.copy(connections[DEFAULT_DB_ALIAS]) + test_connection.settings_dict = copy.deepcopy( + connections[DEFAULT_DB_ALIAS].settings_dict + ) + tests = [ + ("mytest.db", "mytest_2.db"), + ("mytest", "mytest_2"), + ] + for test_db_name, expected_source_db_name in tests: + with self.subTest(test_db_name=test_db_name): + # When calling setup_worker_connection(), the test db has been + # created already and its name has been copied to + # settings_dict["NAME"], so no need to set ["TEST"]["NAME"]. + test_connection.settings_dict["NAME"] = test_db_name + creation_class = test_connection.creation_class(test_connection) + worker_id = 2 + mock_source_db = mock.MagicMock() + mock_target_db = mock.MagicMock() + with ( + # Mock connection to source test database. + mock.patch.object( + test_connection.Database, + "connect", + return_value=mock_source_db, + ) as mock_source_connect, + # Mock connection to target in-memory db for copying. + mock.patch.object( + sqlite3, + "connect", + return_value=mock_target_db, + ) as mock_target_connect, + # Mock reconnection to target in-memory db after copying. + mock.patch.object(test_connection, "connect"), + ): + creation_class.setup_worker_connection(worker_id) + mock_source_connect.assert_called_once_with( + f"file:{expected_source_db_name}?mode=ro", + uri=True, + ) + mock_target_connect.assert_called_once_with( + "file:memorydb_default_2?mode=memory&cache=shared", + uri=True, + ) + mock_source_db.backup.assert_called_once_with(mock_target_db) + mock_source_db.close.assert_called_once() + mock_target_db.close.assert_called_once() |
