Skip to content

Commit

Permalink
Fix combination of __raw__ and mongoengine syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
idocyabra committed Oct 7, 2023
1 parent a0ed25f commit c8607d5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Development
===========
- (Fill this out as you fix issues and develop your features).
- Fix for uuidRepresentation not read when provided in URI #2741
- Fix combination of __raw__ and mongoengine syntax.

Changes in 0.27.0
=================
Expand Down
12 changes: 10 additions & 2 deletions mongoengine/queryset/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,22 @@
)


def handle_raw_query(value: dict, mongo_query: dict):
for op, v in value.items():
if op not in mongo_query:
mongo_query[op] = v
elif op in mongo_query and isinstance(mongo_query[op], dict):
mongo_query[op].update(v)


# TODO make this less complex
def query(_doc_cls=None, **kwargs):
"""Transform a query from Django-style format to Mongo format."""
mongo_query = {}
merge_query = defaultdict(list)
for key, value in sorted(kwargs.items()):
if key == "__raw__":
mongo_query.update(value)
handle_raw_query(value, mongo_query)
continue

parts = key.rsplit("__")
Expand Down Expand Up @@ -234,7 +242,7 @@ def update(_doc_cls=None, **update):

for key, value in update.items():
if key == "__raw__":
mongo_update.update(value)
handle_raw_query(value, mongo_update)
continue

parts = key.split("__")
Expand Down
40 changes: 40 additions & 0 deletions tests/queryset/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,46 @@ class BlogPost(Document):
post.reload()
assert post.slug == "When test test it"

def test_combination_of_mongoengine_and__raw__(self):
"""Ensure that the '__raw__' update/query works in combination with mongoengine syntax correctly."""

class BlogPost(Document):
slug = StringField()
foo = StringField()
tags = ListField(StringField())

BlogPost.drop_collection()

post = BlogPost(slug="test", foo="bar")
post.save()

BlogPost.objects(slug="test").update(
foo="baz",
__raw__={"$set": {"slug": "test test"}},
)
post.reload()
assert post.slug == "test test"
assert post.foo == "baz"

assert BlogPost.objects(foo="baz", __raw__={"slug": "test test"}).count() == 1
assert (
BlogPost.objects(foo__ne="bar", __raw__={"slug": {"$ne": "test"}}).count()
== 1
)
assert (
BlogPost.objects(foo="baz", __raw__={"slug": {"$ne": "test test"}}).count()
== 0
)
assert (
BlogPost.objects(foo__ne="baz", __raw__={"slug": "test test"}).count() == 0
)
assert (
BlogPost.objects(
foo__ne="baz", __raw__={"slug": {"$ne": "test test"}}
).count()
== 0
)

def test_add_to_set_each(self):
class Item(Document):
name = StringField(required=True)
Expand Down

0 comments on commit c8607d5

Please sign in to comment.