Skip to content

Commit

Permalink
add todo service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaniyaps committed Jun 29, 2024
1 parent 17984f2 commit 9450006
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 17 deletions.
37 changes: 33 additions & 4 deletions server/pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test = [
"anyio>=4.4.0",
"pytest-pretty>=1.2.0",
"coverage>=7.5.4",
"polyfactory>=2.16.0",
]

[tool.pdm.scripts]
Expand Down
2 changes: 2 additions & 0 deletions server/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import tests.plugins

from .factories import *

pytest_plugins = [
"anyio",
"sqlalchemy_pytest.database",
Expand Down
12 changes: 12 additions & 0 deletions server/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from app.todos.models import Todo
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory
from polyfactory.pytest_plugin import register_fixture


class BaseFactory(SQLAlchemyFactory):
__is_base_factory__ = True


@register_fixture
class TodoFactory(BaseFactory):
__model__ = Todo
27 changes: 15 additions & 12 deletions server/tests/unit/todos/test_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ async def multiple_todos(todo_repo: TodoRepo) -> list[Todo]:
return [todo_1, todo_2, todo_3, todo_4]


@pytest.fixture
async def __seed_todos(session: AsyncSession) -> None:
todos = [
Todo(
content=f"Todo {i}",
id=i + 1,
)
for i in range(50)
]
session.add_all(todos)
await session.commit()


async def test_create_todo(todo_repo: TodoRepo) -> None:
"""Ensure we can create a new todo."""
todo = await todo_repo.create(
Expand Down Expand Up @@ -57,19 +70,9 @@ async def test_get_multiple_todos_by_ids(
assert retrieved == original


async def test_get_all_todos(todo_repo: TodoRepo, session: AsyncSession) -> None:
@pytest.mark.usefixtures("__seed_todos")
async def test_get_all_todos(todo_repo: TodoRepo) -> None:
"""Ensure we can get all todos."""
# Create multiple todos for testing
todos = [
Todo(
content=f"Todo {i}",
id=i + 1,
)
for i in range(50)
]
session.add_all(todos)
await session.commit()

pagination_limit = 10

# Test fetching the first 10 todos
Expand Down
59 changes: 58 additions & 1 deletion server/tests/unit/todos/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from app.todos.repositories import TodoRepo
from app.todos.services import TodoService
from result import Err, Ok
from sqlalchemy.ext.asyncio import AsyncSession


@pytest.fixture
Expand All @@ -13,6 +14,19 @@ async def todo(todo_repo: TodoRepo) -> Todo:
)


@pytest.fixture
async def __seed_todos(session: AsyncSession) -> None:
todos = [
Todo(
content=f"Todo {i}",
id=i + 1,
)
for i in range(50)
]
session.add_all(todos)
await session.commit()


async def test_create_todo(todo_service: TodoService) -> None:
"""Ensure we can create a new todo."""
result = await todo_service.create(
Expand All @@ -26,9 +40,52 @@ async def test_create_todo(todo_service: TodoService) -> None:
assert result.ok_value.updated_at is None


@pytest.mark.usefixtures("__seed_todos")
async def test_get_all_todos(todo_service: TodoService) -> None:
"""Ensure we can get all todos."""
pass
pagination_limit = 10

# Test fetching the first 10 todos
paginated_result = await todo_service.get_all(first=pagination_limit)
assert len(paginated_result.entities) == pagination_limit
assert paginated_result.page_info.has_next_page is True
assert paginated_result.page_info.has_previous_page is False
assert paginated_result.page_info.start_cursor == 50
assert paginated_result.page_info.end_cursor == 41

# Test fetching the next 10 todos
paginated_result = await todo_service.get_all(first=pagination_limit, after=49)
assert len(paginated_result.entities) == pagination_limit
assert paginated_result.page_info.has_next_page is True
assert paginated_result.page_info.has_previous_page is True
assert paginated_result.page_info.start_cursor == 48
assert paginated_result.page_info.end_cursor == 39

# Test fetching the last 10 todos
paginated_result = await todo_service.get_all(last=pagination_limit)
assert len(paginated_result.entities) == pagination_limit
assert paginated_result.page_info.has_next_page is False
assert paginated_result.page_info.has_previous_page is True
assert paginated_result.page_info.start_cursor == 10
assert paginated_result.page_info.end_cursor == 1

# Test fetching 10 todos before the last one
paginated_result = await todo_service.get_all(last=pagination_limit, before=1)
assert len(paginated_result.entities) == pagination_limit
assert paginated_result.page_info.has_next_page is True
assert paginated_result.page_info.has_previous_page is True
assert paginated_result.page_info.start_cursor == 11
assert paginated_result.page_info.end_cursor == 2

# Test fetching all todos with a limit higher than the total count
pagination_limit = 75

paginated_result = await todo_service.get_all(first=pagination_limit)
assert len(paginated_result.entities) < pagination_limit
assert paginated_result.page_info.has_next_page is False
assert paginated_result.page_info.has_previous_page is False
assert paginated_result.page_info.start_cursor == 50
assert paginated_result.page_info.end_cursor == 1


async def test_delete_todo(
Expand Down

0 comments on commit 9450006

Please sign in to comment.