From 1be0716d1627b792ad2760f533da38ba6c0b1f29 Mon Sep 17 00:00:00 2001 From: Joel Davies Date: Mon, 23 Oct 2023 14:26:57 +0100 Subject: [PATCH] Remove some redundant tests #90 --- test/unit/services/test_catalogue_category.py | 119 +----------------- test/unit/services/test_catalogue_item.py | 106 ++-------------- test/unit/services/test_system.py | 64 +--------- 3 files changed, 22 insertions(+), 267 deletions(-) diff --git a/test/unit/services/test_catalogue_category.py b/test/unit/services/test_catalogue_category.py index 20e1ed1c..091e0c58 100644 --- a/test/unit/services/test_catalogue_category.py +++ b/test/unit/services/test_catalogue_category.py @@ -291,126 +291,19 @@ def test_get_breadcrumbs(test_helpers, catalogue_category_repository_mock, catal assert retrieved_breadcrumbs == breadcrumbs -def test_list(test_helpers, catalogue_category_repository_mock, catalogue_category_service): +def test_list(catalogue_category_repository_mock, catalogue_category_service): """ - Test getting catalogue categories. + Test listing catalogue categories. - Verify that the `list` method properly handles the retrieval of catalogue categories without filters. + Verify that the `list` method properly calls the repository function with any passed filters """ - # pylint: disable=duplicate-code - catalogue_category_a = CatalogueCategoryOut( - id=str(ObjectId()), - name="Category A", - code="category-a", - is_leaf=False, - parent_id=None, - catalogue_item_properties=[], - ) - - catalogue_category_b = CatalogueCategoryOut( - id=str(ObjectId()), - name="Category B", - code="category-b", - is_leaf=False, - parent_id=None, - catalogue_item_properties=[], - ) - # pylint: enable=duplicate-code - - # Mock `list` to return a list of catalogue categories - test_helpers.mock_list(catalogue_category_repository_mock, [catalogue_category_a, catalogue_category_b]) - - retrieved_catalogue_categories = catalogue_category_service.list(None) - - catalogue_category_repository_mock.list.assert_called_once_with(None) - assert retrieved_catalogue_categories == [catalogue_category_a, catalogue_category_b] - - -def test_list_with_parent_id_filter(test_helpers, catalogue_category_repository_mock, catalogue_category_service): - """ - Test getting catalogue categories based on the provided parent_id filter. - - Verify that the `list` method properly handles the retrieval of catalogue categories based on the provided - parent_id filter. - """ - # pylint: disable=duplicate-code - catalogue_category = CatalogueCategoryOut( - id=str(ObjectId()), - name="Category A", - code="category-a", - is_leaf=False, - parent_id=None, - catalogue_item_properties=[], - ) - # pylint: enable=duplicate-code - - # Mock `list` to return a list of catalogue categories - test_helpers.mock_list(catalogue_category_repository_mock, [catalogue_category]) - - parent_id = str(ObjectId()) - retrieved_catalogue_categories = catalogue_category_service.list(parent_id) - catalogue_category_repository_mock.list.assert_called_once_with(parent_id) - assert retrieved_catalogue_categories == [catalogue_category] - - -def test_list_with_null_parent_id_filter(test_helpers, catalogue_category_repository_mock, catalogue_category_service): - """ - Test getting catalogue categories when given a parent_id filter of "null" - - Verify that the `list` method properly handles the retrieval of catalogue categories based on the provided - parent_id filter. - """ - # pylint: disable=duplicate-code - catalogue_category_a = CatalogueCategoryOut( - id=str(ObjectId()), - name="Category A", - code="category-a", - is_leaf=False, - parent_id=None, - catalogue_item_properties=[], - ) - # pylint: enable=duplicate-code - - # pylint: disable=duplicate-code - catalogue_category_b = CatalogueCategoryOut( - id=str(ObjectId()), - name="Category B", - code="category-b", - is_leaf=False, - parent_id=None, - catalogue_item_properties=[], - ) - # pylint: enable=duplicate-code - - # Mock `list` to return a list of catalogue categories - test_helpers.mock_list(catalogue_category_repository_mock, [catalogue_category_a, catalogue_category_b]) - - parent_id = str(ObjectId()) - retrieved_catalogue_categories = catalogue_category_service.list(parent_id) - - catalogue_category_repository_mock.list.assert_called_once_with(parent_id) - assert retrieved_catalogue_categories == [catalogue_category_a, catalogue_category_b] - - -def test_list_with_parent_id_filter_no_matching_results( - test_helpers, catalogue_category_repository_mock, catalogue_category_service -): - """ - Test getting catalogue categories based on the provided parent_id filter when there is no matching - results in the database. - - Verify that the `list` method properly handles the retrieval of catalogue categories based on the parent_id - filter. - """ - # Mock `list` to return an empty list of catalogue categories - test_helpers.mock_list(catalogue_category_repository_mock, []) + parent_id = MagicMock() - parent_id = str(ObjectId()) - retrieved_catalogue_categories = catalogue_category_service.list(parent_id) + result = catalogue_category_service.list(parent_id=parent_id) catalogue_category_repository_mock.list.assert_called_once_with(parent_id) - assert retrieved_catalogue_categories == [] + assert result == catalogue_category_repository_mock.list.return_value def test_update(test_helpers, catalogue_category_repository_mock, catalogue_category_service): diff --git a/test/unit/services/test_catalogue_item.py b/test/unit/services/test_catalogue_item.py index 5892dacf..884160f2 100644 --- a/test/unit/services/test_catalogue_item.py +++ b/test/unit/services/test_catalogue_item.py @@ -3,6 +3,7 @@ Unit tests for the `CatalogueCategoryService` service. """ +from unittest.mock import MagicMock import pytest from bson import ObjectId @@ -513,11 +514,11 @@ def test_get(test_helpers, catalogue_item_repository_mock, catalogue_item_servic assert retrieved_catalogue_item == catalogue_item -def test_get_with_nonexistent_id(test_helpers, catalogue_item_repository_mock, catalogue_item_service): +def test_get_with_non_existent_id(test_helpers, catalogue_item_repository_mock, catalogue_item_service): """ - Test getting a catalogue item with a nonexistent ID. + Test getting a catalogue item with a non-existent ID. - Verify that the `get` method properly handles the retrieval of a catalogue item with a nonexistent ID. + Verify that the `get` method properly handles the retrieval of a catalogue item with a non-existent ID. """ catalogue_item_id = str(ObjectId()) @@ -530,106 +531,19 @@ def test_get_with_nonexistent_id(test_helpers, catalogue_item_repository_mock, c catalogue_item_repository_mock.get.assert_called_once_with(catalogue_item_id) -def test_list(test_helpers, catalogue_item_repository_mock, catalogue_item_service): +def test_list(catalogue_item_repository_mock, catalogue_item_service): """ - Test getting catalogue items. + Test listing systems - Verify that the `list` method properly handles the retrieval of catalogue items without filters. + Verify that the `list` method properly calls the repository function with any passed filters """ - # pylint: disable=duplicate-code - catalogue_item_a = CatalogueItemOut( - id=str(ObjectId()), - catalogue_category_id=str(ObjectId()), - name="Catalogue Item A", - description="This is Catalogue Item A", - properties=[ - Property(name="Property A", value=20, unit="mm"), - Property(name="Property B", value=False), - Property(name="Property C", value="20x15x10", unit="cm"), - ], - manufacturer=Manufacturer( - name="Manufacturer A", - address="1 Address, City, Country, Postcode", - web_url="https://www.manufacturer-a.co.uk", - ), - ) - catalogue_item_b = CatalogueItemOut( - id=str(ObjectId()), - catalogue_category_id=str(ObjectId()), - name="Catalogue Item B", - description="This is Catalogue Item B", - properties=[Property(name="Property A", value=True)], - manufacturer=Manufacturer( - name="Manufacturer A", - address="1 Address, City, Country, Postcode", - web_url="https://www.manufacturer-a.co.uk", - ), - ) - # pylint: enable=duplicate-code - - # Mock `list` to return a list of catalogue items - test_helpers.mock_list(catalogue_item_repository_mock, [catalogue_item_a, catalogue_item_b]) - - retrieved_catalogue_items = catalogue_item_service.list(None) - - catalogue_item_repository_mock.list.assert_called_once_with(None) - assert retrieved_catalogue_items == [catalogue_item_a, catalogue_item_b] - - -def test_list_with_catalogue_category_id_filter(test_helpers, catalogue_item_repository_mock, catalogue_item_service): - """ - Test getting catalogue items based on the provided catalogue category ID filter. + catalogue_category_id = MagicMock() - Verify that the `list` method properly handles the retrieval of catalogue items based on the provided catalogue - category ID filter. - """ - # pylint: disable=duplicate-code - catalogue_item = CatalogueItemOut( - id=str(ObjectId()), - catalogue_category_id=str(ObjectId()), - name="Catalogue Item A", - description="This is Catalogue Item A", - properties=[ - Property(name="Property A", value=20, unit="mm"), - Property(name="Property B", value=False), - Property(name="Property C", value="20x15x10", unit="cm"), - ], - manufacturer=Manufacturer( - name="Manufacturer A", - address="1 Address, City, Country, Postcode", - web_url="https://www.manufacturer-a.co.uk", - ), - ) - # pylint: enable=duplicate-code - - # Mock `list` to return a list of catalogue items - test_helpers.mock_list(catalogue_item_repository_mock, [catalogue_item]) - - retrieved_catalogue_items = catalogue_item_service.list(catalogue_item.catalogue_category_id) - - catalogue_item_repository_mock.list.assert_called_once_with(catalogue_item.catalogue_category_id) - assert retrieved_catalogue_items == [catalogue_item] - - -def test_list_with_catalogue_category_id_filter_no_matching_results( - test_helpers, catalogue_item_repository_mock, catalogue_item_service -): - """ - Test getting catalogue items based on the provided catalogue category ID filter when there is no matching results in - the database. - - Verify that the `list` method properly handles the retrieval of catalogue items based on the provided catalogue - category ID filter. - """ - # Mock `list` to return an empty list of catalogue item documents - test_helpers.mock_list(catalogue_item_repository_mock, []) - - catalogue_category_id = str(ObjectId()) - retrieved_catalogue_items = catalogue_item_service.list(catalogue_category_id) + result = catalogue_item_service.list(catalogue_category_id=catalogue_category_id) catalogue_item_repository_mock.list.assert_called_once_with(catalogue_category_id) - assert retrieved_catalogue_items == [] + assert result == catalogue_item_repository_mock.list.return_value def test_update(test_helpers, catalogue_item_repository_mock, catalogue_item_service): diff --git a/test/unit/services/test_system.py b/test/unit/services/test_system.py index 3fb68730..f87d8798 100644 --- a/test/unit/services/test_system.py +++ b/test/unit/services/test_system.py @@ -2,7 +2,6 @@ Unit tests for the `SystemService` service """ -from typing import Optional from unittest.mock import MagicMock from bson import ObjectId @@ -11,26 +10,6 @@ from inventory_management_system_api.schemas.system import SystemPostRequestSchema -def _test_list(test_helpers, system_repository_mock, system_service, parent_id: Optional[str]): - """ - Utility method that tests getting Systems - - Verifies that the `list` method properly handles the retrieval of systems with the given filters - """ - systems = [MagicMock(), MagicMock()] - - # Mock `list` to return a list of systems - test_helpers.mock_list( - system_repository_mock, - systems, - ) - - retrieved_systems = system_service.list(parent_id) - - system_repository_mock.list.assert_called_once_with(parent_id) - assert retrieved_systems == systems - - def test_create(test_helpers, system_repository_mock, system_service): """ Test creating a System @@ -204,47 +183,16 @@ def test_get_breadcrumbs(test_helpers, system_repository_mock, system_service): assert retrieved_breadcrumbs == breadcrumbs -def test_list(test_helpers, system_repository_mock, system_service): - """ - Test getting Systems - - Verify that the `list` method properly handles the retrieval of Systems without filters - """ - _test_list(test_helpers, system_repository_mock, system_service, None) - - -def test_list_with_parent_id_filter(test_helpers, system_repository_mock, system_service): - """ - Test getting Systems based on the provided parent_id filter - - Verify that the `list` method properly handles the retrieval of Systems based on the provided parent_id filter - """ - _test_list(test_helpers, system_repository_mock, system_service, str(ObjectId())) - - -def test_list_with_null_parent_id_filter(test_helpers, system_repository_mock, system_service): - """ - Test getting Systems based on the provided parent_id filter - - Verify that the `list` method properly handles the retrieval of Systems based on the provided parent_id filter - """ - _test_list(test_helpers, system_repository_mock, system_service, "null") - - -def test_list_with_parent_id_filter_no_matching_results(test_helpers, system_repository_mock, system_service): +def test_list(system_repository_mock, system_service): """ - Test getting Systems based on the provided parent_id filter when there is no matching results in the - database + Test listing systems - Verify that the `list` method properly handles the retrieval of Systems based on the provided parent_id - filter when there is no matching results in the database + Verify that the `list` method properly calls the repository function with any passed filters """ - # Mock `list` to return an empty list of Systems - test_helpers.mock_list(system_repository_mock, []) + parent_id = MagicMock() - parent_id = str(ObjectId()) - retrieved_systems = system_service.list(parent_id) + result = system_service.list(parent_id=parent_id) system_repository_mock.list.assert_called_once_with(parent_id) - assert retrieved_systems == [] + assert result == system_repository_mock.list.return_value