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

@ -829,25 +829,20 @@ class InstanceTest(unittest.TestCase):
self.assertDbEqual([dict(other_doc.to_mongo()), dict(doc.to_mongo())])
@needs_mongodb_v26
def test_modity_push_position(self):
def test_modify_with_positional_push(self):
class BlogPost(Document):
slug = 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()
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())]
# Assert same order of the list items is maintained in the db
self.assertEqual(
list(BlogPost._get_collection().find().sort("id")),
sorted(docs, key=lambda doc: doc["_id"]))
BlogPost._get_collection().find_one({'_id': post.pk})['tags'],
['code', 'mongo', 'python']
)
def test_save(self):
"""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.reload()
self.assertEqual(blog.tags[0], "mongodb")
self.assertEqual(blog.tags[2], "python")
self.assertEqual(blog.tags, ['mongodb', 'code', 'python'])
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__0=["mongodb", "python"])
post.reload()
self.assertEqual(post.tags[0], "mongodb")
self.assertEqual(post.tags[1], "python")
self.assertEqual(post.tags[2], "code")
self.assertEqual(post.tags, ['mongodb', 'python', 'code'])
BlogPost.objects.filter(id=post.id).update(set__tags__2="java")
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):
"""Ensure that the 'pull' update operation works correctly.