From 70088704e202c38ed7265cbb1929a38d383c8a3d Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Sun, 10 Sep 2017 01:37:17 +0900 Subject: [PATCH 1/2] add tests to increase code coverage --- tests/fields/fields.py | 10 +++++++++- tests/queryset/geo.py | 4 ++++ tests/queryset/queryset.py | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 7a0ccc25..dbf148e1 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -334,7 +334,7 @@ class FieldTest(MongoDBTestCase): def test_string_validation(self): """Ensure that invalid values cannot be assigned to string fields.""" class Person(Document): - name = StringField(max_length=20) + name = StringField(max_length=20, min_length=5) userid = StringField(r'[0-9a-z_]+$') person = Person(name=34) @@ -352,6 +352,10 @@ class FieldTest(MongoDBTestCase): person = Person(name='Name that is more than twenty characters') self.assertRaises(ValidationError, person.validate) + # Test max length validation on name + person = Person(name='aa') + self.assertRaises(ValidationError, person.validate) + person.name = 'Shorter name' person.validate() @@ -437,6 +441,10 @@ class FieldTest(MongoDBTestCase): doc.age = 'ten' self.assertRaises(ValidationError, doc.validate) + # Test max_value validation + doc.value = 200 + self.assertRaises(ValidationError, doc.validate) + def test_float_validation(self): """Ensure that invalid values cannot be assigned to float fields. """ diff --git a/tests/queryset/geo.py b/tests/queryset/geo.py index 38c0377e..acfd9364 100644 --- a/tests/queryset/geo.py +++ b/tests/queryset/geo.py @@ -429,6 +429,10 @@ class GeoQueriesTest(MongoDBTestCase): roads = Road.objects.filter(line__geo_within=polygon).count() self.assertEqual(1, roads) + sphere = [[-1, 42,], 2] + roads = Road.objects.filter(line__geo_within_sphere=sphere).count() + self.assertEqual(0, roads) + roads = Road.objects.filter(line__geo_within={"$geometry": polygon}).count() self.assertEqual(1, roads) diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py index c78ed985..5fc73a54 100644 --- a/tests/queryset/queryset.py +++ b/tests/queryset/queryset.py @@ -1882,6 +1882,12 @@ class QuerySetTest(unittest.TestCase): post.reload() self.assertTrue('mongo' in post.tags) + # Push with arrays + BlogPost.objects.update(push__tags=['python', 'scala']) + post.reload() + self.assertTrue('python' in post.tags) + self.assertTrue('scala' in post.tags) + BlogPost.objects.update_one(push_all__tags=['db', 'nosql']) post.reload() self.assertTrue('db' in post.tags and 'nosql' in post.tags) From ba99190f535ba16ed3c9d6fe5acfc4836c64b48d Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Sun, 10 Sep 2017 13:09:20 +0900 Subject: [PATCH 2/2] add tests to increase coverage --- tests/fields/fields.py | 26 ++++++++++++++++++++++++++ tests/queryset/queryset.py | 4 ++++ 2 files changed, 30 insertions(+) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index dbf148e1..d25f2e25 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -312,6 +312,27 @@ class FieldTest(MongoDBTestCase): self.assertEqual(1, TestDocument.objects(long_fld__ne=None).count()) + def test_callable_validation(self): + """Ensure that callable validation works""" + def check_even(value): + return value % 2 == 0 + + class Order(Document): + number = IntField(validation=check_even) + + Order.drop_collection() + + order = Order(number=3) + self.assertRaises(ValidationError, order.validate) + + class User(Document): + name = StringField(validation=1) + + User.drop_collection() + + user = User(name='test') + self.assertRaises(ValidationError, user.validate) + def test_object_id_validation(self): """Ensure that invalid values cannot be assigned to an ObjectIdField. @@ -527,6 +548,11 @@ class FieldTest(MongoDBTestCase): class User(Document): name = StringField(db_field='name\0') + # db field should be a string + with self.assertRaises(TypeError): + class User(Document): + name = StringField(db_field=1) + def test_decimal_comparison(self): class Person(Document): money = DecimalField() diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py index 5fc73a54..9c61513a 100644 --- a/tests/queryset/queryset.py +++ b/tests/queryset/queryset.py @@ -1861,6 +1861,10 @@ class QuerySetTest(unittest.TestCase): post = BlogPost(name="Test Post", hits=5, tags=['test']) post.save() + BlogPost.objects.update(hits=11) + post.reload() + self.assertEqual(post.hits, 11) + BlogPost.objects.update(set__hits=10) post.reload() self.assertEqual(post.hits, 10)