Skip to content

Commit

Permalink
Add tests for task repository
Browse files Browse the repository at this point in the history
  • Loading branch information
samarpan1738 committed Dec 17, 2024
1 parent 3001c56 commit c9dc797
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions todo/tests/fixtures/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
"updatedBy": "qMbT6M2GB65W7UHgJS4g",
},
]

tasks_models = [TaskModel(**data) for data in tasks_db_data]
51 changes: 51 additions & 0 deletions todo/tests/unit/repositories/test_task_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from unittest import TestCase
from unittest.mock import patch, MagicMock
from pymongo.collection import Collection
from todo.models.task import TaskModel
from todo.repositories.task_repository import TaskRepository
from todo.tests.fixtures.task import tasks_db_data


class TaskRepositoryTests(TestCase):
def setUp(self):
self.task_data = tasks_db_data

self.patcher_get_collection = patch("todo.repositories.task_repository.TaskRepository.get_collection")
self.mock_get_collection = self.patcher_get_collection.start()
self.mock_collection = MagicMock(spec=Collection)
self.mock_get_collection.return_value = self.mock_collection

def tearDown(self):
self.patcher_get_collection.stop()

def test_list_applies_pagination_correctly(self):
self.mock_collection.find.return_value.skip.return_value.limit.return_value = self.task_data

page = 1
limit = 10
result = TaskRepository.list(page, limit)

self.assertEqual(len(result), len(self.task_data))
self.assertTrue(all(isinstance(task, TaskModel) for task in result))

self.mock_collection.find.assert_called_once()
self.mock_collection.find.return_value.skip.assert_called_once_with(0)
self.mock_collection.find.return_value.skip.return_value.limit.assert_called_once_with(limit)

def test_list_returns_empty_list_for_no_tasks(self):
self.mock_collection.find.return_value.skip.return_value.limit.return_value = []

result = TaskRepository.list(2, 10)

self.assertEqual(result, [])
self.mock_collection.find.assert_called_once()
self.mock_collection.find.return_value.skip.assert_called_once_with(10)
self.mock_collection.find.return_value.skip.return_value.limit.assert_called_once_with(10)

def test_count_returns_total_task_count(self):
self.mock_collection.count_documents.return_value = 42

result = TaskRepository.count()

self.assertEqual(result, 42)
self.mock_collection.count_documents.assert_called_once_with({})

0 comments on commit c9dc797

Please sign in to comment.