Skip to content

Commit

Permalink
Use RepositoryTestHelpers directly instead of creating instance #90
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Jun 27, 2024
1 parent 249e5ce commit 222d588
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
1 change: 0 additions & 1 deletion test/e2e/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
End-to-End tests for the system router
"""

from datetime import datetime
from test.conftest import add_ids_to_properties
from test.e2e.conftest import E2ETestHelpers, replace_unit_values_with_ids_in_properties
from test.e2e.mock_schemas import USAGE_STATUS_POST_B
Expand Down
2 changes: 2 additions & 0 deletions test/unit/repositories/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ def mock_update_many(collection_mock: Mock) -> None:
collection_mock.update_many.return_value = update_many_result_mock


# pylint:disable=fixme
# TODO: Remove this once tests refactored - should be able to just use `RepositoryTestHelpers.`
@pytest.fixture(name="test_helpers")
def fixture_test_helpers() -> Type[RepositoryTestHelpers]:
"""
Expand Down
34 changes: 16 additions & 18 deletions test/unit/repositories/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class CatalogueCategoryRepoDSL:
"""Base class for CatalogueCategoryRepo unit tests"""

# pylint:disable=too-many-instance-attributes
test_helpers: RepositoryTestHelpers
mock_database: Mock
mock_utils: Mock
catalogue_category_repository: CatalogueCategoryRepo
Expand All @@ -58,10 +57,9 @@ class CatalogueCategoryRepoDSL:
_mock_child_catalogue_item_data: Optional[dict]

@pytest.fixture(autouse=True)
def setup(self, test_helpers, database_mock):
def setup(self, database_mock):
"""Setup fixtures"""

