Add clear comment and tests for positional push #1565
This commit is contained in:
parent
433f10ef93
commit
34fca9d6f5
@ -336,10 +336,11 @@ def update(_doc_cls=None, **update):
|
|||||||
elif op == 'addToSet' and isinstance(value, list):
|
elif op == 'addToSet' and isinstance(value, list):
|
||||||
value = {key: {'$each': value}}
|
value = {key: {'$each': value}}
|
||||||
elif op == 'push':
|
elif op == 'push':
|
||||||
if parts[-1].isdigit() and op == 'push':
|
if parts[-1].isdigit():
|
||||||
key = parts[0]
|
key = parts[0]
|
||||||
position = int(parts[-1])
|
position = int(parts[-1])
|
||||||
# position modifier must appear with each.
|
# $position expects an iterable. If pushing a single value,
|
||||||
|
# wrap it in a list.
|
||||||
if not isinstance(value, (set, tuple, list)):
|
if not isinstance(value, (set, tuple, list)):
|
||||||
value = [value]
|
value = [value]
|
||||||
value = {key: {'$each': value, '$position': position}}
|
value = {key: {'$each': value, '$position': position}}
|
||||||
|
@ -99,26 +99,33 @@ class FindAndModifyTest(unittest.TestCase):
|
|||||||
@needs_mongodb_v26
|
@needs_mongodb_v26
|
||||||
def test_modify_with_push(self):
|
def test_modify_with_push(self):
|
||||||
class BlogPost(Document):
|
class BlogPost(Document):
|
||||||
id = StringField(primary_key=True)
|
|
||||||
tags = ListField(StringField())
|
tags = ListField(StringField())
|
||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
BlogPost(id="ABC").save()
|
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": []})
|
# Push a new tag via modify with new=False (default).
|
||||||
docs = [{"_id": "ABC", "tags":["code"]}, {"_id": "BCD", "tags":[]}]
|
blog = BlogPost(pk='ABC').modify(push__tags='code')
|
||||||
self.assertEqual(list(BlogPost._collection.find().sort("id")), docs)
|
self.assertEqual(blog.tags, [])
|
||||||
|
blog.reload()
|
||||||
|
self.assertEqual(blog.tags, ['code'])
|
||||||
|
|
||||||
another_blog = BlogPost.objects(id="BCD").modify(push__tags="java")
|
# Push a new tag via modify with new=True.
|
||||||
self.assertEqual(another_blog.to_mongo(), {"_id": "BCD", "tags": []})
|
blog = BlogPost.objects(pk='ABC').modify(push__tags='java', new=True)
|
||||||
another_blog = BlogPost.objects(id="BCD").modify(push__tags__0=["python"])
|
self.assertEqual(blog.tags, ['code', 'java'])
|
||||||
self.assertEqual(another_blog.to_mongo(), {"_id": "BCD", "tags": ["java"]})
|
|
||||||
docs = [{"_id": "ABC", "tags":["code"]},
|
# Push a new tag with a positional argument.
|
||||||
{"_id": "BCD", "tags":["python", "java"]}]
|
blog = BlogPost.objects(pk='ABC').modify(
|
||||||
self.assertEqual(list(BlogPost._collection.find().sort("id")), docs)
|
push__tags__0='python',
|
||||||
|
new=True)
|
||||||
|
self.assertEqual(blog.tags, ['python', 'code', 'java'])
|
||||||
|
|
||||||
|
# Push multiple new tags with a positional argument.
|
||||||
|
blog = BlogPost.objects(pk='ABC').modify(
|
||||||
|
push__tags__1=['go', 'rust'],
|
||||||
|
new=True)
|
||||||
|
self.assertEqual(blog.tags, ['python', 'go', 'rust', 'code', 'java'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -1913,8 +1913,7 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
post = BlogPost(slug="test")
|
post = BlogPost.objects.create(slug="test")
|
||||||
post.save()
|
|
||||||
|
|
||||||
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"])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user