Skip to content

Commit f6471c8

Browse files
committed
Improve test coverage #74
1 parent 6574659 commit f6471c8

File tree

7 files changed

+123
-30
lines changed

7 files changed

+123
-30
lines changed

test/unit/repositories/test_catalogue_category.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,20 @@ def call_list(self, parent_id: Optional[str]) -> None:
529529
parent_id=parent_id, session=self.mock_session
530530
)
531531

532-
def check_list_success(self) -> None:
533-
"""Checks that a prior call to `call_list` worked as expected."""
532+
def check_list_success(self, assert_find: bool = True) -> None:
533+
"""Checks that a prior call to `call_list` worked as expected.
534+
535+
:param assert_find: If `True` it asserts whether a `find_one` call was made, else it asserts that no call was
536+
made.
537+
"""
534538

535539
self.mock_utils.list_query.assert_called_once_with(self._parent_id_filter, "catalogue categories")
536-
self.catalogue_categories_collection.find.assert_called_once_with(
537-
self.mock_utils.list_query.return_value, session=self.mock_session
538-
)
540+
if assert_find:
541+
self.catalogue_categories_collection.find.assert_called_once_with(
542+
self.mock_utils.list_query.return_value, session=self.mock_session
543+
)
544+
else:
545+
self.catalogue_categories_collection.find.assert_not_called()
539546

540547
assert self._obtained_catalogue_categories_out == self._expected_catalogue_categories_out
541548

@@ -586,6 +593,14 @@ def test_list_with_parent_id_with_no_results(self):
586593
self.call_list(parent_id=str(ObjectId()))
587594
self.check_list_success()
588595

596+
def test_list_with_invalid_parent_id(self):
597+
"""Test listing all catalogue categories with an invalid `parent_id` filter returning no results."""
598+
599+
self.mock_list([])
600+
self.mock_utils.list_query.side_effect = InvalidObjectIdError("Invalid ID")
601+
self.call_list(parent_id="invalid-id")
602+
self.check_list_success(assert_find=False)
603+
589604

590605
class UpdateDSL(CatalogueCategoryRepoDSL):
591606
"""Base class for `update` tests."""

test/unit/repositories/test_catalogue_item.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,21 @@ def call_list(self, catalogue_category_id: Optional[str]) -> None:
267267
catalogue_category_id=catalogue_category_id, session=self.mock_session
268268
)
269269

270-
def check_list_success(self) -> None:
271-
"""Checks that a prior call to `call_list` worked as expected."""
270+
def check_list_success(self, assert_find: bool = True) -> None:
271+
"""Checks that a prior call to `call_list` worked as expected.
272272
273-
expected_query = {}
274-
if self._catalogue_category_id_filter:
275-
expected_query["catalogue_category_id"] = CustomObjectId(self._catalogue_category_id_filter)
273+
:param assert_find: If `True` it asserts whether a `find_one` call was made, else it asserts that no call was
274+
made.
275+
"""
276276

277-
self.catalogue_items_collection.find.assert_called_once_with(expected_query, session=self.mock_session)
277+
if assert_find:
278+
expected_query = {}
279+
if self._catalogue_category_id_filter:
280+
expected_query["catalogue_category_id"] = CustomObjectId(self._catalogue_category_id_filter)
281+
282+
self.catalogue_items_collection.find.assert_called_once_with(expected_query, session=self.mock_session)
283+
else:
284+
self.catalogue_items_collection.find.assert_not_called()
278285

279286
assert self._obtained_catalogue_items_out == self._expected_catalogue_items_out
280287

@@ -313,6 +320,13 @@ def test_list_with_catalogue_category_id_with_no_results(self):
313320
self.call_list(catalogue_category_id=str(ObjectId()))
314321
self.check_list_success()
315322

323+
def test_list_with_invalid_catalogue_category_id_with_no_results(self):
324+
"""Test listing all catalogue categories with an invalid `catalogue_category_id` filter returning no results."""
325+
326+
self.mock_list([])
327+
self.call_list(catalogue_category_id="invalid-id")
328+
self.check_list_success(assert_find=False)
329+
316330

317331
class UpdateDSL(CatalogueItemRepoDSL):
318332
"""Base class for `update` tests."""

test/unit/repositories/test_item.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,16 +275,23 @@ def call_list(self, system_id: Optional[str], catalogue_item_id: Optional[str])
275275
system_id=system_id, catalogue_item_id=catalogue_item_id, session=self.mock_session
276276
)
277277

