Skip to content

Commit

Permalink
Refactor catalogue category e2e get tests #314
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Jul 3, 2024
1 parent 13c169a commit 9d01ea2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
121 changes: 60 additions & 61 deletions test/e2e/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from test.e2e.mock_schemas import (
CATALOGUE_CATEGORY_POST_ALLOWED_VALUES,
CATALOGUE_CATEGORY_POST_ALLOWED_VALUES_EXPECTED,
CREATED_MODIFIED_VALUES_EXPECTED)
CREATED_MODIFIED_VALUES_EXPECTED,
)
from test.e2e.test_unit import UNIT_POST_A
from test.mock_data import (
CATALOGUE_CATEGORY_DATA_LEAF_WITH_PROPERTIES_NO_PARENT_MM,
Expand All @@ -18,7 +19,8 @@
CATALOGUE_CATEGORY_POST_DATA_LEAF_REQUIRED_VALUES_ONLY,
CATALOGUE_CATEGORY_POST_DATA_NON_LEAF_REQUIRED_VALUES_ONLY,
CATALOGUE_CATEGORY_PROPERTY_DATA_BOOLEAN_MANDATORY_WITHOUT_UNIT,
UNIT_POST_DATA_MM)
UNIT_POST_DATA_MM,
)
from typing import Optional
from unittest.mock import ANY

Expand All @@ -27,8 +29,7 @@
from fastapi.testclient import TestClient
from httpx import Response

from inventory_management_system_api.core.consts import \
BREADCRUMBS_TRAIL_MAX_LENGTH
from inventory_management_system_api.core.consts import BREADCRUMBS_TRAIL_MAX_LENGTH


class CreateDSL:
Expand All @@ -47,7 +48,7 @@ def setup(self, test_client):
self.test_client = test_client
self.unit_value_id_dict = {}

def post_unit(self, unit_post_data: dict) -> Optional[str]:
def post_unit(self, unit_post_data: dict):
"""Posts a unit with the given data and stores the value and id in a dictionary for lookup later
:param unit_post_data: Dictionary containing the unit data that should be posted
Expand All @@ -74,13 +75,17 @@ def post_catalogue_category(self, catalogue_category_data: dict) -> Optional[str

# Replace any unit values with unit ids
if "properties" in catalogue_category_data and catalogue_category_data["properties"]:
new_properties = []
for prop in catalogue_category_data["properties"]:
new_property = {**prop}
if "unit" in prop:
if prop["unit"] is not None:
prop["unit_id"] = self.unit_value_id_dict[prop["unit"]]
new_property["unit_id"] = self.unit_value_id_dict[prop["unit"]]
else:
prop["unit_id"] = None
del prop["unit"]
new_property["unit_id"] = None
del new_property["unit"]
new_properties.append(new_property)
catalogue_category_data = {**catalogue_category_data, "properties": new_properties}

self._post_response = self.test_client.post("/v1/catalogue-categories", json=catalogue_category_data)

Expand Down Expand Up @@ -351,6 +356,53 @@ def test_create_leaf_with_boolean_property_with_allowed_values_list(self):
)


class GetDSL(CreateDSL):
"""Base class for get tests"""

_get_response: Response

def get_catalogue_category(self, catalogue_category_id: str):
"""Gets a system with the given id"""

self._get_response = self.test_client.get(f"/v1/catalogue-categories/{catalogue_category_id}")

def check_get_catalogue_category_success(self, expected_catalogue_category_get_data: dict):
"""Checks that a prior call to 'get_catalogue_category' gave a successful response with the expected data returned"""

assert self._get_response.status_code == 200
assert self._get_response.json() == expected_catalogue_category_get_data

def check_get_catalogue_category_failed_with_message(self, status_code: int, detail: str):
"""Checks that a prior call to 'get_catalogue_category' gave a failed response with the expected code and error message"""

assert self._get_response.status_code == status_code
assert self._get_response.json()["detail"] == detail


class TestGet(GetDSL):
"""Tests for getting a catalogue category"""

def test_get(self):
"""Test getting a catalogue category"""

self.post_unit(UNIT_POST_DATA_MM)
catalogue_category_id = self.post_catalogue_category(CATALOGUE_CATEGORY_DATA_LEAF_WITH_PROPERTIES_NO_PARENT_MM)
self.get_catalogue_category(catalogue_category_id)
self.check_get_catalogue_category_success(CATALOGUE_CATEGORY_GET_DATA_LEAF_WITH_PROPERTIES_NO_PARENT_MM)

def test_get_with_non_existent_id(self):
"""Test getting a catalogue category with a non-existent id"""

self.get_catalogue_category(str(ObjectId()))
self.check_get_catalogue_category_failed_with_message(404, "Catalogue category not found")

def test_get_with_invalid_id(self):
"""Test getting a catalogue category with an invalid id"""

self.get_catalogue_category("invalid-id")
self.check_get_catalogue_category_failed_with_message(404, "Catalogue category not found")


# CATALOGUE_CATEGORY_POST_A = {"name": "Category A", "is_leaf": False}
# CATALOGUE_CATEGORY_POST_A_EXPECTED = {
# **CATALOGUE_CATEGORY_POST_A,
Expand Down Expand Up @@ -599,59 +651,6 @@ def test_create_leaf_with_boolean_property_with_allowed_values_list(self):
# assert response.json()["detail"] == "Catalogue category has child elements and cannot be deleted"


# def test_get_catalogue_category(test_client):
# """
# Test getting a catalogue category.
# """
# # Parent
# response = test_client.post("/v1/catalogue-categories", json=CATALOGUE_CATEGORY_POST_A)
# parent_catalogue_category = response.json()

# units, _ = _post_units(test_client)

# # Child
# response = test_client.post(
# "/v1/catalogue-categories",
# json={
# **CATALOGUE_CATEGORY_POST_B,
# "properties": replace_unit_values_with_ids_in_properties(CATALOGUE_CATEGORY_POST_B["properties"], units),
# "parent_id": parent_catalogue_category["id"],
# },
# )

# response = test_client.get(f"/v1/catalogue-categories/{response.json()['id']}")
# assert response.status_code == 200
# catalogue_category = response.json()
# assert catalogue_category == {
# **CATALOGUE_CATEGORY_POST_B_EXPECTED,
# "parent_id": parent_catalogue_category["id"],
# "properties": add_ids_to_properties(
# catalogue_category["properties"],
# CATALOGUE_CATEGORY_POST_B_EXPECTED["properties"],
# ),
# }


# def test_get_catalogue_category_with_invalid_id(test_client):
# """
# Test getting a catalogue category with an invalid ID.
# """
# response = test_client.get("/v1/catalogue-categories/invalid")

# assert response.status_code == 404
# assert response.json()["detail"] == "Catalogue category not found"


# def test_get_catalogue_category_with_non_existent_id(test_client):
# """
# Test getting a catalogue category with a non-existent ID.
# """
# response = test_client.get(f"/v1/catalogue-categories/{str(ObjectId())}")

# assert response.status_code == 404
# assert response.json()["detail"] == "Catalogue category not found"


# def test_get_catalogue_categories(test_client):
# """
# Test getting catalogue categories.
Expand Down
1 change: 1 addition & 0 deletions test/mock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
**CATALOGUE_CATEGORY_PROPERTY_DATA_STRING_NON_MANDATORY_WITH_ALLOWED_VALUES_LIST,
"id": ANY,
"unit_id": ANY,
"unit": None,
}

# Put _MM at end to signify what units this data would require
Expand Down

0 comments on commit 9d01ea2

Please sign in to comment.