Repository: django/django
File: tests/backends/sqlite/test_creation.py (Python)

Code before:
                self.assertEqual(clone_settings_dict["NAME"], expected_clone_name)

    @mock.patch.object(multiprocessing, "get_start_method", return_value="unsupported")
    def test_get_test_db_clone_settings_not_supported(self, *mocked_objects):
        msg = "Cloning with start method 'unsupported' is not supported."
        with self.assertRaisesMessage(NotSupportedError, msg):
            connection.creation.get_test_db_clone_settings(1)

    @unittest.skipUnless(
        multiprocessing.get_start_method() in {"forkserver", "spawn"},
        "Requires forkserver or spawn start method.",
    )
    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 database has been
                # created already and its name has been copied to settings_dict["NAME"],
                # so we don't need to set settings_dict["TEST"]["NAME"] here.
                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 database for copying
                    mock.patch.object(
                        sqlite3,
                        "connect",
                        return_value=mock_target_db,
                    ) as mock_target_connect,
                    # Mock reconnection to target in-memory database 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,

Review comment (suggestion):
Not sure if we want this committed, but I thought this might clear up any confusion in case someone raised the question of "why does this test modify the `["NAME"]` directly instead of `["TEST"]["NAME"]`?"

It's pretty much equivalent to the comment in this method:

https://github.com/django/django/blob/4aefc9ea51cc2d78f43b1dc2aa69732e55d18a56/django/db/backends/base/creation.py#L286-L297

We could also make it so that this test also changes `["TEST"]["NAME"]`, but it'd be redundant and doesn't really have any effect.

Code after:
                self.assertEqual(clone_settings_dict["NAME"], expected_clone_name)

    @mock.patch.object(multiprocessing, "get_start_method", return_value="unsupported")
    def test_get_test_db_clone_settings_not_supported(self, *mocked_objects):
        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",