forked from Real-Dev-Squad/todo-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for mongo repository (#5)
- Loading branch information
1 parent
15b068b
commit d6ac0c8
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
todo/tests/unit/repositories/common/test_mongo_repository.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |