diff --git a/test/e2e/conftest.py b/test/e2e/conftest.py index 0f162998..91f79d4c 100644 --- a/test/e2e/conftest.py +++ b/test/e2e/conftest.py @@ -38,6 +38,7 @@ def fixture_cleanup_database_collections(): database.systems.delete_many({}) database.units.delete_many({}) database.usage_statuses.delete_many({}) + database.settings.delete_many({}) def replace_unit_values_with_ids_in_properties(properties_without_ids: list[dict], units: Optional[list]) -> list[dict]: diff --git a/test/e2e/test_catalogue_category.py b/test/e2e/test_catalogue_category.py index bcb1c2d3..f14dde58 100644 --- a/test/e2e/test_catalogue_category.py +++ b/test/e2e/test_catalogue_category.py @@ -665,7 +665,7 @@ class UpdateDSL(ListDSL): def patch_catalogue_category(self, catalogue_category_id: str, catalogue_category_update_data: dict) -> None: """ - Updates a catalogue category with the given ID. + Patches a catalogue category with the given ID. :param catalogue_category_id: ID of the catalogue category to patch. :param catalogue_category_update_data: Dictionary containing the basic patch data as would be required for a diff --git a/test/e2e/test_catalogue_item.py b/test/e2e/test_catalogue_item.py index 8cfb354c..45996bb7 100644 --- a/test/e2e/test_catalogue_item.py +++ b/test/e2e/test_catalogue_item.py @@ -775,7 +775,7 @@ class UpdateDSL(ListDSL): def patch_catalogue_item(self, catalogue_item_id: str, catalogue_item_update_data: dict) -> None: """ - Updates a catalogue item with the given ID. + Patches a catalogue item with the given ID. :param catalogue_item_id: ID of the catalogue item to patch. :param catalogue_item_update_data: Dictionary containing the basic patch data as would be required for a diff --git a/test/e2e/test_item.py b/test/e2e/test_item.py index 750f1eb0..ab8e3b44 100644 --- a/test/e2e/test_item.py +++ b/test/e2e/test_item.py @@ -788,7 +788,7 @@ class UpdateDSL(ListDSL): def patch_item(self, item_id: str, item_update_data: dict) -> None: """ - Updates an item with the given ID. + Patches an item with the given ID. :param item_id: ID of the item to patch. :param item_update_data: Dictionary containing the basic patch data as would be required for a `ItemPatchSchema` diff --git a/test/e2e/test_manufacturer.py b/test/e2e/test_manufacturer.py index c505aacb..7b7aba4e 100644 --- a/test/e2e/test_manufacturer.py +++ b/test/e2e/test_manufacturer.py @@ -184,7 +184,7 @@ class UpdateDSL(ListDSL): def patch_manufacturer(self, manufacturer_id: str, manufacturer_patch_data: dict) -> None: """ - Updates a manufacturer with the given ID. + Patches a manufacturer with the given ID. :param manufacturer_id: ID of the manufacturer to be updated. :param manufacturer_patch_data: Dictionary containing the manufacturer patch data as would be required for a diff --git a/test/e2e/test_setting.py b/test/e2e/test_setting.py index 30e7fa4b..66f4ea0e 100644 --- a/test/e2e/test_setting.py +++ b/test/e2e/test_setting.py @@ -6,8 +6,11 @@ from test.e2e.test_usage_status import CreateDSL as UsageStatusCreateDSL from test.mock_data import ( SETTING_SPARES_DEFINITION_DATA_NEW, + SETTING_SPARES_DEFINITION_DATA_NEW_USED, SETTING_SPARES_DEFINITION_GET_DATA_NEW, + SETTING_SPARES_DEFINITION_GET_DATA_NEW_USED, USAGE_STATUS_POST_DATA_NEW, + USAGE_STATUS_POST_DATA_USED, ) from bson import ObjectId @@ -21,7 +24,7 @@ class SetSparesDefinitionDSL(UsageStatusCreateDSL): def put_spares_definition(self, spares_definition_data: dict) -> None: """ - Updates a system with the given ID. + Puts a spares definition. :param spares_definition_data: Dictionary containing the patch data as would be required for a `SparesDefinitionPutSchema` but with any `id`'s replaced by the `value` as the @@ -38,6 +41,19 @@ def put_spares_definition(self, spares_definition_data: dict) -> None: "/v1/settings/spares_definition", json=spares_definition_put_data ) + def put_spares_definition_and_post_prerequisites(self, spares_definition_data: dict) -> None: + """ + Utility method that puts a spares definition having first posted any prerequisite usage statuses. + + :param spares_definition_data: Dictionary containing the patch data as would be required for a + `SparesDefinitionPutSchema` but with any `id`'s replaced by the `value` as the + IDs will be added automatically. + """ + + self.post_usage_status(USAGE_STATUS_POST_DATA_NEW) + self.post_usage_status(USAGE_STATUS_POST_DATA_USED) + self.put_spares_definition(spares_definition_data) + def check_put_spares_definition_success(self, expected_spares_definition_get_data: dict) -> None: """ Checks that a prior call to `put_spares_definition` gave a successful response with the expected data returned. @@ -78,18 +94,25 @@ class TestSetSparesDefinition(SetSparesDefinitionDSL): """Tests for setting the spares definition.""" def test_set_spares_definition(self): - """Test setting the spares definition.""" + """Test setting the spares definition (for the first time).""" - self.post_usage_status(USAGE_STATUS_POST_DATA_NEW) - self.put_spares_definition(SETTING_SPARES_DEFINITION_DATA_NEW) + self.put_spares_definition_and_post_prerequisites(SETTING_SPARES_DEFINITION_DATA_NEW_USED) + + self.check_put_spares_definition_success(SETTING_SPARES_DEFINITION_GET_DATA_NEW_USED) + + def test_set_spares_definition_overwrite(self): + """Test setting the spares definition (when it has already been assigned before).""" + + self.put_spares_definition_and_post_prerequisites(SETTING_SPARES_DEFINITION_DATA_NEW_USED) + self.check_put_spares_definition_success(SETTING_SPARES_DEFINITION_GET_DATA_NEW_USED) + self.put_spares_definition_and_post_prerequisites(SETTING_SPARES_DEFINITION_DATA_NEW) self.check_put_spares_definition_success(SETTING_SPARES_DEFINITION_GET_DATA_NEW) def test_set_spares_definition_with_no_usage_statuses(self): """Test setting the spares definition with no usage statuses given.""" - self.post_usage_status(USAGE_STATUS_POST_DATA_NEW) - self.put_spares_definition({**SETTING_SPARES_DEFINITION_DATA_NEW, "usage_statuses": []}) + self.put_spares_definition({**SETTING_SPARES_DEFINITION_DATA_NEW_USED, "usage_statuses": []}) self.check_put_spares_definition_failed_with_validation_message( 422, "List should have at least 1 item after validation, not 0" @@ -98,13 +121,12 @@ def test_set_spares_definition_with_no_usage_statuses(self): def test_set_spares_definition_with_duplicate_usage_statuses(self): """Test setting the spares definition with no usage statuses given.""" - self.post_usage_status(USAGE_STATUS_POST_DATA_NEW) - self.put_spares_definition( + self.put_spares_definition_and_post_prerequisites( { - **SETTING_SPARES_DEFINITION_DATA_NEW, + **SETTING_SPARES_DEFINITION_DATA_NEW_USED, "usage_statuses": [ - *SETTING_SPARES_DEFINITION_DATA_NEW["usage_statuses"], - SETTING_SPARES_DEFINITION_DATA_NEW["usage_statuses"][0].copy(), + *SETTING_SPARES_DEFINITION_DATA_NEW_USED["usage_statuses"], + SETTING_SPARES_DEFINITION_DATA_NEW_USED["usage_statuses"][0].copy(), ], } ) @@ -112,21 +134,23 @@ def test_set_spares_definition_with_duplicate_usage_statuses(self): self.check_put_spares_definition_failed_with_validation_message( 422, "Value error, usage_statuses contains a duplicate ID: " - f"{self.usage_status_value_id_dict[SETTING_SPARES_DEFINITION_DATA_NEW['usage_statuses'][0]['value']]}", + f"{self.usage_status_value_id_dict[SETTING_SPARES_DEFINITION_DATA_NEW_USED['usage_statuses'][0]['value']]}", ) def test_set_spares_definition_with_non_existent_usage_status_id(self): """Test setting the spares definition when there is a non-existent usage status ID.""" - self.add_usage_status_value_and_id(USAGE_STATUS_POST_DATA_NEW["value"], str(ObjectId())) - self.put_spares_definition(SETTING_SPARES_DEFINITION_DATA_NEW) + self.post_usage_status(USAGE_STATUS_POST_DATA_NEW) + self.add_usage_status_value_and_id(USAGE_STATUS_POST_DATA_USED["value"], str(ObjectId())) + 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") def test_set_spares_definition_with_invalid_usage_status_id(self): """Test setting the spares definition when there is an invalid usage status ID.""" - self.add_usage_status_value_and_id(USAGE_STATUS_POST_DATA_NEW["value"], "invalid-id") - self.put_spares_definition(SETTING_SPARES_DEFINITION_DATA_NEW) + self.post_usage_status(USAGE_STATUS_POST_DATA_NEW) + self.add_usage_status_value_and_id(USAGE_STATUS_POST_DATA_USED["value"], "invalid-id") + 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") diff --git a/test/e2e/test_system.py b/test/e2e/test_system.py index 4c49a86c..78a92db6 100644 --- a/test/e2e/test_system.py +++ b/test/e2e/test_system.py @@ -424,7 +424,7 @@ class UpdateDSL(ListDSL): def patch_system(self, system_id: str, system_patch_data: dict) -> None: """ - Updates a system with the given ID. + Patches a system with the given ID. :param system_id: ID of the system to patch. :param system_patch_data: Dictionary containing the patch data as would be required for a `SystemPatchSchema`. diff --git a/test/mock_data.py b/test/mock_data.py index 84c68309..02dea5ea 100644 --- a/test/mock_data.py +++ b/test/mock_data.py @@ -61,7 +61,7 @@ } USAGE_STATUS_OUT_DATA_NEW = UsageStatusOut( - **UsageStatusIn(**USAGE_STATUS_IN_DATA_NEW).model_dump(), _id="673f07237da637045aaf5747" + **UsageStatusIn(**USAGE_STATUS_IN_DATA_NEW).model_dump(), _id=str(ObjectId()) ).model_dump() USAGE_STATUS_GET_DATA_NEW = { @@ -84,6 +84,10 @@ "code": "used", } +USAGE_STATUS_OUT_DATA_USED = UsageStatusOut( + **UsageStatusIn(**USAGE_STATUS_IN_DATA_USED).model_dump(), _id=str(ObjectId()) +).model_dump() + USAGE_STATUS_GET_DATA_USED = { **USAGE_STATUS_POST_DATA_USED, **CREATED_MODIFIED_GET_DATA_EXPECTED, @@ -794,11 +798,22 @@ # Spares definition, New SETTING_SPARES_DEFINITION_DATA_NEW = {"usage_statuses": [{"value": USAGE_STATUS_POST_DATA_NEW["value"]}]} -SETTING_SPARES_DEFINITION_IN_DATA_NEW = {"usage_statuses": [{"id": USAGE_STATUS_OUT_DATA_NEW["id"]}]} +SETTING_SPARES_DEFINITION_GET_DATA_NEW = {"usage_statuses": [USAGE_STATUS_GET_DATA_NEW]} + +# Spares definition, New or Used +SETTING_SPARES_DEFINITION_DATA_NEW_USED = { + "usage_statuses": [{"value": USAGE_STATUS_POST_DATA_NEW["value"]}, {"value": USAGE_STATUS_POST_DATA_USED["value"]}] +} -SETTING_SPARES_DEFINITION_OUT_DATA_NEW = { +SETTING_SPARES_DEFINITION_IN_DATA_NEW_USED = { + "usage_statuses": [{"id": USAGE_STATUS_OUT_DATA_NEW["id"]}, {"id": USAGE_STATUS_OUT_DATA_USED["id"]}] +} + +SETTING_SPARES_DEFINITION_OUT_DATA_NEW_USED = { "_id": SparesDefinitionOut.SETTING_ID, - "usage_statuses": [USAGE_STATUS_OUT_DATA_NEW], + "usage_statuses": [USAGE_STATUS_OUT_DATA_NEW, USAGE_STATUS_OUT_DATA_USED], } -SETTING_SPARES_DEFINITION_GET_DATA_NEW = {"usage_statuses": [USAGE_STATUS_GET_DATA_NEW]} +SETTING_SPARES_DEFINITION_GET_DATA_NEW_USED = { + "usage_statuses": [USAGE_STATUS_GET_DATA_NEW, USAGE_STATUS_GET_DATA_USED] +} diff --git a/test/unit/repositories/test_setting.py b/test/unit/repositories/test_setting.py index 38ff814c..3ae4c683 100644 --- a/test/unit/repositories/test_setting.py +++ b/test/unit/repositories/test_setting.py @@ -2,7 +2,7 @@ Unit tests for the `SettingRepo` repository. """ -from test.mock_data import SETTING_SPARES_DEFINITION_IN_DATA_NEW, SETTING_SPARES_DEFINITION_OUT_DATA_NEW +from test.mock_data import SETTING_SPARES_DEFINITION_IN_DATA_NEW_USED, SETTING_SPARES_DEFINITION_OUT_DATA_NEW_USED from test.unit.repositories.conftest import RepositoryTestHelpers from typing import ClassVar, Optional, Type from unittest.mock import MagicMock, Mock @@ -124,8 +124,8 @@ def test_upsert_spares_definition(self): """Test upserting the spares definition setting.""" self.mock_upsert( - SETTING_SPARES_DEFINITION_IN_DATA_NEW, - SETTING_SPARES_DEFINITION_OUT_DATA_NEW, + SETTING_SPARES_DEFINITION_IN_DATA_NEW_USED, + SETTING_SPARES_DEFINITION_OUT_DATA_NEW_USED, SparesDefinitionIn, SparesDefinitionOut, ) @@ -209,7 +209,7 @@ def test_get_non_existent(self): def test_get_spares_definition(self): """Test getting the spares definition setting.""" - self.mock_get(SparesDefinitionOut, SETTING_SPARES_DEFINITION_OUT_DATA_NEW) + self.mock_get(SparesDefinitionOut, SETTING_SPARES_DEFINITION_OUT_DATA_NEW_USED) self.call_get(SparesDefinitionOut) self.check_get_success() diff --git a/test/unit/services/test_setting.py b/test/unit/services/test_setting.py index b55cd2f7..c10b2e7a 100644 --- a/test/unit/services/test_setting.py +++ b/test/unit/services/test_setting.py @@ -3,7 +3,11 @@ """ from copy import deepcopy -from test.mock_data import SETTING_SPARES_DEFINITION_DATA_NEW, USAGE_STATUS_OUT_DATA_NEW +from test.mock_data import ( + SETTING_SPARES_DEFINITION_DATA_NEW_USED, + USAGE_STATUS_OUT_DATA_NEW, + USAGE_STATUS_OUT_DATA_USED, +) from test.unit.services.conftest import ServiceTestHelpers from typing import Optional from unittest.mock import MagicMock, Mock, call @@ -133,17 +137,19 @@ class TestSetSpareDefinition(SetSparesDefinitionDSL): def test_set_spare_definition(self): """Test setting the spares definition.""" - self.mock_set_spares_definition(SETTING_SPARES_DEFINITION_DATA_NEW, [USAGE_STATUS_OUT_DATA_NEW]) + self.mock_set_spares_definition( + SETTING_SPARES_DEFINITION_DATA_NEW_USED, [USAGE_STATUS_OUT_DATA_NEW, USAGE_STATUS_OUT_DATA_USED] + ) self.call_set_spares_definition() self.check_set_spares_definition_success() def test_set_spare_definition_with_non_existent_usage_status_id(self): """Test setting the spares definition with a non-existent usage status ID.""" - self.mock_set_spares_definition(SETTING_SPARES_DEFINITION_DATA_NEW, [None]) + self.mock_set_spares_definition(SETTING_SPARES_DEFINITION_DATA_NEW_USED, [USAGE_STATUS_OUT_DATA_NEW, None]) self.call_set_spares_definition_expecting_error(MissingRecordError) self.check_set_spares_definition_failed_with_exception( # Pydantic Field confuses pylint # pylint: disable=unsubscriptable-object - f"No usage status found with ID: {self._spares_definition_put.usage_statuses[0].id}" + f"No usage status found with ID: {self._spares_definition_put.usage_statuses[1].id}" )