-
Notifications
You must be signed in to change notification settings - Fork 35
Add $subtract tests #677
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
PatersonProjects
wants to merge
11
commits into
documentdb:main
Choose a base branch
from
PatersonProjects:substract_tests
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.
Open
Add $subtract tests #677
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
51a482a
Added tests
PatersonProjects 014f2bc
Added docStrings to all files
PatersonProjects 37407ae
Revert "Added docStrings to all files"
PatersonProjects f851712
Revert "Added tests"
PatersonProjects b03fb59
Merge branch 'main' of https://github.com/PatersonProjects/functional…
PatersonProjects 3be56ce
Add subtract tests
PatersonProjects 7b6505c
Added init
PatersonProjects 7acfe86
Fixed failing tests
PatersonProjects 8d105e3
Updated to use more constants
PatersonProjects 550fd9e
Addressed PR Comments
PatersonProjects 1f8a449
Changed round wording, added negative round-down test
PatersonProjects 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.
195 changes: 195 additions & 0 deletions
195
.../compatibility/tests/core/operator/expressions/arithmetic/subtract/test_subtract_basic.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,195 @@ | ||
| 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 ( | ||
| assert_expression_result, | ||
| execute_expression_with_insert, | ||
| ) | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
| from documentdb_tests.framework.test_constants import ( | ||
| DECIMAL128_TWO_AND_HALF, | ||
| DOUBLE_NEGATIVE_ZERO, | ||
| DOUBLE_TWO_AND_HALF, | ||
| DOUBLE_ZERO, | ||
| INT32_ZERO, | ||
| ) | ||
|
|
||
| # Property [Same-type arithmetic]: $subtract preserves the BSON type when both operands share | ||
| # a type. | ||
| SAME_TYPE_ARITHMETIC_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "same_type_int32", | ||
| doc={"a": 10, "b": 3}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=7, | ||
| msg="$subtract should return int32 for int32 - int32", | ||
| ), | ||
| ExpressionTestCase( | ||
| "same_type_int64", | ||
| doc={"a": Int64(20), "b": Int64(5)}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=Int64(15), | ||
| msg="$subtract should return int64 for int64 - int64", | ||
| ), | ||
| ExpressionTestCase( | ||
| "same_type_double", | ||
| doc={"a": 10.5, "b": DOUBLE_TWO_AND_HALF}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=8.0, | ||
| msg="$subtract should return double for double - double", | ||
| ), | ||
| ExpressionTestCase( | ||
| "same_type_decimal", | ||
| doc={"a": Decimal128("20.5"), "b": Decimal128("10.5")}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=Decimal128("10.0"), | ||
| msg="$subtract should return Decimal128 for Decimal128 - Decimal128", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Mixed-type promotion]: $subtract promotes the result to the wider of the two input | ||
| # types. | ||
| MIXED_TYPE_PROMOTION_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "int32_int64", | ||
| doc={"a": 10, "b": Int64(3)}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=Int64(7), | ||
| msg="$subtract should promote to int64 for int32 - int64", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int32_double", | ||
| doc={"a": 10, "b": DOUBLE_TWO_AND_HALF}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=7.5, | ||
| msg="$subtract should promote to double for int32 - double", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int32_decimal", | ||
| doc={"a": 10, "b": DECIMAL128_TWO_AND_HALF}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=Decimal128("7.5"), | ||
| msg="$subtract should promote to Decimal128 for int32 - Decimal128", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int64_double", | ||
| doc={"a": Int64(20), "b": 5.5}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=14.5, | ||
| msg="$subtract should promote to double for int64 - double", | ||
| ), | ||
| ExpressionTestCase( | ||
| "int64_decimal", | ||
| doc={"a": Int64(20), "b": Decimal128("5.5")}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=Decimal128("14.5"), | ||
| msg="$subtract should promote to Decimal128 for int64 - Decimal128", | ||
| ), | ||
| ExpressionTestCase( | ||
| "double_decimal", | ||
| doc={"a": 10.5, "b": DECIMAL128_TWO_AND_HALF}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=Decimal128("8.0000000000000"), | ||
| msg="$subtract should promote to Decimal128 for double - Decimal128", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Sign handling]: $subtract correctly computes the sign of the result. | ||
| SIGN_HANDLING_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "negative_positive", | ||
| doc={"a": -10, "b": 3}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=-13, | ||
| msg="$subtract should return negative for negative minuend minus positive subtrahend", | ||
| ), | ||
| ExpressionTestCase( | ||
| "positive_negative", | ||
| doc={"a": 10, "b": -3}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=13, | ||
| msg="$subtract should return positive for positive minuend minus negative subtrahend", | ||
| ), | ||
| ExpressionTestCase( | ||
| "both_negative", | ||
| doc={"a": -10, "b": -3}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=-7, | ||
| msg="$subtract should return negative when both operands are negative and |a| > |b|", | ||
| ), | ||
| ExpressionTestCase( | ||
| "result_negative", | ||
| doc={"a": 5, "b": 10}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=-5, | ||
| msg="$subtract should return negative when the minuend is smaller than the subtrahend", | ||
| ), | ||
| ExpressionTestCase( | ||
| "result_negative_double", | ||
| doc={"a": 5.5, "b": 10}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=-4.5, | ||
| msg="$subtract should return negative double when minuend is smaller than subtrahend", | ||
| ), | ||
| ] | ||
|
|
||
| # Property [Zero handling]: $subtract handles zero operands correctly. | ||
| ZERO_HANDLING_TESTS: list[ExpressionTestCase] = [ | ||
| ExpressionTestCase( | ||
| "zero_minuend", | ||
| doc={"a": INT32_ZERO, "b": 5}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=-5, | ||
| msg="$subtract should return negated subtrahend when the minuend is zero", | ||
| ), | ||
| ExpressionTestCase( | ||
| "zero_subtrahend", | ||
| doc={"a": 5, "b": INT32_ZERO}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=5, | ||
| msg="$subtract should return the minuend unchanged when the subtrahend is zero", | ||
| ), | ||
| ExpressionTestCase( | ||
| "zeros", | ||
| doc={"a": INT32_ZERO, "b": INT32_ZERO}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=INT32_ZERO, | ||
| msg="$subtract should return zero for zero - zero", | ||
| ), | ||
| ExpressionTestCase( | ||
| "zero_negative_zero", | ||
| doc={"a": INT32_ZERO, "b": DOUBLE_NEGATIVE_ZERO}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=DOUBLE_ZERO, | ||
| msg="$subtract of int32 zero minus double negative-zero should return double positive-zero", | ||
| ), | ||
| ExpressionTestCase( | ||
| "negative_zero_zero", | ||
| doc={"a": DOUBLE_NEGATIVE_ZERO, "b": INT32_ZERO}, | ||
| expression={"$subtract": ["$a", "$b"]}, | ||
| expected=DOUBLE_NEGATIVE_ZERO, | ||
| msg="$subtract of double negative-zero minus int32 zero should return double negative-zero", | ||
| ), | ||
| ] | ||
|
|
||
| SUBTRACT_BASIC_TESTS: list[ExpressionTestCase] = ( | ||
| SAME_TYPE_ARITHMETIC_TESTS | ||
| + MIXED_TYPE_PROMOTION_TESTS | ||
| + SIGN_HANDLING_TESTS | ||
| + ZERO_HANDLING_TESTS | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test_case", pytest_params(SUBTRACT_BASIC_TESTS)) | ||
| def test_subtract_basic(collection, test_case: ExpressionTestCase): | ||
| """Test $subtract same-type and mixed-type numeric arithmetic.""" | ||
| 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.