Skip to content

Commit

Permalink
Rename graph_lookup_from to collection_name #87
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvdavies committed Oct 24, 2023
1 parent da8540b commit d9012d6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions inventory_management_system_api/core/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion inventory_management_system_api/repositories/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
32 changes: 16 additions & 16 deletions test/unit/core/test_breacrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -133,32 +133,32 @@ 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):
"""
Test query_breadcrumbs functions correctly when the given entity_id is invalid
"""
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()
Expand All @@ -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}'"
)
2 changes: 1 addition & 1 deletion test/unit/repositories/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/unit/repositories/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d9012d6

Please sign in to comment.