Skip to content

Commit

Permalink
Update SettingService unit tests #417
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Dec 5, 2024
1 parent 4237f52 commit 1559a05
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions test/unit/services/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from test.unit.services.conftest import ServiceTestHelpers
from typing import Optional
from unittest.mock import MagicMock, Mock, call
from unittest.mock import MagicMock, Mock, call, patch

import pytest
from bson import ObjectId
Expand All @@ -25,24 +25,48 @@ class SettingServiceDSL:
"""Base class for `SettingService` unit tests."""

mock_setting_repository: Mock
mock_catalogue_item_repository: Mock
mock_item_repository: Mock
mock_usage_status_repository: Mock
mock_utils: Mock
mock_start_session_transaction: Mock
setting_service: SettingService

# pylint:disable=too-many-arguments
# pylint:disable=too-many-positional-arguments
@pytest.fixture(autouse=True)
def setup(self, setting_repository_mock, usage_status_repository_mock, setting_service):
def setup(
self,
setting_repository_mock,
catalogue_item_repository_mock,
item_repository_mock,
usage_status_repository_mock,
setting_service,
):
"""Setup fixtures"""

self.mock_setting_repository = setting_repository_mock
self.mock_catalogue_item_repository = catalogue_item_repository_mock
self.mock_item_repository = item_repository_mock
self.mock_usage_status_repository = usage_status_repository_mock
self.setting_service = setting_service

with patch("inventory_management_system_api.services.setting.utils") as mocked_utils:
with patch(
"inventory_management_system_api.services.setting.start_session_transaction"
) as mocked_start_session_transaction:
self.mock_utils = mocked_utils
self.mock_start_session_transaction = mocked_start_session_transaction
yield


class UpdateSparesDefinitionDSL(SettingServiceDSL):
"""Base class for `update_spares_definition` tests."""

_spares_definition_put: SparesDefinitionPutSchema
_expected_spares_definition_in: SparesDefinitionIn
_expected_spares_definition_out: MagicMock
_expected_catalogue_item_ids: list[ObjectId]
_updated_spares_definition: MagicMock
_update_spares_definition_exception: pytest.ExceptionInfo

Expand Down Expand Up @@ -83,6 +107,10 @@ def mock_update_spares_definition(
self._expected_spares_definition_out = MagicMock()
ServiceTestHelpers.mock_upsert(self.mock_setting_repository, self._expected_spares_definition_out)

# Expected list of catalogue item ids that need to be updated (actual values don't matter here)
self._expected_catalogue_item_ids = [ObjectId(), ObjectId()]
self.mock_catalogue_item_repository.list_ids.return_value = self._expected_catalogue_item_ids

def call_update_spares_definition(self) -> None:
"""Calls the `SettingService` `update_spares_definition` method with the appropriate data from a prior call to
`mock_update_spares_definition`."""
Expand Down Expand Up @@ -111,9 +139,41 @@ def check_update_spares_definition_success(self) -> None:
[call(usage_status.id) for usage_status in self._spares_definition_put.usage_statuses]
)

# Ensure started a transaction
self.mock_start_session_transaction.assert_called_once_with("updating spares definition")
expected_session = self.mock_start_session_transaction.return_value.__enter__.return_value

# Ensure upserted with expected data
self.mock_setting_repository.upsert.assert_called_once_with(
self._expected_spares_definition_in, SparesDefinitionOut
self._expected_spares_definition_in, SparesDefinitionOut, session=expected_session
)

# Ensure obtained list of all catalogue item ids and used them to recalculate the number of spares
self.mock_catalogue_item_repository.list_ids.assert_called_once_with()
self.mock_utils.get_usage_status_ids_from_spares_definition.assert_called_once_with(
self._expected_spares_definition_out
)

expected_prepare_for_number_of_spares_recalculation_calls = []
expected_perform_number_of_spares_recalculation_calls = []
for catalogue_item_id in self._expected_catalogue_item_ids:
expected_prepare_for_number_of_spares_recalculation_calls.append(
call(catalogue_item_id, self.mock_catalogue_item_repository, expected_session)
)
expected_perform_number_of_spares_recalculation_calls.append(
call(
catalogue_item_id,
self.mock_utils.get_usage_status_ids_from_spares_definition.return_value,
self.mock_catalogue_item_repository,
self.mock_item_repository,
expected_session,
)
)
self.mock_utils.prepare_for_number_of_spares_recalculation.assert_has_calls(
expected_prepare_for_number_of_spares_recalculation_calls
)
self.mock_utils.perform_number_of_spares_recalculation.assert_has_calls(
expected_perform_number_of_spares_recalculation_calls
)

assert self._updated_spares_definition == self._expected_spares_definition_out
Expand Down

0 comments on commit 1559a05

Please sign in to comment.