Skip to content

Commit

Permalink
Add tests for task service
Browse files Browse the repository at this point in the history
  • Loading branch information
samarpan1738 committed Dec 22, 2024
1 parent f58e1b1 commit 683b8ee
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
39 changes: 39 additions & 0 deletions todo/tests/fixtures/task.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from todo.constants.task import TaskPriority
from todo.models.task import TaskModel
from todo.constants.task import TaskStatus
from todo.dto.task_dto import TaskDTO
from bson import ObjectId

tasks_db_data = [
Expand Down Expand Up @@ -37,3 +38,41 @@
]

tasks_models = [TaskModel(**data) for data in tasks_db_data]


task_dtos = [
TaskDTO(
id="672f7c5b775ee9f4471ff1dd",
displayId="#1",
title="created rest api",
priority=1,
status="TODO",
assignee={"id": "qMbT6M2GB65W7UHgJS4g", "name": "SYSTEM"},
isAcknowledged=False,
labels=[{"name": "Beginner Friendly", "color": "#fa1e4e"}],
isDeleted=False,
startedAt="2024-11-09T15:14:35.724000",
dueAt="2024-11-09T15:14:35.724000",
createdAt="2024-11-09T15:14:35.724000",
updatedAt="2024-10-18T15:55:14.802000Z",
createdBy={"id": "xQ1CkCncM8Novk252oAj", "name": "SYSTEM"},
updatedBy={"id": "Kn5N4Z3mdvpkv0HpqUCt", "name": "SYSTEM"},
),
TaskDTO(
id="674c726ca89aab38040cb964",
displayId="#1",
title="task 2",
priority=1,
status="TODO",
assignee={"id": "qMbT6M2GB65W7UHgJS4g", "name": "SYSTEM"},
isAcknowledged=True,
labels=[{"name": "Beginner Friendly", "color": "#fa1e4e"}],
isDeleted=False,
startedAt="2024-11-09T15:14:35.724000",
dueAt="2024-11-09T15:14:35.724000",
createdAt="2024-11-09T15:14:35.724000",
updatedAt="2024-10-18T15:55:14.802000Z",
createdBy={"id": "xQ1CkCncM8Novk252oAj", "name": "SYSTEM"},
updatedBy={"id": "Kn5N4Z3mdvpkv0HpqUCt", "name": "SYSTEM"},
),
]
1 change: 1 addition & 0 deletions todo/tests/unit/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Added this because without this file Django isn't able to auto detect the test files
77 changes: 77 additions & 0 deletions todo/tests/unit/services/test_task_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from unittest.mock import Mock, patch
from unittest import TestCase

from todo.dto.responses.get_tasks_response import GetTasksResponse
from todo.dto.responses.paginated_response import LinksData
from todo.dto.user_dto import UserDTO
from todo.services.task_service import TaskService
from todo.dto.task_dto import TaskDTO
from todo.tests.fixtures.task import tasks_models
from todo.tests.fixtures.label import label_models


class TaskServiceTests(TestCase):
@patch("todo.services.task_service.reverse_lazy", return_value="/v1/tasks")
def setUp(self, mock_reverse_lazy):
self.mock_reverse_lazy = mock_reverse_lazy

@patch("todo.services.task_service.TaskRepository.count")
@patch("todo.services.task_service.TaskRepository.list")
@patch("todo.services.task_service.LabelRepository.list_by_ids")
def test_get_tasks_returns_paginated_response(
self, mock_label_repo: Mock, mock_task_repo: Mock, mock_task_count: Mock
):
mock_task_repo.return_value = [tasks_models[0]]
mock_label_repo.return_value = label_models
mock_task_count.return_value = 5

response: GetTasksResponse = TaskService.get_tasks(2, 1)
self.assertIsInstance(response, GetTasksResponse)
self.assertEqual(len(response.tasks), 1)

self.assertIsInstance(response.links, LinksData)
self.assertEqual(response.links.next, f"{self.mock_reverse_lazy("tasks")}?page=3&limit=1")
self.assertEqual(response.links.prev, f"{self.mock_reverse_lazy("tasks")}?page=1&limit=1")

@patch("todo.services.task_service.TaskRepository.count")
@patch("todo.services.task_service.TaskRepository.list")
@patch("todo.services.task_service.LabelRepository.list_by_ids")
def test_get_tasks_doesnt_returns_prev_link_for_first_page(
self, mock_label_repo: Mock, mock_task_repo: Mock, mock_task_count: Mock
):
mock_task_repo.return_value = [tasks_models[0]]
mock_label_repo.return_value = label_models
mock_task_count.return_value = 10

response: GetTasksResponse = TaskService.get_tasks(1, 1)

self.assertIsNone(response.links.prev)

@patch("todo.services.task_service.TaskRepository.count")
def test_get_tasks_returns_zero_tasks_if_no_tasks_present(self, mock_task_count: Mock):
mock_task_count.return_value = 5

response: GetTasksResponse = TaskService.get_tasks(2, 10)
self.assertIsInstance(response, GetTasksResponse)
self.assertEqual(len(response.tasks), 0)
self.assertIsNone(response.links)

@patch("todo.services.task_service.LabelRepository.list_by_ids")
def test_prepare_task_dto_maps_model_to_dto(self, mock_label_repo: Mock):
task_model = tasks_models[0]
mock_label_repo.return_value = label_models

result: TaskDTO = TaskService.prepare_task_dto(task_model)

mock_label_repo.assert_called_once_with(task_model.labels)

self.assertIsInstance(result, TaskDTO)
self.assertEqual(result.id, str(task_model.id))

def test_prepare_user_dto_maps_model_to_dto(self):
user_id = tasks_models[0].assignee
result: UserDTO = TaskService.prepare_user_dto(user_id)

self.assertIsInstance(result, UserDTO)
self.assertEqual(result.id, user_id)
self.assertEqual(result.name, "SYSTEM")

0 comments on commit 683b8ee

Please sign in to comment.