-
Notifications
You must be signed in to change notification settings - Fork 33
Add $exp expression tests #663
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
Open
danielfrankcom
wants to merge
3
commits into
documentdb:main
Choose a base branch
from
danielfrankcom:pr/exp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+668
−0
Open
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
166 changes: 166 additions & 0 deletions
166
...tests/compatibility/tests/core/operator/expressions/arithmetic/exp/test_exp_boundaries.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,166 @@ | ||
| """Tests for $exp at representable-range boundaries, including overflow and underflow.""" | ||
|
|
||
| import pytest | ||
| from bson import Decimal128 | ||
|
|
||
| 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 ( | ||
| assert_expression_result, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
| from documentdb_tests.framework.test_constants import ( | ||
| DECIMAL128_INFINITY, | ||
| DECIMAL128_LARGE_EXPONENT, | ||
| DECIMAL128_MAX, | ||
| DECIMAL128_MIN, | ||
| DECIMAL128_SMALL_EXPONENT, | ||
| DOUBLE_MIN_NEGATIVE_SUBNORMAL, | ||
| DOUBLE_MIN_SUBNORMAL, | ||
| DOUBLE_NEAR_MAX, | ||
| DOUBLE_NEAR_MIN, | ||
| DOUBLE_ZERO, | ||
| FLOAT_INFINITY, | ||
| INT32_MAX, | ||
| INT64_MAX, | ||
| INT64_MIN, | ||
| ) | ||
|
|
||
| # Property [Overflow]: $exp overflows to infinity past the largest representable exponent and | ||
| # underflows to zero for large negative inputs. | ||
| EXP_OVERFLOW_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "just_below_overflow", | ||
| doc={"value": 709}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=pytest.approx(8.218407461554972e307), | ||
| msg="$exp should return a large finite double just below the overflow boundary", | ||
| ), | ||
| ExpressionTestCase( | ||
| "overflow", | ||
| doc={"value": 710}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=FLOAT_INFINITY, | ||
| msg="$exp should return infinity past the overflow boundary", | ||
| ), | ||
| ExpressionTestCase( | ||
| "underflow", | ||
| doc={"value": -750}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=DOUBLE_ZERO, | ||
| msg="$exp should underflow to zero for a large negative input", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Integer Boundaries]: $exp of extreme integer inputs overflows to infinity or | ||
| # underflows to zero. | ||
| EXP_INTEGER_BOUNDARY_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "int32_max", | ||
| doc={"value": INT32_MAX}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=FLOAT_INFINITY, | ||
| msg="$exp should return infinity for INT32_MAX", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int64_max", | ||
| doc={"value": INT64_MAX}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=FLOAT_INFINITY, | ||
| msg="$exp should return infinity for INT64_MAX", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int64_min", | ||
| doc={"value": INT64_MIN}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=DOUBLE_ZERO, | ||
| msg="$exp should underflow to zero for INT64_MIN", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Double Boundaries]: $exp near the double subnormal range returns one, and a large | ||
| # double overflows to infinity. | ||
| EXP_DOUBLE_BOUNDARY_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "double_min_subnormal", | ||
| doc={"value": DOUBLE_MIN_SUBNORMAL}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=1.0, | ||
| msg="$exp should return one for the minimum subnormal double", | ||
| ), | ||
| ExpressionTestCase( | ||
| "double_min_negative_subnormal", | ||
| doc={"value": DOUBLE_MIN_NEGATIVE_SUBNORMAL}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=1.0, | ||
| msg="$exp should return one for the minimum negative subnormal double", | ||
| ), | ||
| ExpressionTestCase( | ||
| "double_near_min", | ||
| doc={"value": DOUBLE_NEAR_MIN}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=1.0, | ||
| msg="$exp should return one for a near-zero positive double", | ||
| ), | ||
| ExpressionTestCase( | ||
| "double_near_max", | ||
| doc={"value": DOUBLE_NEAR_MAX}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=FLOAT_INFINITY, | ||
| msg="$exp should overflow to infinity for a large double", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Decimal128 Boundaries]: $exp of extreme decimal128 exponents overflows to infinity or | ||
| # underflows to zero, preserving the decimal type. | ||
| EXP_DECIMAL_BOUNDARY_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "decimal_small_exponent", | ||
| doc={"value": DECIMAL128_SMALL_EXPONENT}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=Decimal128("1"), | ||
| msg="$exp should return one for a decimal128 with a small exponent", | ||
| ), | ||
| ExpressionTestCase( | ||
| "decimal_large_exponent", | ||
| doc={"value": DECIMAL128_LARGE_EXPONENT}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=DECIMAL128_INFINITY, | ||
| msg="$exp should overflow to infinity for a decimal128 with a large exponent", | ||
| ), | ||
| ExpressionTestCase( | ||
| "decimal_max", | ||
| doc={"value": DECIMAL128_MAX}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=DECIMAL128_INFINITY, | ||
| msg="$exp should overflow to infinity for decimal128 max", | ||
| ), | ||
| ExpressionTestCase( | ||
| "decimal_min", | ||
| doc={"value": DECIMAL128_MIN}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=Decimal128("0E-6176"), | ||
| msg="$exp should underflow to zero for decimal128 min", | ||
| ), | ||
| ] | ||
|
|
||
| EXP_BOUNDARY_ALL_TESTS = ( | ||
| EXP_OVERFLOW_TESTS | ||
| + EXP_INTEGER_BOUNDARY_TESTS | ||
| + EXP_DOUBLE_BOUNDARY_TESTS | ||
| + EXP_DECIMAL_BOUNDARY_TESTS | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test_case", pytest_params(EXP_BOUNDARY_ALL_TESTS)) | ||
| def test_exp_boundaries(collection, test_case: ExpressionTestCase): | ||
| """Test $exp representable-range boundary cases.""" | ||
| result = execute_expression_with_insert(collection, test_case.expression, test_case.doc) | ||
| assert_expression_result( | ||
| result, | ||
| expected=test_case.expected, | ||
| error_code=test_case.error_code, | ||
| msg=test_case.msg, | ||
| ) | ||
76 changes: 76 additions & 0 deletions
76
...tdb_tests/compatibility/tests/core/operator/expressions/arithmetic/exp/test_exp_errors.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,76 @@ | ||
| """Tests for $exp type and arity errors.""" | ||
|
|
||
| from datetime import datetime, timezone | ||
|
|
||
| import pytest | ||
| from bson import Binary, Code, MaxKey, MinKey, ObjectId, Regex, Timestamp | ||
|
|
||
| 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 ( | ||
| assert_expression_result, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.error_codes import ( | ||
| EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| NON_NUMERIC_TYPE_MISMATCH_ERROR, | ||
| ) | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| # Property [Type Strictness]: $exp rejects non-numeric input types. | ||
| EXP_TYPE_ERROR_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| f"type_{tid}", | ||
| doc={"value": val}, | ||
| expression={"$exp": ["$value"]}, | ||
| error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, | ||
| msg=f"$exp should reject a {tid} input", | ||
| ) | ||
| for tid, val in [ | ||
| ("string", "abc"), | ||
| ("bool", True), | ||
| ("array", [1, 2]), | ||
| ("object", {"a": 1}), | ||
| ("datetime", datetime(2024, 1, 1, tzinfo=timezone.utc)), | ||
| ("objectid", ObjectId("507f1f77bcf86cd799439011")), | ||
| ("regex", Regex("abc")), | ||
| ("binary", Binary(b"data")), | ||
| ("minkey", MinKey()), | ||
| ("maxkey", MaxKey()), | ||
| ("timestamp", Timestamp(1, 1)), | ||
| ("code", Code("function(){}")), | ||
| ] | ||
| ] | ||
|
|
||
| # Property [Arity]: $exp requires exactly one argument. | ||
| EXP_ARITY_ERROR_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "arity_zero", | ||
| doc={}, | ||
| expression={"$exp": []}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="$exp should reject zero arguments", | ||
| ), | ||
| ExpressionTestCase( | ||
| "arity_two", | ||
| doc={}, | ||
| expression={"$exp": [1, 2]}, | ||
| error_code=EXPRESSION_TYPE_MISMATCH_ERROR, | ||
| msg="$exp should reject two arguments", | ||
| ), | ||
| ] | ||
|
|
||
| EXP_ERROR_ALL_TESTS = EXP_TYPE_ERROR_TESTS + EXP_ARITY_ERROR_TESTS | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test_case", pytest_params(EXP_ERROR_ALL_TESTS)) | ||
| def test_exp_errors(collection, test_case: ExpressionTestCase): | ||
| """Test $exp type and arity error cases.""" | ||
| result = execute_expression_with_insert(collection, test_case.expression, test_case.doc) | ||
| assert_expression_result( | ||
| result, | ||
| expected=test_case.expected, | ||
| error_code=test_case.error_code, | ||
| msg=test_case.msg, | ||
| ) |
97 changes: 97 additions & 0 deletions
97
...ests/compatibility/tests/core/operator/expressions/arithmetic/exp/test_exp_input_forms.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,97 @@ | ||
| """Tests for $exp argument forms, literal input, field paths, and nested expression input.""" | ||
|
|
||
| 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 ( | ||
| assert_expression_result, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| # Property [Argument Form]: $exp accepts its single argument bare or wrapped in a one-element | ||
| # array. | ||
| EXP_ARGUMENT_FORM_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "form_array", | ||
| doc={"value": 1}, | ||
| expression={"$exp": ["$value"]}, | ||
| expected=pytest.approx(2.718281828459045), | ||
| msg="$exp should accept its argument wrapped in a one-element array", | ||
| ), | ||
| ExpressionTestCase( | ||
| "form_bare", | ||
| doc={"value": 1}, | ||
| expression={"$exp": "$value"}, | ||
| expected=pytest.approx(2.718281828459045), | ||
| msg="$exp should accept its argument without an array wrapper", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Literal Input]: $exp evaluates an inline literal argument, not only document fields. | ||
| EXP_LITERAL_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "literal_input", | ||
| doc={}, | ||
| expression={"$exp": [1]}, | ||
| expected=pytest.approx(2.718281828459045), | ||
| msg="$exp should return e for an inline literal argument", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Expression Input]: $exp evaluates a nested expression argument before exponentiating. | ||
| EXP_EXPRESSION_INPUT_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "nested_exp", | ||
| doc={}, | ||
| expression={"$exp": {"$exp": 1}}, | ||
| expected=pytest.approx(15.154262241479262), | ||
| msg="$exp should evaluate a nested $exp expression argument", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Field Path Input]: $exp resolves a field path argument. A dotted path into a nested | ||
| # object yields the referenced value; a path over an array (an array-of-objects path, or a numeric | ||
| # component applied over an array) resolves to an array, which $exp rejects as a non-numeric type. | ||
| EXP_FIELD_PATH_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "nested_field_path", | ||
| doc={"a": {"b": 1}}, | ||
| expression={"$exp": "$a.b"}, | ||
| expected=pytest.approx(2.718281828459045), | ||
| msg="$exp should resolve a dotted field path into a nested object", | ||
| ), | ||
| ExpressionTestCase( | ||
| "composite_array_field_path", | ||
| doc={"a": [{"b": 1}, {"b": 2}]}, | ||
| expression={"$exp": "$a.b"}, | ||
| error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, | ||
| msg="$exp should reject a field path that resolves to an array from an array of objects", | ||
| ), | ||
| ExpressionTestCase( | ||
| "array_index_field_path", | ||
| doc={"a": [1, 2]}, | ||
| expression={"$exp": "$a.0"}, | ||
| error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, | ||
| msg="$exp should reject a numeric path component over an array, which resolves non-numeric", | ||
| ), | ||
| ] | ||
|
|
||
| EXP_INPUT_FORM_TESTS = ( | ||
| EXP_ARGUMENT_FORM_TESTS + EXP_LITERAL_TESTS + EXP_EXPRESSION_INPUT_TESTS + EXP_FIELD_PATH_TESTS | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test_case", pytest_params(EXP_INPUT_FORM_TESTS)) | ||
| def test_exp_input_forms(collection, test_case: ExpressionTestCase): | ||
| """Test $exp argument form, literal, field path, and nested expression input cases.""" | ||
| result = execute_expression_with_insert(collection, test_case.expression, test_case.doc) | ||
| assert_expression_result( | ||
| result, | ||
| expected=test_case.expected, | ||
| error_code=test_case.error_code, | ||
| msg=test_case.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.