From 3e8f3cde172a7940ee059d47d7a908f6595bd99b Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Fri, 22 Nov 2024 11:38:28 +0000 Subject: [PATCH 1/5] Add spares definition get endpoint #414 --- .../routers/v1/setting.py | 13 +++++++++++++ inventory_management_system_api/services/setting.py | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/inventory_management_system_api/routers/v1/setting.py b/inventory_management_system_api/routers/v1/setting.py index fd57e791..8a7e277e 100644 --- a/inventory_management_system_api/routers/v1/setting.py +++ b/inventory_management_system_api/routers/v1/setting.py @@ -37,3 +37,16 @@ def set_spares_definition( message = "A specified usage status does not exist" logger.exception(message) raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=message) from exc + + +@router.get( + path="/spares_definition", summary="Get the definition of a spare", response_description="Spares definition" +) +def get_spares_definition(setting_service: SettingServiceDep) -> SparesDefinitionSchema: + # pylint: disable=missing-function-docstring + logger.info("Getting spares definition") + + spares_definition = setting_service.get_spares_definition() + if not spares_definition: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Spares definition not found.") + return spares_definition diff --git a/inventory_management_system_api/services/setting.py b/inventory_management_system_api/services/setting.py index bba91a2f..412fc36e 100644 --- a/inventory_management_system_api/services/setting.py +++ b/inventory_management_system_api/services/setting.py @@ -52,3 +52,12 @@ def set_spares_definition(self, spares_definition: SparesDefinitionPutSchema) -> return self._setting_repository.upsert( SparesDefinitionIn(**spares_definition.model_dump()), SparesDefinitionOut ) + + def get_spares_definition(self) -> SparesDefinitionOut: + """ + Retrieves the spares definition. + + :return: Retrieved spares definition or `None` if not found. + """ + + return self._setting_repository.get(SparesDefinitionOut) From 68087b5f44be4312eeb0ed9a4033223b568b7e32 Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Fri, 22 Nov 2024 11:49:07 +0000 Subject: [PATCH 2/5] Add a spares definition get SystemService unit test #414 --- test/unit/services/test_setting.py | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/unit/services/test_setting.py b/test/unit/services/test_setting.py index c10b2e7a..4379d7ee 100644 --- a/test/unit/services/test_setting.py +++ b/test/unit/services/test_setting.py @@ -153,3 +153,39 @@ def test_set_spare_definition_with_non_existent_usage_status_id(self): # pylint: disable=unsubscriptable-object f"No usage status found with ID: {self._spares_definition_put.usage_statuses[1].id}" ) + + +class GetSparesDefinitionDSL(SettingServiceDSL): + """Base class for `get_spares_definition` tests.""" + + _expected_spares_definition: MagicMock + _obtained_spares_definition: MagicMock + + def mock_get_spares_definition(self) -> None: + """Mocks repo methods appropriately to test the `get_spares_definition` service method.""" + + # Simply a return currently, so no need to use actual data + self._expected_spares_definition = MagicMock() + ServiceTestHelpers.mock_get(self.mock_setting_repository, self._expected_spares_definition) + + def call_get_spares_definition(self) -> None: + """Calls the `SettingService` `get_spares_definition` method.""" + + self._obtained_spares_definition = self.setting_service.get_spares_definition() + + def check_get_spares_definition_success(self) -> None: + """Checks that a prior call to `call_get_spares_definition` worked as expected.""" + + self.mock_setting_repository.get.assert_called_once_with(SparesDefinitionOut) + assert self._obtained_spares_definition == self._expected_spares_definition + + +class TestGetSparesDefinition(GetSparesDefinitionDSL): + """Tests for getting the spares definition.""" + + def test_get(self): + """Test getting the spares definition.""" + + self.mock_get_spares_definition() + self.call_get_spares_definition() + self.check_get_spares_definition_success() From afa6b002aa53253b764b104ce1ad78e3005186d7 Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Fri, 22 Nov 2024 12:02:50 +0000 Subject: [PATCH 3/5] Add e2e test for spares definition get #414 --- test/e2e/test_setting.py | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/e2e/test_setting.py b/test/e2e/test_setting.py index 66f4ea0e..5cecfc75 100644 --- a/test/e2e/test_setting.py +++ b/test/e2e/test_setting.py @@ -154,3 +154,54 @@ def test_set_spares_definition_with_invalid_usage_status_id(self): self.put_spares_definition(SETTING_SPARES_DEFINITION_DATA_NEW_USED) self.check_put_spares_definition_failed_with_detail(422, "A specified usage status does not exist") + + +class GetSparesDefinitionDSL(SetSparesDefinitionDSL): + """Base class for get spares definition tests.""" + + _get_response_spares_definition: Response + + def get_spares_definition(self): + """Gets the spares definition.""" + + self._get_response_spares_definition = self.test_client.get("/v1/settings/spares_definition") + + def check_get_spares_definition_success(self, expected_spares_definition_get_data: dict): + """ + Checks that a prior call to `get_spares_definition` gave a successful response with the expected data returned. + + :param expected_spares_definition_get_data: Dictionary containing the expected system data returned as would be + required for a `SparesDefinitionSchema`. + """ + + assert self._get_response_spares_definition.status_code == 200 + assert self._get_response_spares_definition.json() == expected_spares_definition_get_data + + def check_get_spares_definition_failed_with_detail(self, status_code: int, detail: str): + """ + Checks that a prior call to `get_spares_definition` gave a failed response with the expected code and error + message. + + :param status_code: Expected status code of the response. + :param detail: Expected detail given in the response. + """ + + assert self._get_response_spares_definition.status_code == status_code + assert self._get_response_spares_definition.json()["detail"] == detail + + +class TestGetSparesDefinition(GetSparesDefinitionDSL): + """Tests for getting the spares definition.""" + + def test_get_spares_definition(self): + """Test getting the spares definition.""" + + self.put_spares_definition_and_post_prerequisites(SETTING_SPARES_DEFINITION_DATA_NEW_USED) + self.get_spares_definition() + self.check_get_spares_definition_success(SETTING_SPARES_DEFINITION_GET_DATA_NEW_USED) + + def test_get_spares_definition_when_non_existent(self): + """Test getting the spares definition when it is non-existent.""" + + self.get_spares_definition() + self.check_get_spares_definition_failed_with_detail(404, "Spares definition not found.") From 2b3df718da25097f328de93fd5b5fdf0e6a78d5e Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Thu, 5 Dec 2024 12:39:27 +0000 Subject: [PATCH 4/5] Fix tests after merge #414 --- test/e2e/test_setting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/test_setting.py b/test/e2e/test_setting.py index f294af4f..0a28351b 100644 --- a/test/e2e/test_setting.py +++ b/test/e2e/test_setting.py @@ -156,7 +156,7 @@ def test_update_spares_definition_with_invalid_usage_status_id(self): self.check_put_spares_definition_failed_with_detail(422, "A specified usage status does not exist") -class GetSparesDefinitionDSL(SetSparesDefinitionDSL): +class GetSparesDefinitionDSL(UpdateSparesDefinitionDSL): """Base class for get spares definition tests.""" _get_response_spares_definition: Response From edd10b1beb2763cc2ba6b85d7a236000771a9e55 Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Thu, 5 Dec 2024 13:10:14 +0000 Subject: [PATCH 5/5] Fix wrong return type in setting router #414 --- inventory_management_system_api/routers/v1/setting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inventory_management_system_api/routers/v1/setting.py b/inventory_management_system_api/routers/v1/setting.py index 52d80373..22698fb1 100644 --- a/inventory_management_system_api/routers/v1/setting.py +++ b/inventory_management_system_api/routers/v1/setting.py @@ -49,4 +49,4 @@ def get_spares_definition(setting_service: SettingServiceDep) -> SparesDefinitio spares_definition = setting_service.get_spares_definition() if not spares_definition: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Spares definition not found.") - return spares_definition + return SparesDefinitionSchema(**spares_definition.model_dump())