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.
- Loading branch information
1 parent
4705106
commit ac141c5
Showing
3 changed files
with
46 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 |
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,44 @@ | ||
from unittest import TestCase | ||
from bson import ObjectId | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from todo.models.common.pyobjectid import PyObjectId | ||
|
||
|
||
class PyObjectIdTests(TestCase): | ||
def test_validate_valid_objectid(self): | ||
valid_id = str(ObjectId()) | ||
validated = PyObjectId.validate(valid_id) | ||
self.assertEqual(validated, ObjectId(valid_id)) | ||
|
||
def test_validate_invalid_objectid(self): | ||
invalid_id = "invalid_objectid" | ||
with self.assertRaises(ValueError) as context: | ||
PyObjectId.validate(invalid_id) | ||
self.assertIn(f"Invalid ObjectId: {invalid_id}", str(context.exception)) | ||
|
||
def test_validate_none(self): | ||
self.assertIsNone(PyObjectId.validate(None)) | ||
|
||
def test_pydantic_json_schema(self): | ||
field_schema = {} | ||
PyObjectId.__get_pydantic_json_schema__(field_schema) | ||
self.assertEqual(field_schema["type"], "string") | ||
|
||
def test_integration_with_pydantic_model(self): | ||
class TestModel(BaseModel): | ||
id: PyObjectId | ||
|
||
valid_id = str(ObjectId()) | ||
instance = TestModel(id=valid_id) | ||
self.assertEqual(instance.id, ObjectId(valid_id)) | ||
|
||
invalid_id = "invalid_objectid" | ||
with self.assertRaises(ValidationError) as context: | ||
TestModel(id=invalid_id) | ||
self.assertIn(f"Invalid ObjectId: {invalid_id}", str(context.exception)) | ||
|
||
try: | ||
TestModel(id=None) | ||
except ValidationError: | ||
self.fail("ValidationError raised for None id") |