Skip to content

Commit

Permalink
Update and add repo unit tests #417
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Dec 2, 2024
1 parent b7fd82c commit 642a7df
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
2 changes: 1 addition & 1 deletion inventory_management_system_api/repositories/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def update_names_of_all_properties_with_id(

# pylint:enable=duplicate-code

def count_with_usage_statuses_ids_in(
def count_with_usage_status_ids_in(
self,
catalogue_item_id: ObjectId,
usage_status_ids: List[CustomObjectId],
Expand Down
2 changes: 1 addition & 1 deletion inventory_management_system_api/services/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def perform_number_of_spares_recalculation(
"""

# Now calculate the new number of spares
new_number_of_spares = item_repository.count_with_usage_statuses_ids_in(
new_number_of_spares = item_repository.count_with_usage_status_ids_in(
catalogue_item_id, usage_status_ids, session=session
)

Expand Down
61 changes: 54 additions & 7 deletions test/unit/repositories/test_catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,27 +613,29 @@ def test_has_child_elements_with_child_item(self):
class ListIDsDSL(CatalogueItemRepoDSL):
"""Base class for `list_ids` tests"""

_list_ids_catalogue_category_id: str
_catalogue_category_id_filter: Optional[str]
_list_ids_result: list[ObjectId]

def call_list_ids(self, catalogue_category_id: str) -> None:
def call_list_ids(self, catalogue_category_id: Optional[str]) -> None:
"""Calls the `CatalogueItemRepo` `list_ids` method.
:param catalogue_category_id: ID of the catalogue category.
"""

self._list_ids_catalogue_category_id = catalogue_category_id
self._catalogue_category_id_filter = catalogue_category_id
self._list_ids_result = self.catalogue_item_repository.list_ids(
catalogue_category_id, session=self.mock_session
)

def check_list_ids_success(self) -> None:
"""Checks that a prior call to `call_list_ids` worked as expected."""

expected_query = {}
if self._catalogue_category_id_filter:
expected_query["catalogue_category_id"] = CustomObjectId(self._catalogue_category_id_filter)

self.catalogue_items_collection.find.assert_called_once_with(
{"catalogue_category_id": CustomObjectId(self._list_ids_catalogue_category_id)},
{"_id": 1},
session=self.mock_session,
expected_query, {"_id": 1}, session=self.mock_session
)
self.catalogue_items_collection.find.return_value.distinct.assert_called_once_with("_id")

Expand All @@ -646,7 +648,13 @@ class TestListIDs(ListIDsDSL):
def test_list_ids(self):
"""Test `list_ids`."""

self.call_list_ids(str(ObjectId()))
self.call_list_ids(catalogue_category_id=None)
self.check_list_ids_success()

def test_list_ids_with_catalogue_category_id_filter(self):
"""Test `list_ids`."""

self.call_list_ids(catalogue_category_id=str(ObjectId()))
self.check_list_ids_success()


Expand Down Expand Up @@ -746,3 +754,42 @@ def test_update_names_of_all_properties_with_id(self):

self.call_update_names_of_all_properties_with_id(str(ObjectId()), "New name")
self.check_update_names_of_all_properties_with_id()


class UpdateNumberOfSparesDSL(CatalogueItemRepoDSL):
"""Base class for `update_number_of_spares` tests"""

_update_number_of_spares_catalogue_item_id: ObjectId
_update_number_of_spares_number_of_spares: Optional[int]

def call_update_number_of_spares(self, catalogue_item_id: ObjectId, number_of_spares: Optional[int]) -> None:
"""Calls the `CatalogueItemRepo` `update_number_of_spares` method.
:param catalogue_item_id: The ID of the catalogue item to update.
:param number_of_spares: New number of spares to update to.
"""

self._update_number_of_spares_catalogue_item_id = catalogue_item_id
self._update_number_of_spares_number_of_spares = number_of_spares
self.catalogue_item_repository.update_number_of_spares(
catalogue_item_id, number_of_spares, session=self.mock_session
)

def check_update_number_of_spares(self) -> None:
"""Checks that a prior call to `update_number_of_spares` worked as expected"""

self.catalogue_items_collection.update_one.assert_called_once_with(
{"_id": self._update_number_of_spares_catalogue_item_id},
{"$set": {"number_of_spares": self._update_number_of_spares_number_of_spares}},
session=self.mock_session,
)


class TestUpdateNumberOfSpares(UpdateNumberOfSparesDSL):
"""Tests for `update_number_of_spares`."""

def test_update_number_of_spares(self):
"""Test `update_number_of_spares`."""

self.call_update_number_of_spares(ObjectId(), 42)
self.check_update_number_of_spares()
45 changes: 45 additions & 0 deletions test/unit/repositories/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,48 @@ def test_update_names_of_all_properties_with_id(self):

self.call_update_names_of_all_properties_with_id(str(ObjectId()), "New name")
self.check_update_names_of_all_properties_with_id()


class CountWithUsageStatusIDsInDSL(ItemRepoDSL):
"""Base class for `count_with_usage_status_ids_in` tests"""

_count_with_usage_status_ids_in_catalogue_item_id: ObjectId
_count_with_usage_status_ids_in_usage_status_ids: list[CustomObjectId]

def call_count_with_usage_status_ids_in(
self, catalogue_item_id: ObjectId, usage_status_ids: list[CustomObjectId]
) -> None:
"""Calls the `ItemRepo` `count_with_usage_status_ids_in` method.
:param catalogue_item_id: ID of the catalogue item for which items should be counted.
:param usage_status_id: List of usage status IDs which should be included in the count.
"""

self._count_with_usage_status_ids_in_catalogue_item_id = catalogue_item_id
self._count_with_usage_status_ids_in_usage_status_ids = usage_status_ids
self.item_repository.count_with_usage_status_ids_in(
catalogue_item_id, usage_status_ids, session=self.mock_session
)

def check_count_with_usage_status_ids_in(self) -> None:
"""Checks that a prior call to `count_with_usage_status_ids_in` worked as expected"""

self.items_collection.count_documents.assert_called_once_with(
{
"catalogue_item_id": self._count_with_usage_status_ids_in_catalogue_item_id,
"usage_status_id": {"$in": self._count_with_usage_status_ids_in_usage_status_ids},
},
session=self.mock_session,
)


class TestCountWithUsageStatusIDsIn(CountWithUsageStatusIDsInDSL):
"""Tests for `count_with_usage_status_ids_in`."""

def test_count_with_usage_status_ids_in(self):
"""Test `count_with_usage_status_ids_in`."""

self.call_count_with_usage_status_ids_in(
ObjectId(), [CustomObjectId(str(ObjectId())), CustomObjectId(str(ObjectId()))]
)
self.check_count_with_usage_status_ids_in()
4 changes: 3 additions & 1 deletion test/unit/repositories/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def check_get_success(self) -> None:
"""Checks that a prior call to `call_get` worked as expected."""

if self._obtained_out_model_type is SparesDefinitionOut:
self.settings_collection.aggregate.assert_called_once_with(SPARES_DEFINITION_GET_AGGREGATION_PIPELINE)
self.settings_collection.aggregate.assert_called_once_with(
SPARES_DEFINITION_GET_AGGREGATION_PIPELINE, session=self.mock_session
)
else:
self.settings_collection.find_one.assert_called_once_with(
{"_id": self._obtained_out_model_type.SETTING_ID}, session=self.mock_session
Expand Down

0 comments on commit 642a7df

Please sign in to comment.