278-
def check_list_success(self) -> None:
279-
"""Checks that a prior call to `call_list` worked as expected."""
278+
def check_list_success(self, assert_find: bool = True) -> None:
279+
"""Checks that a prior call to `call_list` worked as expected.
280280
281-
expected_query = {}
282-
if self._system_id_filter:
283-
expected_query["system_id"] = CustomObjectId(self._system_id_filter)
284-
if self._catalogue_item_id_filter:
285-
expected_query["catalogue_item_id"] = CustomObjectId(self._catalogue_item_id_filter)
281+
:param assert_find: If `True` it asserts whether a `find_one` call was made, else it asserts that no call was
282+
made.
283+
"""
286284

287-
self.items_collection.find.assert_called_once_with(expected_query, session=self.mock_session)
285+
if assert_find:
286+
expected_query = {}
287+
if self._system_id_filter:
288+
expected_query["system_id"] = CustomObjectId(self._system_id_filter)
289+
if self._catalogue_item_id_filter:
290+
expected_query["catalogue_item_id"] = CustomObjectId(self._catalogue_item_id_filter)
291+
292+
self.items_collection.find.assert_called_once_with(expected_query, session=self.mock_session)
293+
else:
294+
self.items_collection.find.assert_not_called()
288295

289296
assert self._obtained_items_out == self._expected_items_out
290297

@@ -306,13 +313,27 @@ def test_list_with_system_id_filter(self):
306313
self.call_list(system_id=str(ObjectId()), catalogue_item_id=None)
307314
self.check_list_success()
308315

316+
def test_list_with_invalid_system_id_filter(self):
317+
"""Test listing all items when giving an invalid `system_id`."""
318+
319+
self.mock_list([])
320+
self.call_list(system_id="invalid-id", catalogue_item_id=None)
321+
self.check_list_success(assert_find=False)
322+
309323
def test_list_with_catalogue_category_id_filter(self):
310324
"""Test listing all items with a given `catalogue_category_id`."""
311325

312326
self.mock_list([ITEM_IN_DATA_REQUIRED_VALUES_ONLY, ITEM_IN_DATA_ALL_VALUES_NO_PROPERTIES])
313327
self.call_list(system_id=None, catalogue_item_id=str(ObjectId()))
314328
self.check_list_success()
315329

330+
def test_list_with_invalid_catalogue_category_id_filter(self):
331+
"""Test listing all items when given an invalid `catalogue_category_id`."""
332+
333+
self.mock_list([])
334+
self.call_list(system_id=None, catalogue_item_id="invalid-id")
335+
self.check_list_success(assert_find=False)
336+
316337
def test_list_with_system_id_and_catalogue_category_id_with_no_results(self):
317338
"""Test listing all items with a `system_id` and `catalogue_category_id` filter returning no results."""
318339

test/unit/repositories/test_manufacturer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ def test_update_name_to_duplicate(self):
476476
MANUFACTURER_IN_DATA_A,
477477
duplicate_manufacturer_in_data={**MANUFACTURER_IN_DATA_A, "name": duplicate_name},
478478
)
479+
self.call_update_expecting_error(manufacturer_id, DuplicateRecordError)
480+
self.check_update_failed_with_exception("Duplicate manufacturer found")
479481

480482
def test_update_with_invalid_id(self):
481483
"""Test updating a manufacturer with an invalid ID."""

test/unit/repositories/test_system.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# Expect some duplicate code inside tests as the tests for the different entities can be very similar
66
# pylint: disable=duplicate-code
7+
# pylint: disable=too-many-lines
78

89
from test.mock_data import ITEM_DATA_REQUIRED_VALUES_ONLY, SYSTEM_IN_DATA_NO_PARENT_A, SYSTEM_IN_DATA_NO_PARENT_B
910
from test.unit.repositories.conftest import RepositoryTestHelpers
@@ -445,13 +446,16 @@ def call_list(self, parent_id: Optional[str]):
445446

446447
self._obtained_systems_out = self.system_repository.list(parent_id=parent_id, session=self.mock_session)
447448

448-
def check_list_success(self):
449+
def check_list_success(self, assert_find: bool = True):
449450
"""Checks that a prior call to `call_list` worked as expected."""
450451

451452
self.mock_utils.list_query.assert_called_once_with(self._parent_id_filter, "systems")
452-
self.systems_collection.find.assert_called_once_with(
453-
self.mock_utils.list_query.return_value, session=self.mock_session
454-
)
453+
if assert_find:
454+
self.systems_collection.find.assert_called_once_with(
455+
self.mock_utils.list_query.return_value, session=self.mock_session
456+
)
457+
else:
458+
self.systems_collection.find.assert_not_called()
455459

