Skip to content

Commit

Permalink
Update tests #33
Browse files Browse the repository at this point in the history
  • Loading branch information
VKTB committed Jul 3, 2023
1 parent 61f7bd0 commit fc60e2d
Show file tree
Hide file tree
Showing 3 changed files with 473 additions and 1 deletion.
111 changes: 111 additions & 0 deletions test/e2e/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,114 @@ def test_get_catalogue_category_with_nonexistent_id(test_client):

assert response.status_code == 404
assert response.json()["detail"] == "The requested catalogue category was not found"


def test_get_catalogue_categories(test_client):
"""
Test getting catalogue categories.
"""
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category A", is_leaf=False)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category B", is_leaf=False)
response = test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())
catalogue_category = CatalogueCategorySchema(**response.json())

parent_id = catalogue_category.id
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category C", is_leaf=True, parent_id=parent_id)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())

response = test_client.get("/v1/catalogue-categories")

assert response.status_code == 200

catalogue_categories = [CatalogueCategorySchema(**r) for r in response.json()]

assert len(catalogue_categories) == 3
assert catalogue_categories[0].path == "/category-a"
assert catalogue_categories[0].parent_path == "/"
assert catalogue_categories[1].path == "/category-b"
assert catalogue_categories[1].parent_path == "/"
assert catalogue_categories[2].path == "/category-b/category-c"
assert catalogue_categories[2].parent_path == "/category-b"


def test_list_with_path_filter(test_client):
"""
Test getting catalogue categories based on the provided parent path filter.
"""
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category A", is_leaf=False)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category B", is_leaf=False)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())

response = test_client.get("/v1/catalogue-categories", params={"path": "/category-a"})

assert response.status_code == 200

catalogue_categories = [CatalogueCategorySchema(**r) for r in response.json()]

assert len(catalogue_categories) == 1
assert catalogue_categories[0].path == "/category-a"
assert catalogue_categories[0].parent_path == "/"


def test_list_with_parent_path_filter(test_client):
"""
Test getting catalogue categories based on the provided parent path filter.
"""
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category B", is_leaf=False)
response = test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())
catalogue_category = CatalogueCategorySchema(**response.json())

parent_id = catalogue_category.id
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category C", is_leaf=True, parent_id=parent_id)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())

response = test_client.get("/v1/catalogue-categories", params={"parent_path": "/"})

assert response.status_code == 200

catalogue_categories = [CatalogueCategorySchema(**r) for r in response.json()]

assert len(catalogue_categories) == 1
assert catalogue_categories[0].path == "/category-b"
assert catalogue_categories[0].parent_path == "/"


def test_list_with_path_and_parent_path_filters(test_client):
"""
Test getting catalogue categories based on the provided path and parent path filters.
"""
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category A", is_leaf=False)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category B", is_leaf=False)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())

response = test_client.get("/v1/catalogue-categories", params={"path": "/category-b", "parent_path": "/"})

assert response.status_code == 200

catalogue_categories = [CatalogueCategorySchema(**r) for r in response.json()]

assert len(catalogue_categories) == 1
assert catalogue_categories[0].path == "/category-b"
assert catalogue_categories[0].parent_path == "/"


def test_list_with_path_and_parent_path_filters_no_matching_results(test_client):
"""
Test getting catalogue categories based on the provided path and parent path filters when there is no matching
results in the database.
"""
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category A", is_leaf=False)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())
catalogue_category_post = CatalogueCategoryPostRequestSchema(name="Category B", is_leaf=False)
test_client.post("/v1/catalogue-categories", json=catalogue_category_post.dict())

response = test_client.get("/v1/catalogue-categories", params={"path": "/category-c", "parent_path": "/"})

assert response.status_code == 200

catalogue_categories = [CatalogueCategorySchema(**r) for r in response.json()]

assert len(catalogue_categories) == 0
226 changes: 225 additions & 1 deletion test/unit/repositories/test_catalogue_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"""
Unit tests for the `CatalogueCategoryRepo` repository.
"""
from unittest.mock import Mock, call
from typing import List
from unittest.mock import Mock, call, MagicMock

import pytest
from bson import ObjectId
from pymongo.collection import Collection
from pymongo.cursor import Cursor
from pymongo.database import Database
from pymongo.results import InsertOneResult

Expand Down Expand Up @@ -68,6 +70,18 @@ def mock_insert_one(database_mock: Mock, inserted_id: ObjectId) -> None:
)


def mock_find(database_mock: Mock, documents: List[dict]) -> None:
"""
Mocks the `find` method of the MongoDB database mock to return a specific list of documents.
:param database_mock: Mocked MongoDB database instance.
:param documents: The list of documents to be returned by the `find` method.
"""
cursor_mock = MagicMock(Cursor)
cursor_mock.__iter__.return_value = iter(documents)
database_mock.catalogue_categories.find.return_value = cursor_mock


