Skip to content

Commit

Permalink
Clear settings collection, use more than one usage status in tests an…
Browse files Browse the repository at this point in the history
…d add an e2e test #431
  • Loading branch information
joelvdavies committed Nov 21, 2024
1 parent 8216524 commit f737d09
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 34 deletions.
1 change: 1 addition & 0 deletions test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test_catalogue_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test_manufacturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 40 additions & 16 deletions test/e2e/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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"
Expand All @@ -98,35 +121,36 @@ 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(),
],
}
)

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")
2 changes: 1 addition & 1 deletion test/e2e/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
25 changes: 20 additions & 5 deletions test/mock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
Expand Down Expand Up @@ -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]
}
8 changes: 4 additions & 4 deletions test/unit/repositories/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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()

Expand Down
14 changes: 10 additions & 4 deletions test/unit/services/test_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
)

0 comments on commit f737d09

Please sign in to comment.