456460
assert self._obtained_systems_out == self._expected_systems_out
457461

@@ -487,6 +491,14 @@ def test_list_with_parent_id_with_no_results(self):
487491
self.call_list(parent_id=str(ObjectId()))
488492
self.check_list_success()
489493

494+
def test_list_with_invalid_parent_id(self):
495+
"""Test listing all systems with an invalid `parent_id` filter returning no results."""
496+
497+
self.mock_list([])
498+
self.mock_utils.list_query.side_effect = InvalidObjectIdError("Invalid ID")
499+
self.call_list(parent_id="invalid-id")
500+
self.check_list_success(assert_find=False)
501+
490502

491503
class UpdateDSL(SystemRepoDSL):
492504
"""Base class for `update` tests."""

test/unit/services/test_catalogue_item.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def check_create_success(self) -> None:
231231
# This is the get for the obsolete replacement catalogue item
232232
if self._catalogue_item_post.obsolete_replacement_catalogue_item_id:
233233
self.mock_catalogue_item_repository.get.assert_called_once_with(
234-
self._catalogue_item_post.obsolete_replacement_catalogue_item_id, entity_type_modifier="specified"
234+
self._catalogue_item_post.obsolete_replacement_catalogue_item_id, entity_type_modifier="replacement"
235235
)
236236

237237
self.wrapped_utils.process_properties.assert_called_once_with(
@@ -273,7 +273,7 @@ def test_create_with_all_properties(self):
273273
catalogue item."""
274274

275275
self.mock_create(
276-
CATALOGUE_ITEM_DATA_WITH_ALL_PROPERTIES,
276+
{**CATALOGUE_ITEM_DATA_WITH_ALL_PROPERTIES, "obsolete_replacement_catalogue_item_id": str(ObjectId())},
277277
catalogue_category_in_data=BASE_CATALOGUE_CATEGORY_IN_DATA_WITH_PROPERTIES_MM,
278278
manufacturer_in_data=MANUFACTURER_IN_DATA_A,
279279
)

test/unit/services/test_system.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from bson import ObjectId
1515

1616
from inventory_management_system_api.core.custom_object_id import CustomObjectId
17-
from inventory_management_system_api.core.exceptions import ChildElementsExistError
17+
from inventory_management_system_api.core.exceptions import ChildElementsExistError, InvalidObjectIdError
1818
from inventory_management_system_api.models.system import SystemIn, SystemOut
1919
from inventory_management_system_api.schemas.system import SystemPatchSchema, SystemPostSchema
2020
from inventory_management_system_api.services import utils
@@ -275,10 +275,13 @@ def mock_update(self, system_id: str, system_patch_data: dict, stored_system_pos
275275

276276
# Construct the expected input for the repository
277277
merged_system_data = {**(stored_system_post_data or {}), **system_patch_data}
278-
self._expected_system_in = SystemIn(
279-
**merged_system_data,
280-
code=utils.generate_code(merged_system_data["name"], "system"),
281-
)
278+
try:
279+
self._expected_system_in = SystemIn(
280+
**merged_system_data,
281+
code=utils.generate_code(merged_system_data["name"], "system"),
282+
)
283+
except InvalidObjectIdError:
284+
pass
282285

283286
def call_update(self, system_id: str) -> None:
284287
"""
@@ -349,6 +352,32 @@ def test_update_all_fields_except_parent_id(self):
349352
self.call_update(system_id)
350353
self.check_update_success()
351354

355+
def test_update_parent_id(self):
356+
"""Test updating the parent ID of a system."""
357+
358+
system_id = str(ObjectId())
359+
360+
self.mock_update(
361+
system_id,
362+
system_patch_data={"parent_id": str(ObjectId())},
363+
stored_system_post_data=SYSTEM_POST_DATA_NO_PARENT_A,
364+
)
365+
self.call_update(system_id)
366+
self.check_update_success()
367+
368+
def test_update_invalid_parent_id(self):
369+
"""Test updating the parent ID of a system to an invalid ID."""
370+
371+
system_id = str(ObjectId())
372+
373+
self.mock_update(
374+
system_id,
375+
system_patch_data={"parent_id": "invalid-id"},
376+
stored_system_post_data=SYSTEM_POST_DATA_NO_PARENT_A,
377+
)
378+
self.call_update_expecting_error(system_id, InvalidObjectIdError)
379+
self.check_update_failed_with_exception("Invalid ObjectId value 'invalid-id'")
380+
352381
def test_update_description_only(self):
353382
"""Test updating system's description field only (code should not need regenerating as name doesn't change)."""
354383

0 commit comments

Comments
 (0)