8000 Project missing fields in a nested dict fix by superqwer · Pull Request #605 · mongomock/mongomock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Project missing fields in a nested dict fix #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mongomock/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ def _handle_project_stage(in_collection, unused_database, options):

for in_doc, out_doc in zip(in_collection, new_fields_collection):
try:
out_doc[field] = _parse_expression(value, in_doc)
out_doc[field] = _parse_expression(value, in_doc, ignore_missing_keys=True)
except KeyError:
pass
if (method == 'include') == (include_id is not False and include_id is not 0):
Expand Down
11 changes: 11 additions & 0 deletions tests/test__collection_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3068,6 +3068,17 @@ def test__aggregate_project_missing_fields(self):
])
self.assertEqual([{}], list(actual))

def test__aggregate_project_missing_nested_fields(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an equivalent test in test__mongomock to ensure the behavior is kept the same between pymongo and mongomock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

self.db.collection.insert_one({'_id': 1, 'a': 2, 'b': {'c': 1}})
actual = self.db.collection.aggregate([
{'$match': {'_id': 1}},
{'$project': collections.OrderedDict([
('_id', False),
('nested_dictionary', {'c': '$b.c', 'd': '$b.d'})
])}
])
self.assertEqual([{'nested_dictionary': {'c': 1}}], list(actual))

def test__aggregate_project_out(self):
self.db.collection.insert_one({'_id': 1, 'arr': {'a': 2, 'b': 3}})
self.db.collection.insert_one({'_id': 2, 'arr': {'a': 4, 'b': 5}})
Expand Down
13 changes: 13 additions & 0 deletions tests/test__mongomock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,19 @@ def test__aggregate_project_with_subfields_exclude(self):
]
self.cmp.compare_ignore_order.aggregate(pipeline)

def test_aggregate_project_with_missing_subfields(self):
self.cmp.do.insert_many([
{'a': {'b': 3}, 'other': 1},
{'a': {'b': {'c': 4}, 'd': 5}},
{'a': {'c': 3, 'd': 5}},
{'b': {'c': 3}},
{'a': 5},
])
pipeline = [
{'$project': {'_id': False, 'e': '$a.b.c'}}
]
self.cmp.compare_ignore_order.aggregate(pipeline)

def test__aggregate_unwind_project_id(self):
self.cmp.do.insert_one({
'_id': 'id0',
Expand Down
0