diff --git a/inventory_management_system_api/core/breadcrumbs.py b/inventory_management_system_api/core/breadcrumbs.py index 524fc53c..51ce88f7 100644 --- a/inventory_management_system_api/core/breadcrumbs.py +++ b/inventory_management_system_api/core/breadcrumbs.py @@ -13,14 +13,14 @@ logger = logging.getLogger() -def query_breadcrumbs(entity_id: str, entity_collection: Collection, graph_lookup_from: str) -> BreadcrumbsGetSchema: +def query_breadcrumbs(entity_id: str, entity_collection: Collection, collection_name: str) -> BreadcrumbsGetSchema: """ Query breadcrumbs for an entity given it's ID :param entity_id: ID of the entity to look up :param entity_collection: Collection for looking up entities - It is assumed that the entities have an _id, _name and parent_id field - :param graph_lookup_from: Value of "from" to use for the $graphLookup query - Should be the name of + :param collection_name: Value of "from" to use for the $graphLookup query - Should be the name of the collection :raises InvalidObjectIdError: If the given entity_id is invalid :raises DatabaseIntegrityError: If the query returns a less than the maximum allowed trail while not @@ -37,7 +37,7 @@ def query_breadcrumbs(entity_id: str, entity_collection: Collection, graph_looku {"$match": {"_id": CustomObjectId(entity_id)}}, { "$graphLookup": { - "from": graph_lookup_from, + "from": collection_name, "startWith": "$parent_id", "connectFromField": "parent_id", "connectToField": "_id", @@ -67,7 +67,7 @@ def query_breadcrumbs(entity_id: str, entity_collection: Collection, graph_looku ) )[0]["result"] if len(result) == 0: - raise MissingRecordError(f"Entity with the ID {entity_id} was not found in the collection {graph_lookup_from}") + raise MissingRecordError(f"Entity with the ID {entity_id} was not found in the collection {collection_name}") for element in result: trail.append((str(element["_id"]), element["name"])) full_trail = result[0]["parent_id"] is None @@ -77,6 +77,6 @@ def query_breadcrumbs(entity_id: str, entity_collection: Collection, graph_looku if not full_trail and len(trail) != BREADCRUMBS_TRAIL_MAX_LENGTH: raise DatabaseIntegrityError( f"Unable to locate full trail for entity with id '{entity_id}' from the database collection " - f"'{graph_lookup_from}'" + f"'{collection_name}'" ) return BreadcrumbsGetSchema(trail=trail, full_trail=full_trail) diff --git a/inventory_management_system_api/repositories/catalogue_category.py b/inventory_management_system_api/repositories/catalogue_category.py index 6e0c8e17..3c81b9cd 100644 --- a/inventory_management_system_api/repositories/catalogue_category.py +++ b/inventory_management_system_api/repositories/catalogue_category.py @@ -109,7 +109,7 @@ def get_breadcrumbs(self, catalogue_category_id: str) -> BreadcrumbsGetSchema: return query_breadcrumbs( entity_id=catalogue_category_id, entity_collection=self._catalogue_categories_collection, - graph_lookup_from="catalogue_categories", + collection_name="catalogue_categories", ) def update(self, catalogue_category_id: str, catalogue_category: CatalogueCategoryIn) -> CatalogueCategoryOut: diff --git a/inventory_management_system_api/repositories/system.py b/inventory_management_system_api/repositories/system.py index 32e16557..76984485 100644 --- a/inventory_management_system_api/repositories/system.py +++ b/inventory_management_system_api/repositories/system.py @@ -109,7 +109,7 @@ def get_breadcrumbs(self, system_id: str) -> BreadcrumbsGetSchema: return query_breadcrumbs( entity_id=system_id, entity_collection=self._systems_collection, - graph_lookup_from="systems", + collection_name="systems", ) def list(self, path: Optional[str], parent_path: Optional[str]) -> list[SystemOut]: diff --git a/test/unit/core/test_breacrumbs.py b/test/unit/core/test_breacrumbs.py index 1b36532f..f3394e1f 100644 --- a/test/unit/core/test_breacrumbs.py +++ b/test/unit/core/test_breacrumbs.py @@ -46,14 +46,14 @@ class TestComputeBreadcrumbsWhenValid: """Test query_breadcrumbs functions correctly when it has no errors""" - def _get_expected_pipeline(self, entity_id: str, graph_lookup_from: str): - """Returns the aggregate pipeline expected given an entity_id and graph_lookup_from values""" + def _get_expected_pipeline(self, entity_id: str, collection_name: str): + """Returns the aggregate pipeline expected given an entity_id and collection_name values""" return [ # pylint: disable=duplicate-code {"$match": {"_id": CustomObjectId(entity_id)}}, { "$graphLookup": { - "from": graph_lookup_from, + "from": collection_name, "startWith": "$parent_id", "connectFromField": "parent_id", "connectToField": "_id", @@ -89,12 +89,12 @@ def _test_query_breadcrumbs( expected breadcrumbs output""" entity_id = str(ObjectId()) mock_entity_collection = MagicMock() - graph_lookup_from = MagicMock() + collection_name = MagicMock() mock_entity_collection.aggregate.return_value = mock_aggregate_response - expected_pipeline = self._get_expected_pipeline(entity_id, graph_lookup_from) + expected_pipeline = self._get_expected_pipeline(entity_id, collection_name) result = breadcrumbs.query_breadcrumbs( - entity_id=entity_id, entity_collection=mock_entity_collection, graph_lookup_from=graph_lookup_from + entity_id=entity_id, entity_collection=mock_entity_collection, collection_name=collection_name ) mock_entity_collection.aggregate.assert_called_once_with(expected_pipeline) @@ -133,18 +133,18 @@ def test_query_breadcrumbs_when_entity_not_found(self): """ entity_id = str(ObjectId()) mock_entity_collection = MagicMock() - graph_lookup_from = MagicMock() - expected_pipeline = self._get_expected_pipeline(entity_id, graph_lookup_from) + collection_name = MagicMock() + expected_pipeline = self._get_expected_pipeline(entity_id, collection_name) mock_entity_collection.aggregate.return_value = MOCK_AGGREGATE_RESPONSE_NON_EXISTENT_ID with pytest.raises(MissingRecordError) as exc: breadcrumbs.query_breadcrumbs( - entity_id=entity_id, entity_collection=mock_entity_collection, graph_lookup_from=graph_lookup_from + entity_id=entity_id, entity_collection=mock_entity_collection, collection_name=collection_name ) mock_entity_collection.aggregate.assert_called_once_with(expected_pipeline) - assert str(exc.value) == f"Entity with the ID {entity_id} was not found in the collection {graph_lookup_from}" + assert str(exc.value) == f"Entity with the ID {entity_id} was not found in the collection {collection_name}" def test_query_breadcrumbs_when_entity_id_is_invalid(self): """ @@ -152,13 +152,13 @@ def test_query_breadcrumbs_when_entity_id_is_invalid(self): """ entity_id = "invalid" mock_entity_collection = MagicMock() - graph_lookup_from = MagicMock() + collection_name = MagicMock() mock_entity_collection.aggregate.return_value = MOCK_AGGREGATE_RESPONSE_NON_EXISTENT_ID with pytest.raises(InvalidObjectIdError) as exc: breadcrumbs.query_breadcrumbs( - entity_id=entity_id, entity_collection=mock_entity_collection, graph_lookup_from=graph_lookup_from + entity_id=entity_id, entity_collection=mock_entity_collection, collection_name=collection_name ) mock_entity_collection.aggregate.assert_not_called() @@ -170,18 +170,18 @@ def test_query_breadcrumbs_when_invalid_parent_in_db(self): """ entity_id = str(ObjectId()) mock_entity_collection = MagicMock() - graph_lookup_from = MagicMock() - expected_pipeline = self._get_expected_pipeline(entity_id, graph_lookup_from) + collection_name = MagicMock() + expected_pipeline = self._get_expected_pipeline(entity_id, collection_name) mock_entity_collection.aggregate.return_value = MOCK_AGGREGATE_RESPONSE_INVALID_PARENT_IN_DB with pytest.raises(DatabaseIntegrityError) as exc: breadcrumbs.query_breadcrumbs( - entity_id=entity_id, entity_collection=mock_entity_collection, graph_lookup_from=graph_lookup_from + entity_id=entity_id, entity_collection=mock_entity_collection, collection_name=collection_name ) mock_entity_collection.aggregate.assert_called_once_with(expected_pipeline) assert str(exc.value) == ( f"Unable to locate full trail for entity with id '{entity_id}' from the database collection " - f"'{graph_lookup_from}'" + f"'{collection_name}'" ) diff --git a/test/unit/repositories/test_catalogue_category.py b/test/unit/repositories/test_catalogue_category.py index 41ccefb6..f780cc90 100644 --- a/test/unit/repositories/test_catalogue_category.py +++ b/test/unit/repositories/test_catalogue_category.py @@ -597,7 +597,7 @@ def test_get_breadcrumbs(mock_query_breadcrumbs, database_mock, catalogue_catego mock_query_breadcrumbs.assert_called_once_with( entity_id=catalogue_category_id, entity_collection=database_mock.catalogue_categories, - graph_lookup_from="catalogue_categories", + collection_name="catalogue_categories", ) assert retrieved_breadcrumbs == mock_breadcrumbs diff --git a/test/unit/repositories/test_system.py b/test/unit/repositories/test_system.py index a41a6b30..6486ad83 100644 --- a/test/unit/repositories/test_system.py +++ b/test/unit/repositories/test_system.py @@ -327,7 +327,7 @@ def test_get_breadcrumbs(mock_query_breadcrumbs, database_mock, system_repositor mock_query_breadcrumbs.assert_called_once_with( entity_id=system_id, entity_collection=database_mock.systems, - graph_lookup_from="systems", + collection_name="systems", ) assert retrieved_breadcrumbs == mock_breadcrumbs