Skip to content

Commit 617cf29

Browse files
committed
Add exception unit tests.
1 parent 8a23852 commit 617cf29

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

tests/model_fields_/test_objectidfield.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from bson import ObjectId
2+
from django.core import exceptions
23
from django.test import SimpleTestCase
34

45
from django_mongodb.fields import ObjectIdField
@@ -19,5 +20,15 @@ def test_get_internal_type(self):
1920
def test_to_python(self):
2021
f = ObjectIdField()
2122
expected = ObjectId("1" * 24)
22-
self.assertEqual(f.to_python("1" * 24), expected)
23-
self.assertEqual(f.to_python(expected), expected)
23+
for value in ["1" * 24, ObjectId("1" * 24)]:
24+
with self.subTest(value=value):
25+
self.assertEqual(f.to_python(value), expected)
26+
with self.subTest(value=None):
27+
self.assertIsNone(f.to_python(None))
28+
29+
def test_to_python_invalid_value(self):
30+
for invalid_value in [3, "None", {}, []]:
31+
with self.subTest(invalid_value=invalid_value):
32+
msg = f"['“{invalid_value}” value must be an Object Id.']"
33+
with self.assertRaisesMessage(exceptions.ValidationError, msg):
34+
ObjectIdField().to_python(invalid_value)

0 commit comments

Comments
 (0)