Skip to content

Commit

Permalink
Add tests for base model Document (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
samarpan1738 committed Dec 16, 2024
1 parent ac141c5 commit e569ed6
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions todo/tests/unit/models/common/test_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from typing import ClassVar
from unittest import TestCase
from bson import ObjectId
from pydantic import Field
from todo.models.common.document import Document


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

class InvalidDocument(Document):
pass

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

def test_subclass_with_invalid_collection_name_type_raises_error(self):
with self.assertRaises(TypeError):

class InvalidDocument(Document):
collection_name: ClassVar[str] = 123

def test_subclass_with_valid_collection_name(self):
try:

class ValidDocument(Document):
collection_name: ClassVar[str] = "valid_collection"
except TypeError as e:
print(e)
self.fail("TypeError raised for valid Document subclass")

def test_id_field_alias(self):
class TestDocument(Document):
collection_name: ClassVar[str] = "test_collection"

obj_id = ObjectId()
doc = TestDocument.model_validate({"_id": obj_id})
self.assertEqual(doc.id, obj_id)
self.assertEqual(doc.model_dump(mode="json", by_alias=True)["_id"], str(obj_id))

def test_json_encoder_serializes_objectid(self):
class TestDocument(Document):
collection_name: ClassVar[str] = "test_collection"

obj_id = ObjectId()
doc = TestDocument(id=obj_id)
serialized = doc.model_dump_json()
self.assertIn(str(obj_id), serialized)

def test_populate_by_name_behavior(self):
class TestDocument(Document):
collection_name: ClassVar[str] = "test_collection"
field_one: str = Field(..., alias="fieldOne")

data = {"_id": ObjectId(), "fieldOne": "value"}
doc = TestDocument.model_validate(data)
self.assertEqual(doc.field_one, "value")
self.assertEqual(doc.model_dump(by_alias=True)["fieldOne"], "value")

0 comments on commit e569ed6

Please sign in to comment.