Skip to content

Commit

Permalink
Add missing assert in catalogue item service update test and align ot…
Browse files Browse the repository at this point in the history
…hers #417
  • Loading branch information
joelvdavies committed Dec 3, 2024
1 parent 8443b9d commit 5847894
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
12 changes: 6 additions & 6 deletions test/unit/repositories/test_manufacturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setup(self, database_mock):
self.mock_session = MagicMock()
yield

def mock_is_duplicate_manufacturer(self, duplicate_manufacturer_in_data: Optional[dict] = None) -> None:
def _mock_is_duplicate_manufacturer(self, duplicate_manufacturer_in_data: Optional[dict] = None) -> None:
"""
Mocks database methods appropriately for when the `_is_duplicate_manufacturer` repo method will be called.
Expand All @@ -61,7 +61,7 @@ def mock_is_duplicate_manufacturer(self, duplicate_manufacturer_in_data: Optiona
),
)

def get_is_duplicate_manufacturer_expected_find_one_call(
def _get_is_duplicate_manufacturer_expected_find_one_call(
self, manufacturer_in: ManufacturerIn, expected_manufacturer_id: Optional[CustomObjectId]
):
"""
Expand Down Expand Up @@ -100,7 +100,7 @@ def mock_create(self, manufacturer_in_data: dict, duplicate_manufacturer_in_data
**self._manufacturer_in.model_dump(), id=inserted_manufacturer_id
)
#
self.mock_is_duplicate_manufacturer(duplicate_manufacturer_in_data)
self._mock_is_duplicate_manufacturer(duplicate_manufacturer_in_data)
# Mock `insert one` to return object for inserted manufacturer
RepositoryTestHelpers.mock_insert_one(self.manufacturers_collection, inserted_manufacturer_id)
# Mock `find_one` to return the inserted manufacturer document
Expand Down Expand Up @@ -132,7 +132,7 @@ def check_create_success(self) -> None:
# Obtain a list of expected find_one calls
expected_find_one_calls = [
# This is the check for the duplicate
self.get_is_duplicate_manufacturer_expected_find_one_call(self._manufacturer_in, None),
self._get_is_duplicate_manufacturer_expected_find_one_call(self._manufacturer_in, None),
# This is the check for the newly inserted manufacturer get
call({"_id": CustomObjectId(self._expected_manufacturer_out.id)}, session=self.mock_session),
]
Expand Down Expand Up @@ -367,7 +367,7 @@ def mock_update(

# Duplicate check
if self._stored_manufacturer_out and (self._manufacturer_in.name != self._stored_manufacturer_out.name):
self.mock_is_duplicate_manufacturer(duplicate_manufacturer_in_data)
self._mock_is_duplicate_manufacturer(duplicate_manufacturer_in_data)

# Final manufacturer after update
self._expected_manufacturer_out = ManufacturerOut(
Expand Down Expand Up @@ -410,7 +410,7 @@ def check_update_success(self) -> None:
# Duplicate check (which only runs if changing the name)
if self._stored_manufacturer_out and (self._manufacturer_in.name != self._stored_manufacturer_out.name):
expected_find_one_calls.append(
self.get_is_duplicate_manufacturer_expected_find_one_call(
self._get_is_duplicate_manufacturer_expected_find_one_call(
self._manufacturer_in, CustomObjectId(self._updated_manufacturer_id)
)
)
Expand Down
10 changes: 6 additions & 4 deletions test/unit/repositories/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setup(self, database_mock):
self.mock_session = MagicMock()
yield

def mock_is_duplicate_unit(self, duplicate_unit_in_data: Optional[dict] = None) -> None:
def _mock_is_duplicate_unit(self, duplicate_unit_in_data: Optional[dict] = None) -> None:
"""
Mocks database methods appropriately for when the `_is_duplicate_unit` repo method will be called.
Expand All @@ -58,7 +58,9 @@ def mock_is_duplicate_unit(self, duplicate_unit_in_data: Optional[dict] = None)
({**UnitIn(**duplicate_unit_in_data).model_dump(), "_id": ObjectId()} if duplicate_unit_in_data else None),
)

def get_is_duplicate_unit_expected_find_one_call(self, unit_in: UnitIn, expected_unit_id: Optional[CustomObjectId]):
def _get_is_duplicate_unit_expected_find_one_call(
self, unit_in: UnitIn, expected_unit_id: Optional[CustomObjectId]
):
"""
Returns the expected `find_one` calls that should occur when `_is_duplicate_unit` is called.
Expand Down Expand Up @@ -92,7 +94,7 @@ def mock_create(self, unit_in_data: dict, duplicate_unit_in_data: Optional[dict]

self._expected_unit_out = UnitOut(**self._unit_in.model_dump(), id=inserted_unit_id)

self.mock_is_duplicate_unit(duplicate_unit_in_data)
self._mock_is_duplicate_unit(duplicate_unit_in_data)
# Mock `insert one` to return object for inserted unit
RepositoryTestHelpers.mock_insert_one(self.units_collection, inserted_unit_id)
# Mock `find_one` to return the inserted unit document
Expand Down Expand Up @@ -122,7 +124,7 @@ def check_create_success(self) -> None:
# Obtain a list of expected find_one calls
expected_find_one_calls = [
# This is the check for the duplicate
self.get_is_duplicate_unit_expected_find_one_call(self._unit_in, None),
self._get_is_duplicate_unit_expected_find_one_call(self._unit_in, None),
# This is the check for the newly inserted unit get
call({"_id": CustomObjectId(self._expected_unit_out.id)}, session=self.mock_session),
]
Expand Down
4 changes: 2 additions & 2 deletions test/unit/repositories/test_usage_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setup(self, database_mock):
self.mock_session = MagicMock()
yield

def mock_is_duplicate_usage_status(self, duplicate_usage_status_in_data: Optional[dict] = None) -> None:
def _mock_is_duplicate_usage_status(self, duplicate_usage_status_in_data: Optional[dict] = None) -> None:
"""
Mocks database methods appropriately for when the `_is_duplicate_usage_status` repo method will be called.
Expand Down Expand Up @@ -104,7 +104,7 @@ def mock_create(self, usage_status_in_data: dict, duplicate_usage_status_in_data
**self._usage_status_in.model_dump(), id=inserted_usage_status_id
)
#
self.mock_is_duplicate_usage_status(duplicate_usage_status_in_data)
self._mock_is_duplicate_usage_status(duplicate_usage_status_in_data)
# Mock `insert one` to return object for inserted usage status
RepositoryTestHelpers.mock_insert_one(self.usage_statuses_collection, inserted_usage_status_id)
# Mock `find_one` to return the inserted usage status document
Expand Down
6 changes: 3 additions & 3 deletions test/unit/services/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def setup(
self.wrapped_utils = wrapped_utils
yield

def mock_add_property_unit_values(
def _mock_add_property_unit_values(
self, units_in_data: list[Optional[dict]], unit_value_id_dict: dict[str, str]
) -> None:
"""
Expand Down Expand Up @@ -156,7 +156,7 @@ def mock_create(
self.construct_catalogue_category_properties_in_and_post_with_ids(catalogue_category_data["properties"])
)

self.mock_add_property_unit_values(units_in_data or [], self.unit_value_id_dict)
self._mock_add_property_unit_values(units_in_data or [], self.unit_value_id_dict)

self._catalogue_category_post = CatalogueCategoryPostSchema(
**{**catalogue_category_data, "properties": property_post_schemas}
Expand Down Expand Up @@ -512,7 +512,7 @@ def mock_update(
)
catalogue_category_update_data["properties"] = property_post_schemas

self.mock_add_property_unit_values(units_in_data or [], self.unit_value_id_dict)
self._mock_add_property_unit_values(units_in_data or [], self.unit_value_id_dict)

# Updated catalogue category
self._expected_catalogue_category_out = MagicMock()
Expand Down
18 changes: 16 additions & 2 deletions test/unit/services/test_catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,18 @@ def mock_update(
),
)

# Update with the new IDs (if its possible to update them in the first place)
if (
self._stored_catalogue_category_out
and self._new_catalogue_category_out
and len(self._stored_catalogue_category_out.properties)
== len(self._new_catalogue_category_out.properties)
):
expected_stored_properties_in, _ = self.construct_properties_in_and_post_with_ids(
self._new_catalogue_category_in.properties,
stored_catalogue_item_data["properties"] if "properties" in stored_catalogue_item_data else [],
)

self._updating_manufacturer = (
"manufacturer_id" in catalogue_item_update_data
and catalogue_item_update_data["manufacturer_id"] != self._stored_catalogue_item.manufacturer_id
Expand Down Expand Up @@ -632,9 +644,8 @@ def mock_update(
)

# When properties are given need to add any property `id`s and ensure the expected data inserts them as well
expected_properties_in = []
expected_properties_in = expected_stored_properties_in
if self._updating_properties:

# When not moving to a different catalogue category the existing catalogue category will still need to be
# mocked
if not self._moving_catalogue_item:
Expand Down Expand Up @@ -750,6 +761,9 @@ def check_update_success(self) -> None:
else:
self.wrapped_utils.process_properties.assert_not_called()

self.mock_catalogue_item_repository.update.assert_called_once_with(
self._updated_catalogue_item_id, self._expected_catalogue_item_in
)
assert self._updated_catalogue_item == self._expected_catalogue_item_out

def check_update_failed_with_exception(self, message: str) -> None:
Expand Down

0 comments on commit 5847894

Please sign in to comment.