|
1 | 1 | import copy |
2 | 2 | import multiprocessing |
| 3 | +import sqlite3 |
3 | 4 | import unittest |
4 | 5 | from unittest import mock |
5 | 6 |
|
@@ -41,3 +42,52 @@ def test_get_test_db_clone_settings_not_supported(self, *mocked_objects): |
41 | 42 | msg = "Cloning with start method 'unsupported' is not supported." |
42 | 43 | with self.assertRaisesMessage(NotSupportedError, msg): |
43 | 44 | connection.creation.get_test_db_clone_settings(1) |
| 45 | + |
| 46 | + @mock.patch.object(multiprocessing, "get_start_method", return_value="spawn") |
| 47 | + def test_setup_worker_connection_respects_test_database_name(self, *mocked_objects): |
| 48 | + test_connection = copy.copy(connections[DEFAULT_DB_ALIAS]) |
| 49 | + test_connection.settings_dict = copy.deepcopy( |
| 50 | + connections[DEFAULT_DB_ALIAS].settings_dict |
| 51 | + ) |
| 52 | + tests = [ |
| 53 | + ("mytest.db", "mytest_2.db"), |
| 54 | + ("mytest", "mytest_2"), |
| 55 | + ] |
| 56 | + for test_db_name, expected_source_db_name in tests: |
| 57 | + with self.subTest(test_db_name=test_db_name): |
| 58 | + # When calling setup_worker_connection(), the test db has been |
| 59 | + # created already and its name has been copied to |
| 60 | + # settings_dict["NAME"], so no need to set ["TEST"]["NAME"]. |
| 61 | + test_connection.settings_dict["NAME"] = test_db_name |
| 62 | + creation_class = test_connection.creation_class(test_connection) |
| 63 | + worker_id = 2 |
| 64 | + mock_source_db = mock.MagicMock() |
| 65 | + mock_target_db = mock.MagicMock() |
| 66 | + with ( |
| 67 | + # Mock connection to source test database. |
| 68 | + mock.patch.object( |
| 69 | + test_connection.Database, |
| 70 | + "connect", |
| 71 | + return_value=mock_source_db, |
| 72 | + ) as mock_source_connect, |
| 73 | + # Mock connection to target in-memory db for copying. |
| 74 | + mock.patch.object( |
| 75 | + sqlite3, |
| 76 | + "connect", |
| 77 | + return_value=mock_target_db, |
| 78 | + ) as mock_target_connect, |
| 79 | + # Mock reconnection to target in-memory db after copying. |
| 80 | + mock.patch.object(test_connection, "connect"), |
| 81 | + ): |
| 82 | + creation_class.setup_worker_connection(worker_id) |
| 83 | + mock_source_connect.assert_called_once_with( |
| 84 | + f"file:{expected_source_db_name}?mode=ro", |
| 85 | + uri=True, |
| 86 | + ) |
| 87 | + mock_target_connect.assert_called_once_with( |
| 88 | + "file:memorydb_default_2?mode=memory&cache=shared", |
| 89 | + uri=True, |
| 90 | + ) |
| 91 | + mock_source_db.backup.assert_called_once_with(mock_target_db) |
| 92 | + mock_source_db.close.assert_called_once() |
| 93 | + mock_target_db.close.assert_called_once() |
0 commit comments