diff --git a/AUTHORS b/AUTHORS index 1d724718..88d4bbe1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -243,3 +243,4 @@ that much better: * Victor Varvaryuk * Stanislav Kaledin (https://github.com/sallyruthstruik) * Dmitry Yantsen (https://github.com/mrTable) + * Erdenezul Batmunkh (https://github.com/erdenezul) diff --git a/tests/document/instance.py b/tests/document/instance.py index 37bbe337..1456f0a1 100644 --- a/tests/document/instance.py +++ b/tests/document/instance.py @@ -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() diff --git a/tests/queryset/modify.py b/tests/queryset/modify.py index 607937f6..4bda9718 100644 --- a/tests/queryset/modify.py +++ b/tests/queryset/modify.py @@ -1,6 +1,6 @@ import unittest -from mongoengine import connect, Document, IntField +from mongoengine import connect, Document, IntField, StringField, ListField __all__ = ("FindAndModifyTest",) @@ -94,6 +94,29 @@ class FindAndModifyTest(unittest.TestCase): self.assertEqual(old_doc.to_mongo(), {"_id": 1}) self.assertDbEqual([{"_id": 0, "value": 0}, {"_id": 1, "value": -1}]) + def test_modify_with_push(self): + class BlogPost(Document): + id = StringField(primary_key=True) + tags = ListField(StringField()) + + BlogPost.drop_collection() + + BlogPost(id="ABC").save() + BlogPost(id="BCD").save() + blog = BlogPost.objects(id="ABC").modify(push__tags="code") + + self.assertEqual(blog.to_mongo(), {"_id": "ABC", "tags": []}) + docs = [{"_id": "ABC", "tags":["code"]}, {"_id": "BCD", "tags":[]}] + self.assertEqual(list(BlogPost._collection.find().sort("id")), docs) + + another_blog = BlogPost.objects(id="BCD").modify(push__tags="java") + self.assertEqual(another_blog.to_mongo(), {"_id": "BCD", "tags": []}) + another_blog = BlogPost.objects(id="BCD").modify(push__tags__0=["python"]) + self.assertEqual(another_blog.to_mongo(), {"_id": "BCD", "tags": ["java"]}) + docs = [{"_id": "ABC", "tags":["code"]}, + {"_id": "BCD", "tags":["python", "java"]}] + self.assertEqual(list(BlogPost._collection.find().sort("id")), docs) + if __name__ == '__main__': unittest.main()