|
4 | 4 | from data import models |
5 | 5 | from data.models import ( |
6 | 6 | CONTINENT_ENUM, |
7 | | - countries_validator, |
8 | | - states_validator, |
9 | 7 | cities_validator, |
| 8 | + countries_validator, |
10 | 9 | ensure_collection, |
| 10 | + ensure_indexes, |
| 11 | + initialize_database_schema, |
| 12 | + states_validator, |
11 | 13 | ) |
12 | 14 |
|
13 | 15 |
|
@@ -39,9 +41,9 @@ def mock_collection(self): |
39 | 41 | @pytest.fixture(autouse=True) |
40 | 42 | def setup_patches(self, mock_client, mock_db, mock_collection): |
41 | 43 | """Set up patches for all database-related operations.""" |
42 | | - with patch("data.models.connect_db", return_value=mock_client), patch( |
43 | | - "data.models.db", mock_db |
44 | | - ), patch.dict(os.environ, {"DB_NAME": "test_db"}): |
| 44 | + with patch("data.models.connect_db", return_value=mock_client), patch.dict( |
| 45 | + os.environ, {"DB_NAME": "test_db"} |
| 46 | + ): |
45 | 47 | mock_client.__getitem__.return_value = mock_db |
46 | 48 | mock_db.get_collection.return_value = mock_collection |
47 | 49 | yield |
@@ -158,57 +160,46 @@ def test_ensure_collection_updates_existing_collection(self, mock_db): |
158 | 160 | validationLevel="strict", |
159 | 161 | ) |
160 | 162 |
|
161 | | - def test_collections_are_ensured_on_import(self, mock_db): |
162 | | - """Test that collections are ensured when module is imported.""" |
163 | | - # Test the ensure_collection function directly since it's called during import |
164 | | - # We can verify the function works correctly with our mocked database |
165 | | - |
166 | | - # Test creating a new collection |
| 163 | + def test_collections_are_initialized_explicitly(self, mock_db): |
| 164 | + """Test that schema setup only happens when explicitly requested.""" |
167 | 165 | mock_db.list_collection_names.return_value = [] |
168 | | - ensure_collection("test_collection", {"test": "validator"}) |
169 | | - mock_db.create_collection.assert_called_with( |
170 | | - "test_collection", |
171 | | - validator={"test": "validator"}, |
| 166 | + |
| 167 | + initialize_database_schema(db=mock_db) |
| 168 | + |
| 169 | + assert mock_db.create_collection.call_count == 4 |
| 170 | + mock_db.create_collection.assert_any_call( |
| 171 | + "continents", |
| 172 | + validator=models.continents_validator, |
172 | 173 | validationAction="error", |
173 | 174 | validationLevel="strict", |
174 | 175 | ) |
175 | | - |
176 | | - # Test updating an existing collection |
177 | | - mock_db.reset_mock() |
178 | | - mock_db.list_collection_names.return_value = ["test_collection"] |
179 | | - ensure_collection("test_collection", {"test": "validator"}) |
180 | | - mock_db.command.assert_called_with( |
181 | | - "collMod", |
182 | | - "test_collection", |
183 | | - validator={"test": "validator"}, |
| 176 | + mock_db.create_collection.assert_any_call( |
| 177 | + "countries", |
| 178 | + validator=countries_validator, |
| 179 | + validationAction="error", |
| 180 | + validationLevel="strict", |
| 181 | + ) |
| 182 | + mock_db.create_collection.assert_any_call( |
| 183 | + "states", |
| 184 | + validator=states_validator, |
| 185 | + validationAction="error", |
| 186 | + validationLevel="strict", |
| 187 | + ) |
| 188 | + mock_db.create_collection.assert_any_call( |
| 189 | + "cities", |
| 190 | + validator=cities_validator, |
184 | 191 | validationAction="error", |
185 | 192 | validationLevel="strict", |
186 | 193 | ) |
187 | | - |
188 | | - # Verify that the validators are properly defined |
189 | | - assert countries_validator is not None |
190 | | - assert states_validator is not None |
191 | | - assert cities_validator is not None |
192 | 194 |
|
193 | 195 | def test_database_indexes_creation(self, mock_db, mock_collection): |
194 | 196 | """Test that database indexes are created properly.""" |
195 | | - # Test that get_collection returns a mock collection that can create indexes |
196 | 197 | mock_db.get_collection.return_value = mock_collection |
197 | 198 |
|
198 | | - # Test that we can get collections for each expected collection type |
199 | | - expected_collections = ["countries", "states", "cities"] |
| 199 | + ensure_indexes(db=mock_db) |
200 | 200 |
|
201 | | - for collection_name in expected_collections: |
202 | | - collection = mock_db.get_collection(collection_name) |
203 | | - assert collection is not None |
204 | | - # Verify the collection can create indexes |
205 | | - collection.create_index("test_field", unique=True, name="test_index") |
206 | | - collection.create_index.assert_called_with( |
207 | | - "test_field", unique=True, name="test_index" |
208 | | - ) |
209 | | - |
210 | | - # Verify get_collection was called for each collection |
211 | | - assert mock_db.get_collection.call_count >= len(expected_collections) |
| 201 | + assert mock_db.get_collection.call_count == 4 |
| 202 | + assert mock_collection.create_index.call_count == 4 |
212 | 203 |
|
213 | 204 | @pytest.mark.parametrize( |
214 | 205 | "validator,expected_required", |
@@ -277,6 +268,20 @@ def test_ensure_collection_command_error(self, mock_db): |
277 | 268 | with pytest.raises(Exception, match="Command error"): |
278 | 269 | ensure_collection("test_collection", {"test": "validator"}) |
279 | 270 |
|
| 271 | + def test_module_import_does_not_touch_database(self, mock_db): |
| 272 | + """Importing the module should not trigger DB schema setup.""" |
| 273 | + mock_db.reset_mock() |
| 274 | + |
| 275 | + with patch("data.models.connect_db") as mock_connect: |
| 276 | + assert countries_validator is not None |
| 277 | + assert states_validator is not None |
| 278 | + assert cities_validator is not None |
| 279 | + mock_connect.assert_not_called() |
| 280 | + |
| 281 | + mock_db.create_collection.assert_not_called() |
| 282 | + mock_db.command.assert_not_called() |
| 283 | + mock_db.get_collection.assert_not_called() |
| 284 | + |
280 | 285 | def test_database_name_from_environment(self): |
281 | 286 | """Test that database name is read from environment variable.""" |
282 | 287 | # Test the logic for getting database name from environment |
|
0 commit comments