From e569ed6ff1609dd9a8023b5ce243edfcd6bfe6b3 Mon Sep 17 00:00:00 2001 From: Samarpan Harit Date: Mon, 9 Dec 2024 22:09:00 +0530 Subject: [PATCH] Add tests for base model Document (#5) --- .../tests/unit/models/common/test_document.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 todo/tests/unit/models/common/test_document.py diff --git a/todo/tests/unit/models/common/test_document.py b/todo/tests/unit/models/common/test_document.py new file mode 100644 index 0000000..6a2b10f --- /dev/null +++ b/todo/tests/unit/models/common/test_document.py @@ -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")