-
Notifications
You must be signed in to change notification settings - Fork 7
Added query tests for $gt #99
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
146 changes: 146 additions & 0 deletions
146
...mentdb_tests/compatibility/tests/core/operator/query/comparison/gt/test_gt_bson_wiring.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| """ | ||
| Tests for $gt BSON type wiring. | ||
|
|
||
| A representative sample of types to confirm $gt is wired up to the | ||
| BSON comparison engine correctly (not exhaustive cross-type matrix). | ||
| """ | ||
|
|
||
| from datetime import datetime, timezone | ||
|
|
||
| import pytest | ||
| from bson import Binary, Decimal128, Int64, MaxKey, MinKey, ObjectId, Timestamp | ||
| from bson.codec_options import CodecOptions | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.query.utils.query_test_case import ( | ||
| QueryTestCase, | ||
| ) | ||
| from documentdb_tests.framework.assertions import assertSuccess | ||
| from documentdb_tests.framework.executor import execute_command | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="double", | ||
| filter={"a": {"$gt": 5.0}}, | ||
| doc=[{"_id": 1, "a": 1.0}, {"_id": 2, "a": 5.0}, {"_id": 3, "a": 10.0}], | ||
| expected=[{"_id": 3, "a": 10.0}], | ||
| msg="$gt with double returns docs with value > 5.0", | ||
| ), | ||
| QueryTestCase( | ||
| id="int", | ||
| filter={"a": {"$gt": 5}}, | ||
| doc=[{"_id": 1, "a": 1}, {"_id": 2, "a": 5}, {"_id": 3, "a": 10}], | ||
| expected=[{"_id": 3, "a": 10}], | ||
| msg="$gt with int returns docs with value > 5", | ||
| ), | ||
| QueryTestCase( | ||
| id="long", | ||
| filter={"a": {"$gt": Int64(5)}}, | ||
| doc=[ | ||
| {"_id": 1, "a": Int64(1)}, | ||
| {"_id": 2, "a": Int64(5)}, | ||
| {"_id": 3, "a": Int64(10)}, | ||
| ], | ||
| expected=[{"_id": 3, "a": Int64(10)}], | ||
| msg="$gt with long returns docs with value > 5", | ||
| ), | ||
| QueryTestCase( | ||
| id="decimal128", | ||
| filter={"a": {"$gt": Decimal128("5")}}, | ||
| doc=[ | ||
| {"_id": 1, "a": Decimal128("1")}, | ||
| {"_id": 2, "a": Decimal128("5")}, | ||
| {"_id": 3, "a": Decimal128("10")}, | ||
| ], | ||
| expected=[{"_id": 3, "a": Decimal128("10")}], | ||
| msg="$gt with decimal128 returns docs with value > 5", | ||
| ), | ||
| QueryTestCase( | ||
| id="string", | ||
| filter={"a": {"$gt": "banana"}}, | ||
| doc=[ | ||
| {"_id": 1, "a": "apple"}, | ||
| {"_id": 2, "a": "banana"}, | ||
| {"_id": 3, "a": "cherry"}, | ||
| ], | ||
| expected=[{"_id": 3, "a": "cherry"}], | ||
| msg="$gt with string returns docs with value > 'banana'", | ||
| ), | ||
| QueryTestCase( | ||
| id="date", | ||
| filter={"a": {"$gt": datetime(2023, 1, 1, tzinfo=timezone.utc)}}, | ||
| doc=[ | ||
| {"_id": 1, "a": datetime(2020, 1, 1, tzinfo=timezone.utc)}, | ||
| {"_id": 2, "a": datetime(2023, 1, 1, tzinfo=timezone.utc)}, | ||
| {"_id": 3, "a": datetime(2025, 1, 1, tzinfo=timezone.utc)}, | ||
| ], | ||
| expected=[{"_id": 3, "a": datetime(2025, 1, 1, tzinfo=timezone.utc)}], | ||
| msg="$gt with date returns docs with later dates", | ||
| ), | ||
| QueryTestCase( | ||
| id="timestamp", | ||
| filter={"a": {"$gt": Timestamp(2000, 1)}}, | ||
| doc=[ | ||
| {"_id": 1, "a": Timestamp(1000, 1)}, | ||
| {"_id": 2, "a": Timestamp(3000, 1)}, | ||
| ], | ||
| expected=[{"_id": 2, "a": Timestamp(3000, 1)}], | ||
| msg="$gt with timestamp returns docs with larger timestamp", | ||
| ), | ||
| QueryTestCase( | ||
| id="objectid", | ||
| filter={"a": {"$gt": ObjectId("507f1f77bcf86cd799439012")}}, | ||
| doc=[ | ||
| {"_id": 1, "a": ObjectId("507f1f77bcf86cd799439011")}, | ||
| {"_id": 2, "a": ObjectId("507f1f77bcf86cd799439013")}, | ||
| ], | ||
| expected=[{"_id": 2, "a": ObjectId("507f1f77bcf86cd799439013")}], | ||
| msg="$gt with ObjectId returns docs with later ObjectId", | ||
| ), | ||
| QueryTestCase( | ||
| id="boolean", | ||
| filter={"a": {"$gt": False}}, | ||
| doc=[{"_id": 1, "a": False}, {"_id": 2, "a": True}], | ||
| expected=[{"_id": 2, "a": True}], | ||
| msg="$gt with boolean false returns doc with true", | ||
| ), | ||
| QueryTestCase( | ||
| id="bindata", | ||
| filter={"a": {"$gt": Binary(b"\x05\x06", 128)}}, | ||
| doc=[ | ||
| {"_id": 1, "a": Binary(b"\x01\x02", 128)}, | ||
| {"_id": 2, "a": Binary(b"\x09\x0a", 128)}, | ||
| ], | ||
| expected=[{"_id": 2, "a": Binary(b"\x09\x0a", 128)}], | ||
| msg="$gt with BinData returns docs with larger binary", | ||
| ), | ||
| QueryTestCase( | ||
| id="minkey", | ||
| filter={"a": {"$gt": MinKey()}}, | ||
| doc=[{"_id": 1, "a": MinKey()}, {"_id": 2, "a": 1}], | ||
| expected=[{"_id": 2, "a": 1}], | ||
| msg="$gt with MinKey returns all non-MinKey docs", | ||
| ), | ||
| QueryTestCase( | ||
| id="maxkey", | ||
| filter={"a": {"$gt": MaxKey()}}, | ||
| doc=[ | ||
| {"_id": 1, "a": 1}, | ||
| {"_id": 2, "a": "hello"}, | ||
| {"_id": 3, "a": MaxKey()}, | ||
| ], | ||
| expected=[], | ||
| msg="$gt with MaxKey returns nothing", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(TESTS)) | ||
| def test_gt_bson_wiring(collection, test): | ||
| """Parametrized test for $gt BSON type wiring.""" | ||
| collection.insert_many(test.doc) | ||
| codec = CodecOptions(tz_aware=True, tzinfo=timezone.utc) | ||
| result = execute_command( | ||
| collection, {"find": collection.name, "filter": test.filter}, codec_options=codec | ||
| ) | ||
| assertSuccess(result, test.expected, ignore_doc_order=True) | ||
137 changes: 137 additions & 0 deletions
137
documentdb_tests/compatibility/tests/core/operator/query/comparison/gt/test_gt_edge_cases.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| """ | ||
| Edge case tests for $gt operator. | ||
|
|
||
| Covers deeply nested field paths with NaN, large array element matching, | ||
| empty string ordering, null/missing field handling, and cross-type bracketing. | ||
| """ | ||
|
|
||
| from datetime import datetime, timezone | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.query.utils.query_test_case import ( | ||
| QueryTestCase, | ||
| ) | ||
| from documentdb_tests.framework.assertions import assertSuccess | ||
| from documentdb_tests.framework.executor import execute_command | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| MISC_EDGE_CASE_TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="deeply_nested_field_with_nan", | ||
| filter={"a.b.c.d.e": {"$gt": 10}}, | ||
| doc=[ | ||
| {"_id": 1, "a": {"b": {"c": {"d": {"e": 5}}}}}, | ||
| {"_id": 2, "a": {"b": {"c": {"d": {"e": 15}}}}}, | ||
| {"_id": 3, "a": {"b": {"c": {"d": {"e": float("nan")}}}}}, | ||
| ], | ||
| expected=[{"_id": 2, "a": {"b": {"c": {"d": {"e": 15}}}}}], | ||
| msg="$gt on deeply nested field path; NaN does not satisfy $gt", | ||
| ), | ||
| QueryTestCase( | ||
| id="large_array_element_match", | ||
| filter={"a": {"$gt": 1000}}, | ||
| doc=[ | ||
| {"_id": 1, "a": list(range(0, 1000)) + [1001]}, | ||
| {"_id": 2, "a": list(range(0, 1000))}, | ||
| ], | ||
| expected=[{"_id": 1, "a": list(range(0, 1000)) + [1001]}], | ||
| msg="$gt matches element in a large (1001-element) array", | ||
| ), | ||
| QueryTestCase( | ||
| id="empty_string_gt_nothing", | ||
| filter={"a": {"$gt": ""}}, | ||
| doc=[{"_id": 1, "a": ""}, {"_id": 2, "a": "a"}], | ||
| expected=[{"_id": 2, "a": "a"}], | ||
| msg="non-empty string is greater than empty string", | ||
| ), | ||
| ] | ||
|
|
||
| NULL_MISSING_TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="gt_null_query_returns_nothing", | ||
| filter={"a": {"$gt": None}}, | ||
| doc=[{"_id": 1, "a": 5}, {"_id": 2, "a": None}, {"_id": 3}], | ||
| expected=[], | ||
| msg="$gt null returns no documents", | ||
| ), | ||
| QueryTestCase( | ||
| id="null_field_not_gt_numeric_query", | ||
| filter={"a": {"$gt": 5}}, | ||
| doc=[{"_id": 1, "a": None}], | ||
| expected=[], | ||
| msg="null field does not match $gt with numeric query", | ||
| ), | ||
| QueryTestCase( | ||
| id="missing_field_not_gt_numeric_query", | ||
| filter={"a": {"$gt": 5}}, | ||
| doc=[{"_id": 1, "b": 10}], | ||
| expected=[], | ||
| msg="missing field does not match $gt numeric query", | ||
| ), | ||
| QueryTestCase( | ||
| id="gt_null_on_missing_field_no_match", | ||
| filter={"a": {"$gt": None}}, | ||
| doc=[{"_id": 1, "b": 10}], | ||
| expected=[], | ||
| msg="$gt null on missing field returns no match", | ||
| ), | ||
| QueryTestCase( | ||
| id="gt_null_on_non_null_field_no_match", | ||
| filter={"a": {"$gt": None}}, | ||
| doc=[{"_id": 1, "a": 5}], | ||
| expected=[], | ||
| msg="$gt null on non-null field returns no match (type bracketing)", | ||
| ), | ||
| ] | ||
|
|
||
| TYPE_BRACKETING_TESTS: list[QueryTestCase] = [ | ||
| QueryTestCase( | ||
| id="bool_false_field_not_gt_int_0_query", | ||
| filter={"a": {"$gt": 0}}, | ||
| doc=[{"_id": 1, "a": False}], | ||
| expected=[], | ||
| msg="boolean false is not > int 0 (different type brackets)", | ||
| ), | ||
| QueryTestCase( | ||
| id="bool_true_field_not_gt_int_1_query", | ||
| filter={"a": {"$gt": 1}}, | ||
| doc=[{"_id": 1, "a": True}], | ||
| expected=[], | ||
| msg="boolean true is not > int 1 (different type brackets)", | ||
| ), | ||
| QueryTestCase( | ||
| id="date_field_not_gt_numeric_query", | ||
| filter={"a": {"$gt": 0}}, | ||
| doc=[{"_id": 1, "a": datetime(2025, 1, 1, tzinfo=timezone.utc)}], | ||
| expected=[], | ||
| msg="date field does not match $gt numeric query (different type brackets)", | ||
| ), | ||
| QueryTestCase( | ||
| id="null_field_not_gt_string_query", | ||
| filter={"a": {"$gt": "abc"}}, | ||
| doc=[{"_id": 1, "a": None}], | ||
| expected=[], | ||
| msg="null field does not match $gt string query (different type brackets)", | ||
| ), | ||
| QueryTestCase( | ||
| id="object_not_gt_null", | ||
| filter={"a": {"$gt": None}}, | ||
| doc=[{"_id": 1, "a": {}}], | ||
| expected=[], | ||
| msg="empty object does not match $gt null (different BSON types)", | ||
| ), | ||
| ] | ||
|
|
||
| ALL_PARAMETRIZED_TESTS = MISC_EDGE_CASE_TESTS + NULL_MISSING_TESTS + TYPE_BRACKETING_TESTS | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(ALL_PARAMETRIZED_TESTS)) | ||
| def test_gt_edge_cases(collection, test): | ||
| """Parametrized test for $gt edge cases. | ||
|
|
||
| Covers nested fields, large arrays, null/missing, and type bracketing. | ||
| """ | ||
| collection.insert_many(test.doc) | ||
| result = execute_command(collection, {"find": collection.name, "filter": test.filter}) | ||
| assertSuccess(result, test.expected, ignore_doc_order=True) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.