Skip to content

Commit

Permalink
feat: Add unit tests for task management functions and implement task…
Browse files Browse the repository at this point in the history
… model tests
  • Loading branch information
jmcerrejon committed Nov 17, 2024
1 parent dfe1fda commit ae9746e
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 16 deletions.
116 changes: 100 additions & 16 deletions tests/tasks/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@

import src.tasks.models as models
from src.tasks import schemas
from src.tasks.controllers import create_task, get_task, get_task_list, update_task
from src.tasks.controllers import (
create_task,
delete_task,
get_task,
get_task_list,
update_task,
)


def test_get_task_list_success():
# Arrange
session = MagicMock(spec=Session)
task_list = [
models.Task(id=1, task="Test Task 1"),
models.Task(id=2, task="Test Task 2"),
]
session.query().all.return_value = task_list

# Act
result = get_task_list(session)

# Assert
assert result == task_list
session.query().all.assert_called_once()


def test_get_task_list_exception():
# Arrange
session = MagicMock(spec=Session)
session.query().all.side_effect = Exception("Database error")

# Act & Assert
with pytest.raises(HTTPException) as exc_info:
get_task_list(session)

Expand All @@ -41,28 +42,111 @@ def test_get_task_list_exception():


def test_get_task_success():
# Arrange
session = MagicMock(spec=Session)
task = models.Task(id=1, task="Test Task")
session.query().get.return_value = task
session.get.return_value = task

# Act
result = get_task(1, session)

# Assert
assert result == schemas.OkResponse(**task.__dict__)
session.query().get.assert_called_once_with(1)
assert isinstance(result, schemas.OkResponse)
assert result.id == 1
assert result.task == "Test Task"
session.get.assert_called_once_with(models.Task, 1)


def test_get_task_not_found():
# Arrange
session = MagicMock(spec=Session)
session.query().get.return_value = None
session.get.return_value = None

# Act & Assert
with pytest.raises(HTTPException) as exc_info:
get_task(1, session)

assert exc_info.value.status_code == 404
session.get.assert_called_once_with(models.Task, 1)


def test_create_task_success():
session = MagicMock(spec=Session)
task_create = schemas.TaskCreate(task="New Task")

session.add.return_value = None
session.commit.return_value = None
session.refresh.return_value = None
session.refresh.side_effect = lambda x: setattr(x, "id", 1)

result = create_task(task_create, session)

assert isinstance(result, schemas.OkResponse)
assert result.id == 1
assert result.task == "New Task"
session.add.assert_called_once()
session.commit.assert_called_once()
session.refresh.assert_called_once()


def test_create_task_exception():
session = MagicMock(spec=Session)
task_create = schemas.TaskCreate(task="New Task")

session.add.side_effect = Exception("Database error")

with pytest.raises(HTTPException) as exc_info:
create_task(task_create, session)

assert exc_info.value.status_code == 500
assert exc_info.value.detail == "Error creating task: Database error"
session.add.assert_called_once()
session.commit.assert_not_called()
session.refresh.assert_not_called()


def test_update_task_success():
session = MagicMock(spec=Session)
task_db = models.Task(id=1, task="Old Task")
session.get.return_value = task_db

result = update_task(1, "Updated Task", session)

assert result.task == "Updated Task"
session.get.assert_called_once_with(models.Task, 1)
session.commit.assert_called_once()


def test_update_task_not_found():
session = MagicMock(spec=Session)
session.get.return_value = None

with pytest.raises(HTTPException) as exc_info:
update_task(1, "Updated Task", session)

assert exc_info.value.status_code == 404
assert exc_info.value.detail == "Task with id 1 not found"
session.get.assert_called_once_with(models.Task, 1)
session.commit.assert_not_called()


def test_delete_task_success():
session = MagicMock(spec=Session)
task_db = models.Task(id=1, task="Test Task")
session.get.return_value = task_db

result = delete_task(1, session)

assert result is None
session.get.assert_called_once_with(models.Task, 1)
session.delete.assert_called_once_with(task_db)
session.commit.assert_called_once()


def test_delete_task_not_found():
session = MagicMock(spec=Session)
session.get.return_value = None

with pytest.raises(HTTPException) as exc_info:
delete_task(1, session)

assert exc_info.value.status_code == 404
assert exc_info.value.detail == "Task with id 1 not found"
session.query().get.assert_called_once_with(1)
session.get.assert_called_once_with(models.Task, 1)
session.delete.assert_not_called()
session.commit.assert_not_called()
37 changes: 37 additions & 0 deletions tests/tasks/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from src.tasks.models import Base, Task

DATABASE_URL = "sqlite:///:memory:"


@pytest.fixture(scope="module")
def engine():
return create_engine(DATABASE_URL)


@pytest.fixture(scope="module")
def tables(engine):
Base.metadata.create_all(engine)
yield
Base.metadata.drop_all(engine)


@pytest.fixture(scope="module")
def session(engine, tables):
Session = sessionmaker(bind=engine)
session = Session()
yield session
session.close()


def test_task_model(session):
new_task = Task(task="Test Task")
session.add(new_task)
session.commit()

retrieved_task = session.query(Task).filter_by(task="Test Task").first()
assert retrieved_task is not None
assert retrieved_task.task == "Test Task"
36 changes: 36 additions & 0 deletions tests/tasks/test_schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest
from pydantic import ValidationError

from src.tasks.schemas import OkResponse, Task, TaskCreate


def test_ok_response():
response = OkResponse(id=1, task="Test Task")
assert response.id == 1
assert response.task == "Test Task"


def test_ok_response_invalid():
with pytest.raises(ValidationError):
OkResponse(id="one", task="Test Task")


def test_task_create():
task_create = TaskCreate(task="New Task")
assert task_create.task == "New Task"


def test_task_create_invalid():
with pytest.raises(ValidationError):
TaskCreate(task=123)


def test_task():
task = Task(id=1, task="Existing Task")
assert task.id == 1
assert task.task == "Existing Task"


def test_task_invalid():
with pytest.raises(ValidationError):
Task(id="one", task="Existing Task")

0 comments on commit ae9746e

Please sign in to comment.