Skip to content

Commit

Permalink
Add unit tests for mongo repository (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
samarpan1738 committed Dec 16, 2024
1 parent 15b068b commit d6ac0c8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions todo/tests/unit/repositories/__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
1 change: 1 addition & 0 deletions todo/tests/unit/repositories/common/__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
57 changes: 57 additions & 0 deletions todo/tests/unit/repositories/common/test_mongo_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from unittest import TestCase
from unittest.mock import MagicMock, patch
from todo.repositories.common.mongo_repository import MongoRepository
from todo_project.db.config import DatabaseManager


class MongoRepositoryTests(TestCase):
def test_subclass_without_collection_name_raises_error(self):
with self.assertRaises(TypeError) as context:

class InvalidRepository(MongoRepository):
pass

self.assertIn(
"Class InvalidRepository must define a static `collection_name` field as a string.", str(context.exception)
)

def test_subclass_with_invalid_collection_name_raises_error(self):
with self.assertRaises(TypeError) as context:

class InvalidRepository(MongoRepository):
collection_name = 123

self.assertIn(
"Class InvalidRepository must define a static `collection_name` field as a string.", str(context.exception)
)

def test_subclass_with_valid_collection_name_passes(self):
try:

class ValidRepository(MongoRepository):
collection_name = "valid_collection"
except TypeError:
self.fail("TypeError raised for a valid subclass with collection_name")

@patch.object(DatabaseManager, "get_collection")
def test_get_collection_initializes_collection(self, mock_get_collection):
class TestRepository(MongoRepository):
collection_name = "test_collection"

mock_get_collection.return_value = MagicMock()

collection = TestRepository.get_collection()
mock_get_collection.assert_called_once_with("test_collection")
self.assertEqual(TestRepository.collection, collection)

@patch.object(DatabaseManager, "get_collection")
def test_get_collection_uses_cached_collection(self, mock_get_collection):
class TestRepository(MongoRepository):
collection_name = "test_collection"

mock_get_collection.return_value = MagicMock()

TestRepository.get_collection()
TestRepository.get_collection()

mock_get_collection.assert_called_once_with("test_collection")

0 comments on commit d6ac0c8

Please sign in to comment.