-
Notifications
You must be signed in to change notification settings - Fork 7
Added comparison tests for $cmp #119
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Empty file.
Empty file.
128 changes: 128 additions & 0 deletions
128
...mpatibility/tests/core/operator/expressions/comparisons/cmp/test_cmp_argument_handling.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,128 @@ | ||
| """ | ||
| Tests for $cmp argument handling. | ||
|
|
||
| Covers argument validation (error cases), field references, return type verification, | ||
| and nested $cmp expressions. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.expression_test_case import ( # noqa: E501 | ||
| ExpressionTestCase, | ||
| ) | ||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( # noqa: E501 | ||
| assert_expression_result, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.error_codes import EXPRESSION_TYPE_MISMATCH_ERROR | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| ARG_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "no_args", | ||
| expression={"$cmp": []}, | ||
| doc={"a": 1}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="Should error for empty arguments", | ||
| ), | ||
| ExpressionTestCase( | ||
| "single_arg", | ||
| expression={"$cmp": ["$a"]}, | ||
| doc={"a": 1}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="Should error for single argument", | ||
| ), | ||
| ExpressionTestCase( | ||
| "non_array_int", | ||
| expression={"$cmp": 1}, | ||
| doc={"a": 1}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="Should error for non-array int argument", | ||
| ), | ||
| ExpressionTestCase( | ||
| "non_array_string", | ||
| expression={"$cmp": "string"}, | ||
| doc={"a": 1}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="Should error for non-array string argument", | ||
| ), | ||
| ExpressionTestCase( | ||
| "non_array_object", | ||
| expression={"$cmp": {"a": 1}}, | ||
| doc={"a": 1}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="Should error for non-array object argument", | ||
| ), | ||
| ExpressionTestCase( | ||
| "non_array_boolean", | ||
| expression={"$cmp": True}, | ||
| doc={"a": 1}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="Should error for non-array boolean argument", | ||
| ), | ||
| ExpressionTestCase( | ||
| "three_args", | ||
| expression={"$cmp": ["$a", "$b", "$c"]}, | ||
| doc={"a": 1, "b": 2, "c": 3}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="Should error for three arguments", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| FIELD_REF_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "field_ref_lt", | ||
| expression={"$cmp": ["$qty", 250]}, | ||
| doc={"qty": 200}, | ||
| expected=-1, | ||
| msg="Field reference less than literal", | ||
| ), | ||
| ExpressionTestCase( | ||
| "field_ref_eq", | ||
| expression={"$cmp": ["$qty", 250]}, | ||
| doc={"qty": 250}, | ||
| expected=0, | ||
| msg="Field reference equals literal", | ||
| ), | ||
| ExpressionTestCase( | ||
| "field_ref_gt", | ||
| expression={"$cmp": ["$qty", 250]}, | ||
| doc={"qty": 300}, | ||
| expected=1, | ||
| msg="Field reference greater than literal", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| RETURN_TYPE_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "return_type_neg1", | ||
| expression={"$type": {"$cmp": [1, 2]}}, | ||
| doc={"a": 1}, | ||
| expected="int", | ||
| msg="$cmp should return BSON int type", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| NESTED_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "nested_cmp", | ||
| expression={"$cmp": [{"$cmp": [1, 2]}, -1]}, | ||
| doc={"a": 1}, | ||
| expected=0, | ||
| msg="Nested $cmp(-1) equals -1", | ||
| ), | ||
| ] | ||
|
|
||
| ALL_TESTS = ARG_TESTS + FIELD_REF_TESTS + RETURN_TYPE_TESTS + NESTED_TESTS | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(ALL_TESTS)) | ||
| def test_cmp_argument_handling(collection, test): | ||
| """Test $cmp argument validation, field references, return type, and nesting.""" | ||
| result = execute_expression_with_insert(collection, test.expression, test.doc) | ||
| assert_expression_result( | ||
| result, expected=test.expected, error_code=test.error_code, msg=test.msg | ||
| ) | ||
106 changes: 106 additions & 0 deletions
106
...patibility/tests/core/operator/expressions/comparisons/cmp/test_cmp_boundary_precision.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,106 @@ | ||
| """ | ||
| Tests for $cmp boundary values and precision. | ||
|
|
||
| Covers cross-type boundary values, large number precision at double/long boundary, | ||
| and overflow adjacency. | ||
| """ | ||
|
|
||
| import pytest | ||
| from bson import Decimal128, Int64 | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.expression_test_case import ( # noqa: E501 | ||
| ExpressionTestCase, | ||
| ) | ||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( # noqa: E501 | ||
| assert_expression_result, | ||
| execute_expression, | ||
| ) | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
| from documentdb_tests.framework.test_constants import ( | ||
| DOUBLE_MAX_SAFE_INTEGER, | ||
| DOUBLE_NEGATIVE_ZERO, | ||
| DOUBLE_PRECISION_LOSS, | ||
| INT32_MAX, | ||
| INT32_MIN, | ||
| INT32_OVERFLOW, | ||
| INT32_UNDERFLOW, | ||
| INT64_MAX, | ||
| INT64_MIN, | ||
| ) | ||
|
|
||
| BOUNDARY_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "int32_max_self", | ||
| expression={"$cmp": [INT32_MAX, INT32_MAX]}, | ||
| expected=0, | ||
| msg="INT32_MAX equals itself", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int32_max_vs_overflow", | ||
| expression={"$cmp": [INT32_MAX, INT32_OVERFLOW]}, | ||
| expected=-1, | ||
| msg="INT32_MAX < INT32_OVERFLOW (promoted to long)", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int32_min_vs_underflow", | ||
| expression={"$cmp": [INT32_MIN, INT32_UNDERFLOW]}, | ||
| expected=1, | ||
| msg="INT32_MIN > INT32_UNDERFLOW (promoted to long)", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int32_max_vs_int64_max", | ||
| expression={"$cmp": [INT32_MAX, INT64_MAX]}, | ||
| expected=-1, | ||
| msg="INT32_MAX < INT64_MAX", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int64_min_vs_int32_min", | ||
| expression={"$cmp": [INT64_MIN, INT32_MIN]}, | ||
| expected=-1, | ||
| msg="INT64_MIN < INT32_MIN", | ||
| ), | ||
|
vic-tsang marked this conversation as resolved.
vic-tsang marked this conversation as resolved.
|
||
| ExpressionTestCase( | ||
| "int64_max_self", | ||
| expression={"$cmp": [INT64_MAX, INT64_MAX]}, | ||
| expected=0, | ||
| msg="INT64_MAX equals itself", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| LARGE_NUMBER_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "beyond_double_precision", | ||
| expression={"$cmp": [Int64(DOUBLE_PRECISION_LOSS), float(DOUBLE_PRECISION_LOSS)]}, | ||
| expected=1, | ||
| msg="Int64(2^53+1) > double(2^53+1) — beyond double precision", | ||
| ), | ||
| ExpressionTestCase( | ||
| "exactly_representable", | ||
| expression={"$cmp": [Int64(DOUBLE_MAX_SAFE_INTEGER), float(DOUBLE_MAX_SAFE_INTEGER)]}, | ||
| expected=0, | ||
| msg="Int64(2^53) vs double(2^53) — exactly representable", | ||
| ), | ||
| ExpressionTestCase( | ||
| "dec128_preserves_precision", | ||
| expression={"$cmp": [Decimal128(str(DOUBLE_PRECISION_LOSS)), Int64(DOUBLE_PRECISION_LOSS)]}, | ||
| expected=0, | ||
| msg="Decimal128(2^53+1) equals Int64(2^53+1)", | ||
| ), | ||
| ExpressionTestCase( | ||
| "neg_zero_vs_pos_zero", | ||
| expression={"$cmp": [DOUBLE_NEGATIVE_ZERO, 0]}, | ||
| expected=0, | ||
| msg="-0.0 equals 0 (negative zero is equal to positive zero)", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| ALL_TESTS = BOUNDARY_TESTS + LARGE_NUMBER_TESTS | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(ALL_TESTS)) | ||
| def test_literal(collection, test): | ||
| """Test $cmp across numeric boundary values and cross-type precision edges.""" | ||
| result = execute_expression(collection, test.expression) | ||
| assert_expression_result(result, expected=test.expected, msg=test.msg) | ||
83 changes: 83 additions & 0 deletions
83
...ompatibility/tests/core/operator/expressions/comparisons/cmp/test_cmp_expression_types.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,83 @@ | ||
| """ | ||
| Tests for $cmp expression type smoke tests. | ||
|
|
||
| Covers literal, field path, expression operator, and array/object expression inputs. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.expression_test_case import ( # noqa: E501 | ||
| ExpressionTestCase, | ||
| ) | ||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( # noqa: E501 | ||
| assert_expression_result, | ||
| execute_expression, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| LITERAL_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "literal_lt", | ||
| expression={"$cmp": [5, 10]}, | ||
| expected=-1, | ||
| msg="Literal 5 < 10", | ||
| ), | ||
| ExpressionTestCase( | ||
| "literal_gt", | ||
| expression={"$cmp": [10, 5]}, | ||
| expected=1, | ||
| msg="Literal 10 > 5", | ||
| ), | ||
| ExpressionTestCase( | ||
| "literal_eq", | ||
| expression={"$cmp": [7, 7]}, | ||
| expected=0, | ||
| msg="Literal 7 == 7", | ||
| ), | ||
| ExpressionTestCase( | ||
| "literal_nested_expr", | ||
| expression={"$cmp": [{"$add": [1, 2]}, 4]}, | ||
| expected=-1, | ||
| msg="Literal $add(1,2)=3 < 4", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(LITERAL_TESTS)) | ||
| def test_cmp_expression_literal(collection, test): | ||
| """Test $cmp with literal expression inputs.""" | ||
| result = execute_expression(collection, test.expression) | ||
| assert_expression_result(result, expected=test.expected, msg=test.msg) | ||
|
|
||
|
|
||
| INSERT_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "field_path", | ||
| expression={"$cmp": ["$a", "$b"]}, | ||
| doc={"a": 10, "b": 5}, | ||
| expected=1, | ||
| msg="Field path $a(10) > $b(5)", | ||
| ), | ||
| ExpressionTestCase( | ||
| "expression_operator", | ||
| expression={"$cmp": [{"$abs": "$x"}, 3]}, | ||
| doc={"x": -5}, | ||
| expected=1, | ||
| msg="abs(-5)=5 > 3", | ||
| ), | ||
| ExpressionTestCase( | ||
| "array_expression", | ||
| expression={"$cmp": [["$x", "$y"], [1, 2]]}, | ||
| doc={"x": 1, "y": 2}, | ||
| expected=0, | ||
| msg="Array expression [1,2] == [1,2]", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(INSERT_TESTS)) | ||
| def test_cmp_expression_with_insert(collection, test): | ||
| """Test $cmp with field path and expression inputs.""" | ||
| result = execute_expression_with_insert(collection, test.expression, test.doc) | ||
| assert_expression_result(result, expected=test.expected, msg=test.msg) |
54 changes: 54 additions & 0 deletions
54
...ts/compatibility/tests/core/operator/expressions/comparisons/cmp/test_cmp_field_lookup.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,54 @@ | ||
| """ | ||
| Tests for $cmp field lookup and array index paths. | ||
|
|
||
| Covers array vs scalar in BSON order and deep nested path resolution. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.expression_test_case import ( # noqa: E501 | ||
| ExpressionTestCase, | ||
| ) | ||
| from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( # noqa: E501 | ||
| assert_expression_result, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| ALL_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "array_vs_scalar", | ||
| expression={"$cmp": ["$a", "$b"]}, | ||
| doc={"a": [1, 2, 3], "b": 5}, | ||
| expected=1, | ||
| msg="Array > number in BSON order", | ||
| ), | ||
| ExpressionTestCase( | ||
| "array_element_by_element", | ||
| expression={"$cmp": ["$a", "$b"]}, | ||
| doc={"a": [1, 2, 3], "b": [1, 2, 4]}, | ||
| expected=-1, | ||
| msg="[1,2,3] < [1,2,4] element-by-element", | ||
| ), | ||
| ExpressionTestCase( | ||
| "deep_nested_path", | ||
| expression={"$cmp": ["$a.b.c.d", 1]}, | ||
| doc={"a": {"b": {"c": {"d": 1}}}}, | ||
| expected=0, | ||
| msg="Deep nested path $a.b.c.d resolves to 1", | ||
| ), | ||
| ExpressionTestCase( | ||
| "deep_nested_missing", | ||
| expression={"$cmp": ["$a.b.c.d", 1]}, | ||
| doc={"a": {"x": 1}}, | ||
| expected=-1, | ||
| msg="Missing intermediate field in deep path treated as null", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(ALL_TESTS)) | ||
| def test_cmp_field_lookup(collection, test): | ||
| """Test $cmp field lookup and array index paths.""" | ||
| result = execute_expression_with_insert(collection, test.expression, test.doc) | ||
| assert_expression_result(result, expected=test.expected, msg=test.msg) |
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.