-
Notifications
You must be signed in to change notification settings - Fork 31
INTPYTHON-793: Push conditions into lookup when possible. #440
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,28 @@ class MongoTestCaseMixin: | |
| maxDiff = None | ||
| query_types = {"SON": SON, "ObjectId": ObjectId, "Decimal128": Decimal128} | ||
|
|
||
| COMMUTATIVE_OPERATORS = {"$and", "$or", "$all"} | ||
|
|
||
| @staticmethod | ||
| def _normalize_query(obj): | ||
| if isinstance(obj, dict): | ||
| normalized = {} | ||
| for k, v in obj.items(): | ||
| if k in MongoTestCaseMixin.COMMUTATIVE_OPERATORS and isinstance(v, list): | ||
| # Only sort for commutative operators | ||
| normalized[k] = sorted( | ||
| (MongoTestCaseMixin._normalize_query(i) for i in v), key=lambda x: str(x) | ||
| ) | ||
| else: | ||
| normalized[k] = MongoTestCaseMixin._normalize_query(v) | ||
| return normalized | ||
|
|
||
| if isinstance(obj, list): | ||
| # Lists not under commutative ops keep their order | ||
| return [MongoTestCaseMixin._normalize_query(i) for i in obj] | ||
|
|
||
| return obj | ||
|
Comment on lines
+13
to
+30
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs a docstring. It looks like there's some sorting happening, perhaps because the generated query isn't deterministic?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The query is deterministic, but if I remove this, I had to change many tests because some commutative operators like OR and AND. Since both queries mean the same, the test should accept them as equal under commutative operations. |
||
|
|
||
| def assertAggregateQuery(self, query, expected_collection, expected_pipeline): | ||
| """ | ||
| Assert that the logged query is equal to: | ||
|
|
@@ -16,7 +38,14 @@ def assertAggregateQuery(self, query, expected_collection, expected_pipeline): | |
| _, collection, operator = prefix.split(".") | ||
| self.assertEqual(operator, "aggregate") | ||
| self.assertEqual(collection, expected_collection) | ||
| self.assertEqual(eval(pipeline[:-1], self.query_types, {}), expected_pipeline) # noqa: S307 | ||
| self.assertEqual( | ||
| self._normalize_query( | ||
| eval( # noqa: S307 | ||
| pipeline[:-1], {"SON": SON, "ObjectId": ObjectId, "Decimal128": Decimal128}, {} | ||
| ) | ||
| ), | ||
| self._normalize_query(expected_pipeline), | ||
| ) | ||
|
|
||
| def assertInsertQuery(self, query, expected_collection, expected_documents): | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added