add tests for push operator #1565
This commit is contained in:
@@ -22,6 +22,8 @@ from mongoengine.queryset import NULLIFY, Q
|
||||
from mongoengine.context_managers import switch_db, query_counter
|
||||
from mongoengine import signals
|
||||
|
||||
from tests.utils import needs_mongodb_v26
|
||||
|
||||
TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__),
|
||||
'../fields/mongoengine.png')
|
||||
|
||||
@@ -775,6 +777,7 @@ class InstanceTest(unittest.TestCase):
|
||||
|
||||
self.assertDbEqual([dict(doc.to_mongo())])
|
||||
|
||||
|
||||
def test_modify_invalid_query(self):
|
||||
doc1 = self.Person(name="bob", age=10).save()
|
||||
doc2 = self.Person(name="jim", age=20).save()
|
||||
@@ -826,6 +829,27 @@ class InstanceTest(unittest.TestCase):
|
||||
|
||||
self.assertDbEqual([dict(other_doc.to_mongo()), dict(doc.to_mongo())])
|
||||
|
||||
@needs_mongodb_v26
|
||||
def test_modity_push_position(self):
|
||||
class BlogPost(Document):
|
||||
slug = StringField()
|
||||
tags = ListField(StringField())
|
||||
|
||||
other_blog = BlogPost(slug="ABC", tags=["code", "java", "python"]).save()
|
||||
|
||||
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())]
|
||||
self.assertEqual(
|
||||
list(BlogPost._get_collection().find().sort("id")),
|
||||
sorted(docs, key=lambda doc: doc["_id"]))
|
||||
|
||||
def test_save(self):
|
||||
"""Ensure that a document may be saved in the database."""
|
||||
|
||||
@@ -3149,6 +3173,23 @@ class InstanceTest(unittest.TestCase):
|
||||
|
||||
person.update(set__height=2.0)
|
||||
|
||||
@needs_mongodb_v26
|
||||
def test_push_with_position(self):
|
||||
"""Ensure that push with position works properly for an instance."""
|
||||
class BlogPost(Document):
|
||||
slug = StringField()
|
||||
tags = ListField(StringField())
|
||||
|
||||
blog = BlogPost()
|
||||
blog.slug = "ABC"
|
||||
blog.tags = ["python"]
|
||||
blog.save()
|
||||
|
||||
blog.update(push__tags__0=["mongodb", "code"])
|
||||
blog.reload()
|
||||
self.assertEqual(blog.tags[0], "mongodb")
|
||||
self.assertEqual(blog.tags[2], "python")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user