self.test_helpers = test_helpers
self.mock_database = database_mock
self.catalogue_category_repository = CatalogueCategoryRepo(database_mock)
self.catalogue_categories_collection = database_mock.catalogue_categories
Expand All @@ -88,8 +86,8 @@ def mock_has_child_elements(
self._mock_child_catalogue_category_data = child_catalogue_category_data
self._mock_child_catalogue_item_data = child_catalogue_item_data

self.test_helpers.mock_find_one(self.catalogue_categories_collection, child_catalogue_category_data)
self.test_helpers.mock_find_one(self.catalogue_items_collection, child_catalogue_item_data)
RepositoryTestHelpers.mock_find_one(self.catalogue_categories_collection, child_catalogue_category_data)
RepositoryTestHelpers.mock_find_one(self.catalogue_items_collection, child_catalogue_item_data)

def check_has_child_elements_performed_expected_calls(self, expected_catalogue_category_id: str):
"""Checks that a call to `has_child_elements` performed the expected function calls
Expand Down Expand Up @@ -144,7 +142,7 @@ def mock_create(
if catalogue_category_in_data["parent_id"]:
# If parent_catalogue_category_data is given as None, then it is intentionally supposed to be, otherwise
# pass through CatalogueCategoryIn first to ensure it has creation and modified times
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection,
(
{
Expand All @@ -155,16 +153,16 @@ def mock_create(
else None
),
)
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection,
(
{**CatalogueCategoryIn(**duplicate_catalogue_category_in_data).model_dump(), "_id": ObjectId()}
if duplicate_catalogue_category_in_data
else None
),
)
self.test_helpers.mock_insert_one(self.catalogue_categories_collection, inserted_catalogue_category_id)
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_insert_one(self.catalogue_categories_collection, inserted_catalogue_category_id)
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection,
{**self._catalogue_category_in.model_dump(by_alias=True), "_id": inserted_catalogue_category_id},
)
Expand Down Expand Up @@ -317,7 +315,7 @@ def mock_get(self, catalogue_category_id: str, catalogue_category_in_data: Optio
else None
)

self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection,
self._expected_catalogue_category_out.model_dump() if self._expected_catalogue_category_out else None,
)
Expand Down Expand Up @@ -474,7 +472,7 @@ def mock_list(self, catalogue_categories_in_data: list[dict]):
for catalogue_category_in_data in catalogue_categories_in_data
]

self.test_helpers.mock_find(
RepositoryTestHelpers.mock_find(
self.catalogue_categories_collection,
[catalogue_category_out.model_dump() for catalogue_category_out in self._expected_catalogue_categories_out],
)
Expand Down Expand Up @@ -595,7 +593,7 @@ def mock_update(
if new_catalogue_category_in_data["parent_id"]:
# If new_parent_catalogue_category_data is given as none, then it is intentionally supposed to be, otherwise
# pass through CatalogueCategoryIn first to ensure it has creation and modified times
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection,
(
{
Expand All @@ -616,7 +614,7 @@ def mock_update(
if stored_catalogue_category_in_data
else None
)
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection,
self._stored_catalogue_category_out.model_dump() if self._stored_catalogue_category_out else None,
)
Expand All @@ -629,7 +627,7 @@ def mock_update(
self._stored_catalogue_category_out
and (self._catalogue_category_in.name != self._stored_catalogue_category_out.name)
) or self._moving_catalogue_category:
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection,
(
{
Expand All @@ -645,7 +643,7 @@ def mock_update(
self._expected_catalogue_category_out = CatalogueCategoryOut(
**self._catalogue_category_in.model_dump(), id=CustomObjectId(catalogue_category_id)
)
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.catalogue_categories_collection, self._expected_catalogue_category_out.model_dump()
)

Expand Down Expand Up @@ -953,7 +951,7 @@ def mock_delete(
"""

self.mock_has_child_elements(child_catalogue_category_data, child_catalogue_item_data)
self.test_helpers.mock_delete_one(self.catalogue_categories_collection, deleted_count)
RepositoryTestHelpers.mock_delete_one(self.catalogue_categories_collection, deleted_count)

def call_delete(self, catalogue_category_id: str):
"""Calls the CatalogueCategoryRepo `delete` method"""
Expand Down Expand Up @@ -1076,7 +1074,7 @@ def mock_create_property(self, property_in_data: dict):
self._property_in = CatalogueCategoryPropertyIn(**property_in_data)
self._expected_property_out = CatalogueCategoryPropertyOut(**self._property_in.model_dump(by_alias=True))

self.test_helpers.mock_update_one(self.catalogue_categories_collection)
RepositoryTestHelpers.mock_update_one(self.catalogue_categories_collection)

def call_create_property(self, catalogue_category_id: str):
"""Calls the CatalogueCategoryRepo `create_property` method with the appropriate data from a prior call to
Expand Down Expand Up @@ -1157,7 +1155,7 @@ def mock_update_property(self, property_in_data: dict):
self._property_in = CatalogueCategoryPropertyIn(**property_in_data)
self._expected_property_out = CatalogueCategoryPropertyOut(**self._property_in.model_dump(by_alias=True))

self.test_helpers.mock_update_one(self.catalogue_categories_collection)
RepositoryTestHelpers.mock_update_one(self.catalogue_categories_collection)

def call_update_property(self, catalogue_category_id: str, property_id: str):
"""Calls the CatalogueCategoryRepo `update_property` method with the appropriate data from a prior call to
Expand Down
28 changes: 13 additions & 15 deletions test/unit/repositories/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class SystemRepoDSL:
"""Base class for SystemRepo unit tests"""

# pylint:disable=too-many-instance-attributes
test_helpers: RepositoryTestHelpers
mock_database: Mock
mock_utils: Mock
system_repository: SystemRepo
Expand All @@ -46,10 +45,9 @@ class SystemRepoDSL:
_mock_child_item_data: Optional[dict]

@pytest.fixture(autouse=True)
def setup(self, test_helpers, database_mock):
def setup(self, database_mock):
"""Setup fixtures"""

self.test_helpers = test_helpers
self.mock_database = database_mock
self.system_repository = SystemRepo(database_mock)
self.systems_collection = database_mock.systems
Expand All @@ -74,8 +72,8 @@ def mock_has_child_elements(self, child_system_data: Optional[dict] = None, chil
self._mock_child_system_data = child_system_data
self._mock_child_item_data = child_item_data

self.test_helpers.mock_find_one(self.systems_collection, child_system_data)
self.test_helpers.mock_find_one(self.items_collection, child_item_data)
RepositoryTestHelpers.mock_find_one(self.systems_collection, child_system_data)
RepositoryTestHelpers.mock_find_one(self.items_collection, child_item_data)

def check_has_child_elements_performed_expected_calls(self, expected_system_id: str):
"""Checks that a call to `_has_child_elements` performed the expected function calls
Expand All @@ -98,7 +96,7 @@ def mock_is_duplicate_system(self, duplicate_system_in_data: Optional[dict] = No
:param duplicate_system_in_data: Either None or a dictionary containing system data for a duplicate system
"""

self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.systems_collection,
(
{**SystemIn(**duplicate_system_in_data).model_dump(), "_id": ObjectId()}
Expand Down Expand Up @@ -159,7 +157,7 @@ def mock_create(
if system_in_data["parent_id"]:
# If parent_system_data is given as None, then it is intentionally supposed to be, otherwise
# pass through SystemIn first to ensure it has creation and modified times
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.systems_collection,
(
{**SystemIn(**parent_system_in_data).model_dump(), "_id": system_in_data["parent_id"]}
Expand All @@ -168,8 +166,8 @@ def mock_create(
),
)
self.mock_is_duplicate_system(duplicate_system_in_data)
self.test_helpers.mock_insert_one(self.systems_collection, inserted_system_id)
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_insert_one(self.systems_collection, inserted_system_id)
RepositoryTestHelpers.mock_find_one(
self.systems_collection, {**self._system_in.model_dump(), "_id": inserted_system_id}
)

Expand Down Expand Up @@ -281,7 +279,7 @@ def mock_get(self, system_id: str, system_in_data: Optional[dict]):
else None
)

self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.systems_collection, self._expected_system_out.model_dump() if self._expected_system_out else None
)

Expand Down Expand Up @@ -420,7 +418,7 @@ def mock_list(self, systems_in_data: list[dict]):
SystemOut(**SystemIn(**system_in_data).model_dump(), id=ObjectId()) for system_in_data in systems_in_data
]

self.test_helpers.mock_find(
RepositoryTestHelpers.mock_find(
self.systems_collection, [system_out.model_dump() for system_out in self._expected_systems_out]
)

Expand Down Expand Up @@ -523,7 +521,7 @@ def mock_update(
if new_system_in_data["parent_id"]:
# If new_parent_system_data is given as none, then it is intentionally supposed to be, otherwise
# pass through SystemIn first to ensure it has creation and modified times
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.systems_collection,
(
{**SystemIn(**new_parent_system_in_data).model_dump(), "_id": new_system_in_data["parent_id"]}
Expand All @@ -538,7 +536,7 @@ def mock_update(
if stored_system_in_data
else None
)
self.test_helpers.mock_find_one(
RepositoryTestHelpers.mock_find_one(
self.systems_collection, self._stored_system_out.model_dump() if self._stored_system_out else None
)

Expand All @@ -551,7 +549,7 @@ def mock_update(

# Final system after update
self._expected_system_out = SystemOut(**self._system_in.model_dump(), id=CustomObjectId(system_id))
self.test_helpers.mock_find_one(self.systems_collection, self._expected_system_out.model_dump())
RepositoryTestHelpers.mock_find_one(self.systems_collection, self._expected_system_out.model_dump())

if self._moving_system:
mock_aggregation_pipeline = MagicMock()
Expand Down Expand Up @@ -765,7 +763,7 @@ def mock_delete(
"""

self.mock_has_child_elements(child_system_data, child_item_data)
self.test_helpers.mock_delete_one(self.systems_collection, deleted_count)
RepositoryTestHelpers.mock_delete_one(self.systems_collection, deleted_count)

def call_delete(self, system_id: str):
"""Calls the SystemRepo `delete` method"""
Expand Down

0 comments on commit 222d588

Please sign in to comment.