def mock_find_one(database_mock: Mock, document: dict | None) -> None:
"""
Mocks the `find_one` method of the MongoDB database mock to return a specific document.
Expand Down Expand Up @@ -367,3 +381,213 @@ def test_get_with_nonexistent_id(database_mock, catalogue_category_repository):

assert retrieved_catalogue_category is None
database_mock.catalogue_categories.find_one.assert_called_once_with({"_id": CustomObjectId(catalogue_category_id)})


def test_list(database_mock, catalogue_category_repository):
"""
Test getting catalogue categories.
Verify that the `list` method properly handles the retrieval of catalogue categories without filters.
"""
catalogue_category_a = CatalogueCategoryOut(
id=str(ObjectId()),
name="Category A",
code="category-a",
is_leaf=False,
path="/category-a",
parent_path="/",
parent_id=None,
)

catalogue_category_b = CatalogueCategoryOut(
id=str(ObjectId()),
name="Category B",
code="category-b",
is_leaf=False,
path="/category-b",
parent_path="/",
parent_id=None,
)

# Mock find to return a list of catalogue category documents
mock_find(
database_mock,
[
{
"_id": CustomObjectId(catalogue_category_a.id),
"name": catalogue_category_a.name,
"code": catalogue_category_a.code,
"is_leaf": catalogue_category_a.is_leaf,
"path": catalogue_category_a.path,
"parent_path": catalogue_category_a.parent_path,
"parent_id": catalogue_category_a.parent_id,
},
{
"_id": CustomObjectId(catalogue_category_b.id),
"name": catalogue_category_b.name,
"code": catalogue_category_b.code,
"is_leaf": catalogue_category_b.is_leaf,
"path": catalogue_category_b.path,
"parent_path": catalogue_category_b.parent_path,
"parent_id": catalogue_category_b.parent_id,
},
],
)

retrieved_catalogue_categories = catalogue_category_repository.list(None, None)

database_mock.catalogue_categories.find.assert_called_once_with({})
assert retrieved_catalogue_categories == [catalogue_category_a, catalogue_category_b]


def test_list_with_path_filter(database_mock, catalogue_category_repository):
"""
Test getting catalogue categories based on the provided path filter.
Verify that the `list` method properly handles the retrieval of catalogue categories based on the provided path
filter.
"""
catalogue_category = CatalogueCategoryOut(
id=str(ObjectId()),
name="Category A",
code="category-a",
is_leaf=False,
path="/category-a",
parent_path="/",
parent_id=None,
)

# Mock find to return a list of catalogue category documents
mock_find(
database_mock,
[
{
"_id": CustomObjectId(catalogue_category.id),
"name": catalogue_category.name,
"code": catalogue_category.code,
"is_leaf": catalogue_category.is_leaf,
"path": catalogue_category.path,
"parent_path": catalogue_category.parent_path,
"parent_id": catalogue_category.parent_id,
}
],
)

retrieved_catalogue_categories = catalogue_category_repository.list("/category-a", None)

database_mock.catalogue_categories.find.assert_called_once_with({"path": "/category-a"})
assert retrieved_catalogue_categories == [catalogue_category]


def test_list_with_parent_path_filter(database_mock, catalogue_category_repository):
"""
Test getting catalogue categories based on the provided parent path filter.
Verify that the `list` method properly handles the retrieval of catalogue categories based on the provided parent
path filter.
"""
catalogue_category_a = CatalogueCategoryOut(
id=str(ObjectId()),
name="Category A",
code="category-a",
is_leaf=False,
path="/category-a",
parent_path="/",
parent_id=None,
)

catalogue_category_b = CatalogueCategoryOut(
id=str(ObjectId()),
name="Category B",
code="category-b",
is_leaf=False,
path="/category-b",
parent_path="/",
parent_id=None,
)

# Mock find to return a list of catalogue category documents
mock_find(
database_mock,
[
{
"_id": CustomObjectId(catalogue_category_a.id),
"name": catalogue_category_a.name,
"code": catalogue_category_a.code,
"is_leaf": catalogue_category_a.is_leaf,
"path": catalogue_category_a.path,
"parent_path": catalogue_category_a.parent_path,
"parent_id": catalogue_category_a.parent_id,
},
{
"_id": CustomObjectId(catalogue_category_b.id),
"name": catalogue_category_b.name,
"code": catalogue_category_b.code,
"is_leaf": catalogue_category_b.is_leaf,
"path": catalogue_category_b.path,
"parent_path": catalogue_category_b.parent_path,
"parent_id": catalogue_category_b.parent_id,
},
],
)

retrieved_catalogue_categories = catalogue_category_repository.list(None, "/")

database_mock.catalogue_categories.find.assert_called_once_with({"parent_path": "/"})
assert retrieved_catalogue_categories == [catalogue_category_a, catalogue_category_b]


def test_list_with_path_and_parent_path_filters(database_mock, catalogue_category_repository):
"""
Test getting catalogue categories based on the provided path and parent path filters.
Verify that the `list` method properly handles the retrieval of catalogue categories based on the provided path and
parent path filters.
"""
catalogue_category = CatalogueCategoryOut(
id=str(ObjectId()),
name="Category B",
code="category-b",
is_leaf=False,
path="/category-b",
parent_path="/",
parent_id=None,
)

# Mock find to return a list of catalogue category documents
mock_find(
database_mock,
[
{
"_id": CustomObjectId(catalogue_category.id),
"name": catalogue_category.name,
"code": catalogue_category.code,
"is_leaf": catalogue_category.is_leaf,
"path": catalogue_category.path,
"parent_path": catalogue_category.parent_path,
"parent_id": catalogue_category.parent_id,
}
],
)

retrieved_catalogue_categories = catalogue_category_repository.list("/category-b", "/")

database_mock.catalogue_categories.find.assert_called_once_with({"path": "/category-b", "parent_path": "/"})
assert retrieved_catalogue_categories == [catalogue_category]


def test_list_with_path_and_parent_path_filters_no_matching_results(database_mock, catalogue_category_repository):
"""
Test getting catalogue categories based on the provided path and parent path filters when there is no matching
results in the database.
Verify that the `list` method properly handles the retrieval of catalogue categories based on the provided path and
parent path filters.
"""
# Mock find to return an empty list of catalogue category documents
mock_find(database_mock, [])

retrieved_catalogue_categories = catalogue_category_repository.list("/category-a", "/")

database_mock.catalogue_categories.find.assert_called_once_with({"path": "/category-a", "parent_path": "/"})
assert retrieved_catalogue_categories == []
Loading

0 comments on commit fc60e2d

Please sign in to comment.