use explicit tests and fix unneccessary indent #1565

This commit is contained in:
Erdenezul Batmunkh 2017-07-13 22:59:21 +08:00
parent 7311895894
commit 3dcc9bc143
3 changed files with 13 additions and 21 deletions

View File

@ -288,7 +288,7 @@ def update(_doc_cls=None, **update):
value = [field.prepare_query_value(op, v) for v in value] value = [field.prepare_query_value(op, v) for v in value]
elif op in (None, 'set', 'push', 'pull'): elif op in (None, 'set', 'push', 'pull'):
if field.required or value is not None: if field.required or value is not None:
value = field.prepare_query_value(op, value) value = field.prepare_query_value(op, value)
elif op in ('pushAll', 'pullAll'): elif op in ('pushAll', 'pullAll'):
value = [field.prepare_query_value(op, v) for v in value] value = [field.prepare_query_value(op, v) for v in value]
elif op in ('addToSet', 'setOnInsert'): elif op in ('addToSet', 'setOnInsert'):

View File

@ -829,25 +829,20 @@ class InstanceTest(unittest.TestCase):
self.assertDbEqual([dict(other_doc.to_mongo()), dict(doc.to_mongo())]) self.assertDbEqual([dict(other_doc.to_mongo()), dict(doc.to_mongo())])
@needs_mongodb_v26 @needs_mongodb_v26
def test_modity_push_position(self): def test_modify_with_positional_push(self):
class BlogPost(Document): class BlogPost(Document):
slug = StringField()
tags = ListField(StringField()) tags = ListField(StringField())
other_blog = BlogPost(slug="ABC", tags=["code", "java", "python"]).save() post = BlogPost.objects.create(tags=['python'])
self.assertEqual(post.tags, ['python'])
post.modify(push__tags__0=['code', 'mongo'])
self.assertEqual(post.tags, ['code', 'mongo', 'python'])
blog = BlogPost(slug="ABC", tags=["python"]).save() # Assert same order of the list items is maintained in the db
blog_copy = blog._from_son(blog.to_mongo())
assert blog.modify(push__tags__0=["code", "java"])
blog_copy.tags = ["code", "java", "python"]
assert blog.to_json() == blog_copy.to_json()
assert blog._get_changed_fields() == []
docs = [dict(other_blog.to_mongo()), dict(blog.to_mongo())]
self.assertEqual( self.assertEqual(
list(BlogPost._get_collection().find().sort("id")), BlogPost._get_collection().find_one({'_id': post.pk})['tags'],
sorted(docs, key=lambda doc: doc["_id"])) ['code', 'mongo', 'python']
)
def test_save(self): def test_save(self):
"""Ensure that a document may be saved in the database.""" """Ensure that a document may be saved in the database."""
@ -3186,8 +3181,7 @@ class InstanceTest(unittest.TestCase):
blog.update(push__tags__0=["mongodb", "code"]) blog.update(push__tags__0=["mongodb", "code"])
blog.reload() blog.reload()
self.assertEqual(blog.tags[0], "mongodb") self.assertEqual(blog.tags, ['mongodb', 'code', 'python'])
self.assertEqual(blog.tags[2], "python")
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1919,13 +1919,11 @@ class QuerySetTest(unittest.TestCase):
BlogPost.objects.filter(id=post.id).update(push__tags="code") BlogPost.objects.filter(id=post.id).update(push__tags="code")
BlogPost.objects.filter(id=post.id).update(push__tags__0=["mongodb", "python"]) BlogPost.objects.filter(id=post.id).update(push__tags__0=["mongodb", "python"])
post.reload() post.reload()
self.assertEqual(post.tags[0], "mongodb") self.assertEqual(post.tags, ['mongodb', 'python', 'code'])
self.assertEqual(post.tags[1], "python")
self.assertEqual(post.tags[2], "code")
BlogPost.objects.filter(id=post.id).update(set__tags__2="java") BlogPost.objects.filter(id=post.id).update(set__tags__2="java")
post.reload() post.reload()
self.assertEqual(post.tags[2], "java") self.assertEqual(post.tags, ['mongodb', 'python', 'java'])
def test_update_push_and_pull_add_to_set(self): def test_update_push_and_pull_add_to_set(self):
"""Ensure that the 'pull' update operation works correctly. """Ensure that the 'pull' update operation works correctly.