From bb1089e03d3d2a4e4e7929a377e917d056f28517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastien=20G=C3=A9rard?= Date: Thu, 16 May 2019 22:31:24 +0200 Subject: [PATCH] Improve coverage in fields test --- tests/fields/test_cached_reference_field.py | 11 +++++++---- tests/fields/test_datetime_field.py | 9 +++++++++ tests/fields/test_lazy_reference_field.py | 17 +++++++++++++++++ tests/fields/test_long_field.py | 4 ++-- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/tests/fields/test_cached_reference_field.py b/tests/fields/test_cached_reference_field.py index 989cea6d..470ecc5d 100644 --- a/tests/fields/test_cached_reference_field.py +++ b/tests/fields/test_cached_reference_field.py @@ -208,10 +208,7 @@ class TestCachedReferenceField(MongoDBTestCase): ('pj', "PJ") ) name = StringField() - tp = StringField( - choices=TYPES - ) - + tp = StringField(choices=TYPES) father = CachedReferenceField('self', fields=('tp',)) Person.drop_collection() @@ -222,6 +219,9 @@ class TestCachedReferenceField(MongoDBTestCase): a2 = Person(name='Wilson Junior', tp='pf', father=a1) a2.save() + a2 = Person.objects.with_id(a2.id) + self.assertEqual(a2.father.tp, a1.tp) + self.assertEqual(dict(a2.to_mongo()), { "_id": a2.pk, "name": u"Wilson Junior", @@ -374,6 +374,9 @@ class TestCachedReferenceField(MongoDBTestCase): self.assertEqual(o.to_mongo()['animal']['tag'], 'heavy') self.assertEqual(o.to_mongo()['animal']['owner']['t'], 'u') + # Check to_mongo with fields + self.assertNotIn('animal', o.to_mongo(fields=['person'])) + # counts Ocorrence(person="teste 2").save() Ocorrence(person="teste 3").save() diff --git a/tests/fields/test_datetime_field.py b/tests/fields/test_datetime_field.py index c6253043..5af6a011 100644 --- a/tests/fields/test_datetime_field.py +++ b/tests/fields/test_datetime_field.py @@ -172,6 +172,9 @@ class TestDateTimeField(MongoDBTestCase): log.time = datetime.datetime.now().isoformat(' ') log.validate() + log.time = '2019-05-16 21:42:57.897847' + log.validate() + if dateutil: log.time = datetime.datetime.now().isoformat('T') log.validate() @@ -180,6 +183,12 @@ class TestDateTimeField(MongoDBTestCase): self.assertRaises(ValidationError, log.validate) log.time = 'ABC' self.assertRaises(ValidationError, log.validate) + log.time = '2019-05-16 21:GARBAGE:12' + self.assertRaises(ValidationError, log.validate) + log.time = '2019-05-16 21:42:57.GARBAGE' + self.assertRaises(ValidationError, log.validate) + log.time = '2019-05-16 21:42:57.123.456' + self.assertRaises(ValidationError, log.validate) class TestDateTimeTzAware(MongoDBTestCase): diff --git a/tests/fields/test_lazy_reference_field.py b/tests/fields/test_lazy_reference_field.py index d8031409..b10506e7 100644 --- a/tests/fields/test_lazy_reference_field.py +++ b/tests/fields/test_lazy_reference_field.py @@ -508,6 +508,23 @@ class TestGenericLazyReferenceField(MongoDBTestCase): p = Ocurrence.objects.get() self.assertIs(p.animal, None) + def test_generic_lazy_reference_accepts_string_instead_of_class(self): + class Animal(Document): + name = StringField() + tag = StringField() + + class Ocurrence(Document): + person = StringField() + animal = GenericLazyReferenceField('Animal') + + Animal.drop_collection() + Ocurrence.drop_collection() + + animal = Animal().save() + Ocurrence(animal=animal).save() + p = Ocurrence.objects.get() + self.assertEqual(p.animal, animal) + def test_generic_lazy_reference_embedded(self): class Animal(Document): name = StringField() diff --git a/tests/fields/test_long_field.py b/tests/fields/test_long_field.py index 4ab7403d..3f307809 100644 --- a/tests/fields/test_long_field.py +++ b/tests/fields/test_long_field.py @@ -39,9 +39,9 @@ class TestLongField(MongoDBTestCase): doc.value = -1 self.assertRaises(ValidationError, doc.validate) - doc.age = 120 + doc.value = 120 self.assertRaises(ValidationError, doc.validate) - doc.age = 'ten' + doc.value = 'ten' self.assertRaises(ValidationError, doc.validate) def test_long_ne_operator(self):