diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_basic.py new file mode 100644 index 000000000..3592cc7d5 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_basic.py @@ -0,0 +1,148 @@ +""" +$acos results. + +Tests $acos results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and the domain bounds and the values just inside them. Expected values are +MongoDB's. +""" + +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_HALF, + DECIMAL128_NAN, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_NEGATIVE_ZERO, + FLOAT_NAN, + MISSING, +) + +ACOS_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$acos": 1}, + expected=0.0, + msg="Should return 0.0 for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$acos": Int64(1)}, + expected=0.0, + msg="Should return 0.0 for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$acos": DOUBLE_HALF}, + expected=1.0471975511965979, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$acos": DECIMAL128_HALF}, + expected=Decimal128("1.047197551196597746154214461093168"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$acos": DECIMAL128_ZERO}, + expected=Decimal128("1.570796326794896619231321691639751"), + msg="Should return the Decimal128 result for Decimal128 zero", + ), + ExpressionTestCase( + "null", + expression={"$acos": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$acos": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$acos": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$acos": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$acos": DOUBLE_NEGATIVE_ZERO}, + expected=1.5707963267948966, + msg="Should return the double result for -0.0", + ), + ExpressionTestCase( + "single_element_array", + expression={"$acos": [DOUBLE_HALF]}, + expected=1.0471975511965979, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "double_one", + expression={"$acos": 1.0}, + expected=0.0, + msg="Should return 0.0 for the upper domain bound", + ), + ExpressionTestCase( + "double_negative_one", + expression={"$acos": -1.0}, + expected=3.141592653589793, + msg="Should return the double result for the lower domain bound", + ), + ExpressionTestCase( + "long_negative_one", + expression={"$acos": Int64(-1)}, + expected=3.141592653589793, + msg="Should return the double result for the lower domain bound as a long", + ), + ExpressionTestCase( + "decimal128_one", + expression={"$acos": Decimal128("1")}, + expected=Decimal128("0E-6176"), + msg="Should return a Decimal128 zero for the upper domain bound as Decimal128", + ), + ExpressionTestCase( + "decimal128_negative_one", + expression={"$acos": Decimal128("-1")}, + expected=Decimal128("3.141592653589793238462643383279503"), + msg="Should return the Decimal128 result for the lower domain bound as Decimal128", + ), + ExpressionTestCase( + "double_just_below_one", + expression={"$acos": 0.9999999999999999}, + expected=1.4901161193847656e-08, + msg="Should return the double result for the largest double below 1", + ), + ExpressionTestCase( + "decimal128_just_below_one", + expression={"$acos": Decimal128("0.9999999999999999999999999999999999")}, + expected=Decimal128("1.414213562373095048801688724209698E-17"), + msg="Should return the Decimal128 result for the largest 34-digit Decimal128 below 1", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ACOS_BASIC_TESTS)) +def test_acos_basic(collection, test): + """Test $acos results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_bson_type_validation.py new file mode 100644 index 000000000..bb6e6651c --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_bson_type_validation.py @@ -0,0 +1,69 @@ +""" +BSON type validation tests for $acos expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest +from bson import Decimal128, Int64 + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +ACOS_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$acos should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + valid_inputs={ + BsonType.DOUBLE: 0.5, + BsonType.INT: 1, + BsonType.LONG: Int64(1), + BsonType.DECIMAL: Decimal128("0.5"), + }, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(ACOS_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(ACOS_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_acos_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $acos rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$acos": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_acos_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $acos accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$acos": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$acos should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_errors.py new file mode 100644 index 000000000..af3765e70 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acos/test_acos_errors.py @@ -0,0 +1,170 @@ +""" +$acos errors. + +Tests that $acos rejects a wrong argument count (16020), non-numeric input (28765) and +out-of-range input (50989). +""" + +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.error_codes import ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, + TRIG_INPUT_OUT_OF_RANGE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DECIMAL128_INFINITY, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ONE_AND_HALF, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_ONE_AND_HALF, + FLOAT_INFINITY, + FLOAT_NEGATIVE_INFINITY, +) + +ACOS_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "double_two", + expression={"$acos": 2.0}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a double input of 2 with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$acos": Decimal128("-2")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a Decimal128 input of -2 with error 50989", + ), + ExpressionTestCase( + "double_max", + expression={"$acos": DOUBLE_MAX}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest double with error 50989", + ), + ExpressionTestCase( + "double_infinity", + expression={"$acos": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity with error 50989", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$acos": FLOAT_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double -Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$acos": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$acos": DECIMAL128_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 -Infinity with error 50989", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$acos": "$v"}, + doc={"v": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$acos": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$acos": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$acos": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$acos": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$acos": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$acos": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$acos": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), + ExpressionTestCase( + "double_just_above_one", + expression={"$acos": 1.0000000000000002}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the smallest double above 1 with error 50989", + ), + ExpressionTestCase( + "double_just_below_negative_one", + expression={"$acos": -1.0000000000000002}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest double below -1 with error 50989", + ), + ExpressionTestCase( + "decimal128_just_above_one", + expression={"$acos": Decimal128("1.000000000000000000000000000000001")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the smallest 34-digit Decimal128 above 1 with error 50989", + ), + ExpressionTestCase( + "out_of_range_field", + expression={"$acos": "$v"}, + doc={"v": DOUBLE_ONE_AND_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain double read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_out_of_range_field", + expression={"$acos": "$v"}, + doc={"v": DECIMAL128_ONE_AND_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain Decimal128 read from a field with error 50989", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ACOS_ERRORS_TESTS)) +def test_acos_errors(collection, test): + """Test $acos errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_basic.py new file mode 100644 index 000000000..37aded6e3 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_basic.py @@ -0,0 +1,131 @@ +""" +$acosh results. + +Tests $acosh results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0, the domain bounds and the values just inside them and infinite input. +Expected values are MongoDB's. +""" + +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_INFINITY, + DECIMAL128_NAN, + DOUBLE_MAX, + FLOAT_INFINITY, + FLOAT_NAN, + MISSING, +) + +ACOSH_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$acosh": 1}, + expected=0.0, + msg="Should return 0.0 for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$acosh": Int64(1)}, + expected=0.0, + msg="Should return 0.0 for a long input", + ), + ExpressionTestCase( + "double_two", + expression={"$acosh": 2.0}, + expected=1.3169578969248166, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "double_max", + expression={"$acosh": DOUBLE_MAX}, + expected=710.4758600739439, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$acosh": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$acosh": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$acosh": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$acosh": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "double_infinity", + expression={"$acosh": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$acosh": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$acosh": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$acosh": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "double_one", + expression={"$acosh": 1.0}, + expected=0.0, + msg="Should return 0.0 for the lower domain bound", + ), + ExpressionTestCase( + "decimal128_one", + expression={"$acosh": Decimal128("1")}, + expected=Decimal128("0"), + msg="Should return a Decimal128 zero for the lower domain bound as Decimal128", + ), + ExpressionTestCase( + "decimal128_two", + expression={"$acosh": Decimal128("2")}, + expected=Decimal128("1.316957896924816708625046347307968"), + msg="Should return the Decimal128 result for a Decimal128 input of 2", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ACOSH_BASIC_TESTS)) +def test_acosh_basic(collection, test): + """Test $acosh results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_bson_type_validation.py new file mode 100644 index 000000000..1bf5f2b9b --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_bson_type_validation.py @@ -0,0 +1,69 @@ +""" +BSON type validation tests for $acosh expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest +from bson import Decimal128, Int64 + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +ACOSH_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$acosh should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + valid_inputs={ + BsonType.DOUBLE: 2.0, + BsonType.INT: 1, + BsonType.LONG: Int64(1), + BsonType.DECIMAL: Decimal128("2"), + }, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(ACOSH_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(ACOSH_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_acosh_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $acosh rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$acosh": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_acosh_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $acosh accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$acosh": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$acosh should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_errors.py new file mode 100644 index 000000000..8adfa3768 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/acosh/test_acosh_errors.py @@ -0,0 +1,154 @@ +""" +$acosh errors. + +Tests that $acosh rejects a wrong argument count (16020), non-numeric input (28765) and +out-of-range input (50989). +""" + +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.error_codes import ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, + TRIG_INPUT_OUT_OF_RANGE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DECIMAL128_HALF, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_NEGATIVE_ZERO, + FLOAT_NEGATIVE_INFINITY, +) + +ACOSH_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "double_half", + expression={"$acosh": DOUBLE_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a double input with error 50989", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$acosh": DECIMAL128_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a Decimal128 input with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$acosh": Decimal128("-2")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a Decimal128 input of -2 with error 50989", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$acosh": DECIMAL128_ZERO}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 zero with error 50989", + ), + ExpressionTestCase( + "negative_zero", + expression={"$acosh": DOUBLE_NEGATIVE_ZERO}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject -0.0 with error 50989", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$acosh": FLOAT_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double -Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$acosh": DECIMAL128_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 -Infinity with error 50989", + ), + ExpressionTestCase( + "single_element_array", + expression={"$acosh": [DOUBLE_HALF]}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a single-element argument array with error 50989", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$acosh": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$acosh": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$acosh": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$acosh": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$acosh": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$acosh": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), + ExpressionTestCase( + "double_just_below_one", + expression={"$acosh": 0.9999999999999999}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest double below 1 with error 50989", + ), + ExpressionTestCase( + "decimal128_just_below_one", + expression={"$acosh": Decimal128("0.9999999999999999999999999999999999")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest 34-digit Decimal128 below 1 with error 50989", + ), + ExpressionTestCase( + "out_of_range_field", + expression={"$acosh": "$v"}, + doc={"v": DOUBLE_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain double read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_out_of_range_field", + expression={"$acosh": "$v"}, + doc={"v": DECIMAL128_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain Decimal128 read from a field with error 50989", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ACOSH_ERRORS_TESTS)) +def test_acosh_errors(collection, test): + """Test $acosh errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_basic.py new file mode 100644 index 000000000..5f7eb096e --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_basic.py @@ -0,0 +1,148 @@ +""" +$asin results. + +Tests $asin results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and the domain bounds and the values just inside them. Expected values are +MongoDB's. +""" + +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_HALF, + DECIMAL128_NAN, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_NEGATIVE_ZERO, + FLOAT_NAN, + MISSING, +) + +ASIN_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$asin": 1}, + expected=1.5707963267948966, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$asin": Int64(1)}, + expected=1.5707963267948966, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$asin": DOUBLE_HALF}, + expected=0.5235987755982989, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$asin": DECIMAL128_HALF}, + expected=Decimal128("0.5235987755982988730771072305465838"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$asin": DECIMAL128_ZERO}, + expected=Decimal128("0E-40"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "null", + expression={"$asin": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$asin": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$asin": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$asin": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$asin": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "single_element_array", + expression={"$asin": [DOUBLE_HALF]}, + expected=0.5235987755982989, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "double_one", + expression={"$asin": 1.0}, + expected=1.5707963267948966, + msg="Should return the double result for the upper domain bound", + ), + ExpressionTestCase( + "double_negative_one", + expression={"$asin": -1.0}, + expected=-1.5707963267948966, + msg="Should return the double result for the lower domain bound", + ), + ExpressionTestCase( + "long_negative_one", + expression={"$asin": Int64(-1)}, + expected=-1.5707963267948966, + msg="Should return the double result for the lower domain bound as a long", + ), + ExpressionTestCase( + "decimal128_one", + expression={"$asin": Decimal128("1")}, + expected=Decimal128("1.570796326794896619231321691639751"), + msg="Should return the Decimal128 result for the upper domain bound as Decimal128", + ), + ExpressionTestCase( + "decimal128_negative_one", + expression={"$asin": Decimal128("-1")}, + expected=Decimal128("-1.570796326794896619231321691639751"), + msg="Should return the Decimal128 result for the lower domain bound as Decimal128", + ), + ExpressionTestCase( + "double_just_below_one", + expression={"$asin": 0.9999999999999999}, + expected=1.5707963118937354, + msg="Should return the double result for the largest double below 1", + ), + ExpressionTestCase( + "decimal128_just_below_one", + expression={"$asin": Decimal128("0.9999999999999999999999999999999999")}, + expected=Decimal128("1.570796326794896605089186067908801"), + msg="Should return the Decimal128 result for the largest 34-digit Decimal128 below 1", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ASIN_BASIC_TESTS)) +def test_asin_basic(collection, test): + """Test $asin results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_bson_type_validation.py new file mode 100644 index 000000000..d6c8c4e96 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_bson_type_validation.py @@ -0,0 +1,69 @@ +""" +BSON type validation tests for $asin expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest +from bson import Decimal128, Int64 + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +ASIN_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$asin should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + valid_inputs={ + BsonType.DOUBLE: 0.5, + BsonType.INT: 1, + BsonType.LONG: Int64(1), + BsonType.DECIMAL: Decimal128("0.5"), + }, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(ASIN_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(ASIN_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_asin_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $asin rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$asin": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_asin_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $asin accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$asin": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$asin should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_errors.py new file mode 100644 index 000000000..93ea9c2ea --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asin/test_asin_errors.py @@ -0,0 +1,170 @@ +""" +$asin errors. + +Tests that $asin rejects a wrong argument count (16020), non-numeric input (28765) and +out-of-range input (50989). +""" + +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.error_codes import ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, + TRIG_INPUT_OUT_OF_RANGE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DECIMAL128_INFINITY, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ONE_AND_HALF, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_ONE_AND_HALF, + FLOAT_INFINITY, + FLOAT_NEGATIVE_INFINITY, +) + +ASIN_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "double_two", + expression={"$asin": 2.0}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a double input of 2 with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$asin": Decimal128("-2")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a Decimal128 input of -2 with error 50989", + ), + ExpressionTestCase( + "double_max", + expression={"$asin": DOUBLE_MAX}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest double with error 50989", + ), + ExpressionTestCase( + "double_infinity", + expression={"$asin": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity with error 50989", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$asin": FLOAT_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double -Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$asin": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$asin": DECIMAL128_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 -Infinity with error 50989", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$asin": "$v"}, + doc={"v": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$asin": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$asin": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$asin": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$asin": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$asin": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$asin": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$asin": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), + ExpressionTestCase( + "double_just_above_one", + expression={"$asin": 1.0000000000000002}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the smallest double above 1 with error 50989", + ), + ExpressionTestCase( + "double_just_below_negative_one", + expression={"$asin": -1.0000000000000002}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest double below -1 with error 50989", + ), + ExpressionTestCase( + "decimal128_just_above_one", + expression={"$asin": Decimal128("1.000000000000000000000000000000001")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the smallest 34-digit Decimal128 above 1 with error 50989", + ), + ExpressionTestCase( + "out_of_range_field", + expression={"$asin": "$v"}, + doc={"v": DOUBLE_ONE_AND_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain double read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_out_of_range_field", + expression={"$asin": "$v"}, + doc={"v": DECIMAL128_ONE_AND_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain Decimal128 read from a field with error 50989", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ASIN_ERRORS_TESTS)) +def test_asin_errors(collection, test): + """Test $asin errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_basic.py new file mode 100644 index 000000000..eed191e94 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_basic.py @@ -0,0 +1,166 @@ +""" +$asinh results. + +Tests $asinh results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and infinite input. Expected values are MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +ASINH_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$asinh": 1}, + expected=0.881373587019543, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$asinh": Int64(1)}, + expected=0.881373587019543, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$asinh": DOUBLE_HALF}, + expected=0.48121182505960347, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$asinh": 2.0}, + expected=1.4436354751788103, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$asinh": DECIMAL128_HALF}, + expected=Decimal128("0.4812118250596034474977589134243684"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$asinh": Decimal128("-2")}, + expected=Decimal128("-1.443635475178810342493276740273105"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$asinh": DECIMAL128_ZERO}, + expected=Decimal128("0E-6176"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$asinh": DOUBLE_MAX}, + expected=710.4758600739439, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$asinh": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$asinh": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$asinh": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$asinh": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$asinh": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "double_infinity", + expression={"$asinh": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$asinh": FLOAT_NEGATIVE_INFINITY}, + expected=FLOAT_NEGATIVE_INFINITY, + msg="Should return -Infinity for double -Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$asinh": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$asinh": DECIMAL128_NEGATIVE_INFINITY}, + expected=DECIMAL128_NEGATIVE_INFINITY, + msg="Should return Decimal128 -Infinity for Decimal128 -Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$asinh": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$asinh": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "single_element_array", + expression={"$asinh": [DOUBLE_HALF]}, + expected=0.48121182505960347, + msg="Should return the double result for a single-element argument array", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ASINH_BASIC_TESTS)) +def test_asinh_basic(collection, test): + """Test $asinh results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_bson_type_validation.py new file mode 100644 index 000000000..be1e948e5 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_bson_type_validation.py @@ -0,0 +1,62 @@ +""" +BSON type validation tests for $asinh expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +ASINH_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$asinh should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(ASINH_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(ASINH_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_asinh_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $asinh rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$asinh": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_asinh_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $asinh accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$asinh": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$asinh should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_errors.py new file mode 100644 index 000000000..562634329 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/asinh/test_asinh_errors.py @@ -0,0 +1,72 @@ +""" +$asinh errors. + +Tests that $asinh rejects a wrong argument count (16020) and non-numeric input (28765). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DOUBLE_HALF, +) + +ASINH_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "arity_zero_args", + expression={"$asinh": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$asinh": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$asinh": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$asinh": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$asinh": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$asinh": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ASINH_ERRORS_TESTS)) +def test_asinh_errors(collection, test): + """Test $asinh errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_basic.py new file mode 100644 index 000000000..9772ddfe3 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_basic.py @@ -0,0 +1,166 @@ +""" +$atan results. + +Tests $atan results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and infinite input. Expected values are MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +ATAN_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$atan": 1}, + expected=0.7853981633974483, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$atan": Int64(1)}, + expected=0.7853981633974483, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$atan": DOUBLE_HALF}, + expected=0.4636476090008061, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$atan": 2.0}, + expected=1.1071487177940904, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$atan": DECIMAL128_HALF}, + expected=Decimal128("0.4636476090008061162142562314612144"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$atan": Decimal128("-2")}, + expected=Decimal128("-1.107148717794090503017065460178537"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$atan": DECIMAL128_ZERO}, + expected=Decimal128("0"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$atan": DOUBLE_MAX}, + expected=1.5707963267948966, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$atan": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$atan": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$atan": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$atan": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$atan": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "double_infinity", + expression={"$atan": FLOAT_INFINITY}, + expected=1.5707963267948966, + msg="Should return the double result for double Infinity", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$atan": FLOAT_NEGATIVE_INFINITY}, + expected=-1.5707963267948966, + msg="Should return the double result for double -Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$atan": DECIMAL128_INFINITY}, + expected=Decimal128("1.570796326794896619231321691639751"), + msg="Should return the Decimal128 result for Decimal128 Infinity", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$atan": DECIMAL128_NEGATIVE_INFINITY}, + expected=Decimal128("-1.570796326794896619231321691639751"), + msg="Should return the Decimal128 result for Decimal128 -Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$atan": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=1.5707963267948966, + msg="Should return the double result for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$atan": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=Decimal128("1.570796326794896619231321691639751"), + msg="Should return the Decimal128 result for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "single_element_array", + expression={"$atan": [DOUBLE_HALF]}, + expected=0.4636476090008061, + msg="Should return the double result for a single-element argument array", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ATAN_BASIC_TESTS)) +def test_atan_basic(collection, test): + """Test $atan results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_bson_type_validation.py new file mode 100644 index 000000000..e04176ff2 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_bson_type_validation.py @@ -0,0 +1,62 @@ +""" +BSON type validation tests for $atan expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +ATAN_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$atan should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(ATAN_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(ATAN_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_atan_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $atan rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$atan": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_atan_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $atan accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$atan": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$atan should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_errors.py new file mode 100644 index 000000000..ad3e6bb89 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan/test_atan_errors.py @@ -0,0 +1,72 @@ +""" +$atan errors. + +Tests that $atan rejects a wrong argument count (16020) and non-numeric input (28765). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DOUBLE_HALF, +) + +ATAN_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "arity_zero_args", + expression={"$atan": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$atan": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$atan": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$atan": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$atan": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$atan": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ATAN_ERRORS_TESTS)) +def test_atan_errors(collection, test): + """Test $atan errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_basic.py new file mode 100644 index 000000000..43e51ea59 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_basic.py @@ -0,0 +1,178 @@ +""" +$atan2 results. + +Tests $atan2 results for int, long, double and Decimal128 argument pairs, null and +missing in each argument, NaN and infinity in each argument, the sign of zero and field- +path arguments. Expected values are MongoDB's. +""" + +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_NEGATIVE_ZERO, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + MISSING, +) + +ATAN2_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_int", + expression={"$atan2": [1, 1]}, + expected=0.7853981633974483, + msg="Should return the double result for two int arguments", + ), + ExpressionTestCase( + "long_long", + expression={"$atan2": [Int64(1), Int64(1)]}, + expected=0.7853981633974483, + msg="Should return the double result for two long arguments", + ), + ExpressionTestCase( + "double_double", + expression={"$atan2": [1.0, 1.0]}, + expected=0.7853981633974483, + msg="Should return the double result for two double arguments", + ), + ExpressionTestCase( + "decimal128_decimal128", + expression={"$atan2": [Decimal128("1"), Decimal128("1")]}, + expected=Decimal128("0.7853981633974483096156608458198757"), + msg="Should return the Decimal128 result for two Decimal128 arguments", + ), + ExpressionTestCase( + "decimal128_double", + expression={"$atan2": [Decimal128("1"), 1.0]}, + expected=Decimal128("0.7853981633974483096156608458198757"), + msg="Should return the Decimal128 result for a Decimal128 y and a double x", + ), + ExpressionTestCase( + "double_decimal128", + expression={"$atan2": [1.0, Decimal128("1")]}, + expected=Decimal128("0.7853981633974483096156608458198757"), + msg="Should return the Decimal128 result for a double y and a Decimal128 x", + ), + ExpressionTestCase( + "double_max_both", + expression={"$atan2": [DOUBLE_MAX, DOUBLE_MAX]}, + expected=0.7853981633974483, + msg="Should return the double result for the largest double in both arguments", + ), + ExpressionTestCase( + "fields", + expression={"$atan2": ["$y", "$x"]}, + doc={"y": 1.0, "x": -1.0}, + expected=2.356194490192345, + msg="Should return the double result for both arguments read from fields", + ), + ExpressionTestCase( + "null_y", + expression={"$atan2": [None, 1.0]}, + expected=None, + msg="Should return null for a null y", + ), + ExpressionTestCase( + "null_x", + expression={"$atan2": [1.0, None]}, + expected=None, + msg="Should return null for a null x", + ), + ExpressionTestCase( + "missing_y", + expression={"$atan2": [MISSING, 1.0]}, + expected=None, + msg="Should return null for a missing y", + ), + ExpressionTestCase( + "missing_x", + expression={"$atan2": [1.0, MISSING]}, + expected=None, + msg="Should return null for a missing x", + ), + ExpressionTestCase( + "null_y_string_x", + expression={"$atan2": [None, "a"]}, + expected=None, + msg="Should return null for a null y with a string x", + ), + ExpressionTestCase( + "nan_y", + expression={"$atan2": [FLOAT_NAN, 1.0]}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for a NaN y", + ), + ExpressionTestCase( + "nan_x", + expression={"$atan2": [1.0, FLOAT_NAN]}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for a NaN x", + ), + ExpressionTestCase( + "infinity_y", + expression={"$atan2": [FLOAT_INFINITY, 1.0]}, + expected=1.5707963267948966, + msg="Should return the double result for an infinite y", + ), + ExpressionTestCase( + "infinity_x", + expression={"$atan2": [1.0, FLOAT_INFINITY]}, + expected=0.0, + msg="Should return 0.0 for an infinite x", + ), + ExpressionTestCase( + "infinity_both", + expression={"$atan2": [FLOAT_INFINITY, FLOAT_INFINITY]}, + expected=0.7853981633974483, + msg="Should return the double result for infinite y and x", + ), + ExpressionTestCase( + "zero_zero", + expression={"$atan2": [0.0, 0.0]}, + expected=0.0, + msg="Should return 0.0 for zero y and zero x", + ), + ExpressionTestCase( + "zero_negative_zero", + expression={"$atan2": [0.0, DOUBLE_NEGATIVE_ZERO]}, + expected=3.141592653589793, + msg="Should return the double result for zero y and -0.0 x", + ), + ExpressionTestCase( + "negative_zero_zero", + expression={"$atan2": [DOUBLE_NEGATIVE_ZERO, 0.0]}, + expected=-0.0, + msg="Should return -0.0 for -0.0 y and zero x", + ), + ExpressionTestCase( + "negative_zero_negative_x", + expression={"$atan2": [DOUBLE_NEGATIVE_ZERO, -1.0]}, + expected=-3.141592653589793, + msg="Should return the double result for -0.0 y and a negative x", + ), + ExpressionTestCase( + "decimal128_negative_zero_negative_x", + expression={"$atan2": [DECIMAL128_NEGATIVE_ZERO, Decimal128("-1")]}, + expected=Decimal128("-3.141592653589793238462643383279503"), + msg="Should return the Decimal128 result for Decimal128 -0 y and a negative Decimal128 x", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ATAN2_BASIC_TESTS)) +def test_atan2_basic(collection, test): + """Test $atan2 results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_bson_type_validation.py new file mode 100644 index 000000000..a2438fe4c --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_bson_type_validation.py @@ -0,0 +1,74 @@ +""" +BSON type validation tests for $atan2 expression. + +Verifies that each of the two numeric inputs (y and x) accepts the four numeric +types plus null, and rejects every other BSON type with its own error code. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import ( + TWO_NUMERIC_ARGS_FIRST_TYPE_ERROR, + TWO_NUMERIC_ARGS_SECOND_TYPE_ERROR, +) + +NUMERIC_OR_NULL = [ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, +] + +ATAN2_BSON_PARAMS = [ + BsonTypeTestCase( + id="y", + msg="$atan2 should reject a non-numeric first argument", + keyword="y", + valid_types=NUMERIC_OR_NULL, + default_error_code=TWO_NUMERIC_ARGS_FIRST_TYPE_ERROR, + ), + BsonTypeTestCase( + id="x", + msg="$atan2 should reject a non-numeric second argument", + keyword="x", + valid_types=NUMERIC_OR_NULL, + default_error_code=TWO_NUMERIC_ARGS_SECOND_TYPE_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(ATAN2_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(ATAN2_BSON_PARAMS) + + +def _expression(spec): + return {"$atan2": ["$value", 1]} if spec.keyword == "y" else {"$atan2": [1, "$value"]} + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_atan2_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $atan2 rejects invalid BSON types in each argument.""" + result = execute_expression_with_insert(collection, _expression(spec), {"value": sample_value}) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_atan2_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $atan2 accepts valid numeric BSON types (and null) in each argument.""" + result = execute_expression_with_insert(collection, _expression(spec), {"value": sample_value}) + assertNotError(result, msg=f"$atan2 should accept {bson_type.value} for {spec.keyword}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_errors.py new file mode 100644 index 000000000..709f5aaec --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atan2/test_atan2_errors.py @@ -0,0 +1,63 @@ +""" +$atan2 errors. + +Tests that $atan2 rejects a wrong argument count (16020) and a non-numeric first +argument (51044). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + TWO_NUMERIC_ARGS_FIRST_TYPE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params + +ATAN2_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "string_y_null_x", + expression={"$atan2": ["a", None]}, + error_code=TWO_NUMERIC_ARGS_FIRST_TYPE_ERROR, + msg="Should reject a string y with a null x with error 51044", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$atan2": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_one_arg", + expression={"$atan2": [1]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject one argument with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$atan2": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "non_array_arg", + expression={"$atan2": 1}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject a non-array argument with error 16020", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ATAN2_ERRORS_TESTS)) +def test_atan2_errors(collection, test): + """Test $atan2 errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_basic.py new file mode 100644 index 000000000..cb1f3f06e --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_basic.py @@ -0,0 +1,152 @@ +""" +$atanh results. + +Tests $atanh results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and the domain bounds and the values just inside them. Expected values are +MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +ATANH_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$atanh": 1}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$atanh": Int64(1)}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$atanh": DOUBLE_HALF}, + expected=0.5493061443340548, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$atanh": DECIMAL128_HALF}, + expected=Decimal128("0.5493061443340548456976226184612628"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$atanh": DECIMAL128_ZERO}, + expected=Decimal128("0E-6176"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "null", + expression={"$atanh": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$atanh": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$atanh": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$atanh": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$atanh": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "single_element_array", + expression={"$atanh": [DOUBLE_HALF]}, + expected=0.5493061443340548, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "double_one", + expression={"$atanh": 1.0}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for the upper domain bound", + ), + ExpressionTestCase( + "double_negative_one", + expression={"$atanh": -1.0}, + expected=FLOAT_NEGATIVE_INFINITY, + msg="Should return -Infinity for the lower domain bound", + ), + ExpressionTestCase( + "long_negative_one", + expression={"$atanh": Int64(-1)}, + expected=FLOAT_NEGATIVE_INFINITY, + msg="Should return -Infinity for the lower domain bound as a long", + ), + ExpressionTestCase( + "decimal128_one", + expression={"$atanh": Decimal128("1")}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for the upper domain bound as Decimal128", + ), + ExpressionTestCase( + "decimal128_negative_one", + expression={"$atanh": Decimal128("-1")}, + expected=DECIMAL128_NEGATIVE_INFINITY, + msg="Should return Decimal128 -Infinity for the lower domain bound as Decimal128", + ), + ExpressionTestCase( + "double_just_below_one", + expression={"$atanh": 0.9999999999999999}, + expected=18.714973875118524, + msg="Should return the double result for the largest double below 1", + ), + ExpressionTestCase( + "decimal128_just_below_one", + expression={"$atanh": Decimal128("0.9999999999999999999999999999999999")}, + expected=Decimal128("39.49052017117874928301447079036328"), + msg="Should return the Decimal128 result for the largest 34-digit Decimal128 below 1", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ATANH_BASIC_TESTS)) +def test_atanh_basic(collection, test): + """Test $atanh results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_bson_type_validation.py new file mode 100644 index 000000000..1d36ec403 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_bson_type_validation.py @@ -0,0 +1,69 @@ +""" +BSON type validation tests for $atanh expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest +from bson import Decimal128, Int64 + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +ATANH_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$atanh should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + valid_inputs={ + BsonType.DOUBLE: 0.5, + BsonType.INT: 1, + BsonType.LONG: Int64(1), + BsonType.DECIMAL: Decimal128("0.5"), + }, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(ATANH_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(ATANH_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_atanh_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $atanh rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$atanh": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_atanh_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $atanh accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$atanh": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$atanh should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_errors.py new file mode 100644 index 000000000..e56fbda7e --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/atanh/test_atanh_errors.py @@ -0,0 +1,170 @@ +""" +$atanh errors. + +Tests that $atanh rejects a wrong argument count (16020), non-numeric input (28765) and +out-of-range input (50989). +""" + +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.error_codes import ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, + TRIG_INPUT_OUT_OF_RANGE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DECIMAL128_INFINITY, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ONE_AND_HALF, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_ONE_AND_HALF, + FLOAT_INFINITY, + FLOAT_NEGATIVE_INFINITY, +) + +ATANH_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "double_two", + expression={"$atanh": 2.0}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a double input of 2 with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$atanh": Decimal128("-2")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject a Decimal128 input of -2 with error 50989", + ), + ExpressionTestCase( + "double_max", + expression={"$atanh": DOUBLE_MAX}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest double with error 50989", + ), + ExpressionTestCase( + "double_infinity", + expression={"$atanh": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity with error 50989", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$atanh": FLOAT_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double -Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$atanh": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$atanh": DECIMAL128_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 -Infinity with error 50989", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$atanh": "$v"}, + doc={"v": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$atanh": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$atanh": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$atanh": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$atanh": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$atanh": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$atanh": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$atanh": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), + ExpressionTestCase( + "double_just_above_one", + expression={"$atanh": 1.0000000000000002}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the smallest double above 1 with error 50989", + ), + ExpressionTestCase( + "double_just_below_negative_one", + expression={"$atanh": -1.0000000000000002}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the largest double below -1 with error 50989", + ), + ExpressionTestCase( + "decimal128_just_above_one", + expression={"$atanh": Decimal128("1.000000000000000000000000000000001")}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject the smallest 34-digit Decimal128 above 1 with error 50989", + ), + ExpressionTestCase( + "out_of_range_field", + expression={"$atanh": "$v"}, + doc={"v": DOUBLE_ONE_AND_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain double read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_out_of_range_field", + expression={"$atanh": "$v"}, + doc={"v": DECIMAL128_ONE_AND_HALF}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject an out-of-domain Decimal128 read from a field with error 50989", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(ATANH_ERRORS_TESTS)) +def test_atanh_errors(collection, test): + """Test $atanh errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_basic.py new file mode 100644 index 000000000..39371e841 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_basic.py @@ -0,0 +1,156 @@ +""" +$cos results. + +Tests $cos results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and multiples of a quarter turn. Expected values are MongoDB's. +""" + +import math + +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_HALF, + DECIMAL128_NAN, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_NAN, + MISSING, +) + +COS_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$cos": 1}, + expected=0.5403023058681398, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$cos": Int64(1)}, + expected=0.5403023058681398, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$cos": DOUBLE_HALF}, + expected=0.8775825618903728, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$cos": 2.0}, + expected=-0.4161468365471424, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$cos": DECIMAL128_HALF}, + expected=Decimal128("0.8775825618903727161162815826038297"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$cos": Decimal128("-2")}, + expected=Decimal128("-0.4161468365471423869975682295007621"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$cos": DECIMAL128_ZERO}, + expected=Decimal128("1.000000000000000000000000000000000"), + msg="Should return the Decimal128 result for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$cos": DOUBLE_MAX}, + expected=-0.9999876894265599, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$cos": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$cos": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$cos": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$cos": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$cos": DOUBLE_NEGATIVE_ZERO}, + expected=1.0, + msg="Should return the double result for -0.0", + ), + ExpressionTestCase( + "single_element_array", + expression={"$cos": [DOUBLE_HALF]}, + expected=0.8775825618903728, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "half_pi", + expression={"$cos": math.pi / 2}, + expected=6.123233995736766e-17, + msg="Should return the double result for a quarter-turn angle", + ), + ExpressionTestCase( + "pi", + expression={"$cos": math.pi}, + expected=-1.0, + msg="Should return the double result for a half-turn angle", + ), + ExpressionTestCase( + "three_halves_pi", + expression={"$cos": 3 * math.pi / 2}, + expected=-1.8369701987210297e-16, + msg="Should return the double result for a three-quarter-turn angle", + ), + ExpressionTestCase( + "two_pi", + expression={"$cos": 2 * math.pi}, + expected=1.0, + msg="Should return the double result for a full-turn angle", + ), + ExpressionTestCase( + "decimal128_pi", + expression={"$cos": Decimal128("3.141592653589793238462643383279503")}, + expected=Decimal128("-1"), + msg="Should return the Decimal128 result for a Decimal128 half-turn angle", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(COS_BASIC_TESTS)) +def test_cos_basic(collection, test): + """Test $cos results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_bson_type_validation.py new file mode 100644 index 000000000..7d75b8ca0 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_bson_type_validation.py @@ -0,0 +1,58 @@ +""" +BSON type validation tests for $cos expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +COS_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$cos should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(COS_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(COS_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_cos_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $cos rejects invalid BSON types.""" + result = execute_expression_with_insert(collection, {"$cos": "$value"}, {"value": sample_value}) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_cos_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $cos accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert(collection, {"$cos": "$value"}, {"value": sample_value}) + assertNotError(result, msg=f"$cos should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_errors.py new file mode 100644 index 000000000..a47eb70a6 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cos/test_cos_errors.py @@ -0,0 +1,116 @@ +""" +$cos errors. + +Tests that $cos rejects a wrong argument count (16020), non-numeric input (28765) and +out-of-range input (50989). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, + TRIG_INPUT_OUT_OF_RANGE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DECIMAL128_INFINITY, + DECIMAL128_NEGATIVE_INFINITY, + DOUBLE_HALF, + FLOAT_INFINITY, + FLOAT_NEGATIVE_INFINITY, +) + +COS_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "double_infinity", + expression={"$cos": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity with error 50989", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$cos": FLOAT_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double -Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$cos": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$cos": DECIMAL128_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 -Infinity with error 50989", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$cos": "$v"}, + doc={"v": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$cos": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$cos": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$cos": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$cos": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$cos": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$cos": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$cos": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(COS_ERRORS_TESTS)) +def test_cos_errors(collection, test): + """Test $cos errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_basic.py new file mode 100644 index 000000000..d0f5ae14b --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_basic.py @@ -0,0 +1,166 @@ +""" +$cosh results. + +Tests $cosh results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and infinite input. Expected values are MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +COSH_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$cosh": 1}, + expected=1.5430806348152437, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$cosh": Int64(1)}, + expected=1.5430806348152437, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$cosh": DOUBLE_HALF}, + expected=1.1276259652063807, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$cosh": 2.0}, + expected=3.7621956910836314, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$cosh": DECIMAL128_HALF}, + expected=Decimal128("1.127625965206380785226225161402672"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$cosh": Decimal128("-2")}, + expected=Decimal128("3.762195691083631459562213477773746"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$cosh": DECIMAL128_ZERO}, + expected=Decimal128("1.000000000000000000000000000000000"), + msg="Should return the Decimal128 result for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$cosh": DOUBLE_MAX}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$cosh": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$cosh": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$cosh": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$cosh": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$cosh": DOUBLE_NEGATIVE_ZERO}, + expected=1.0, + msg="Should return the double result for -0.0", + ), + ExpressionTestCase( + "double_infinity", + expression={"$cosh": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$cosh": FLOAT_NEGATIVE_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double -Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$cosh": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$cosh": DECIMAL128_NEGATIVE_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 -Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$cosh": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$cosh": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "single_element_array", + expression={"$cosh": [DOUBLE_HALF]}, + expected=1.1276259652063807, + msg="Should return the double result for a single-element argument array", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(COSH_BASIC_TESTS)) +def test_cosh_basic(collection, test): + """Test $cosh results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_bson_type_validation.py new file mode 100644 index 000000000..4eaeed36c --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_bson_type_validation.py @@ -0,0 +1,62 @@ +""" +BSON type validation tests for $cosh expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +COSH_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$cosh should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(COSH_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(COSH_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_cosh_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $cosh rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$cosh": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_cosh_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $cosh accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$cosh": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$cosh should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_errors.py new file mode 100644 index 000000000..7f681e27f --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/cosh/test_cosh_errors.py @@ -0,0 +1,72 @@ +""" +$cosh errors. + +Tests that $cosh rejects a wrong argument count (16020) and non-numeric input (28765). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DOUBLE_HALF, +) + +COSH_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "arity_zero_args", + expression={"$cosh": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$cosh": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$cosh": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$cosh": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$cosh": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$cosh": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(COSH_ERRORS_TESTS)) +def test_cosh_errors(collection, test): + """Test $cosh errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_basic.py new file mode 100644 index 000000000..693109f98 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_basic.py @@ -0,0 +1,195 @@ +""" +$degreesToRadians results. + +Tests $degreesToRadians results for int, long, double and Decimal128 input, null and +missing input, NaN, -0.0, overflow and underflow at the double and Decimal128 limits and +infinite input. Expected values are MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_MAX, + DECIMAL128_MIN_POSITIVE, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_MIN_SUBNORMAL, + DOUBLE_NEAR_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +DEGREESTORADIANS_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$degreesToRadians": 1}, + expected=0.017453292519943295, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$degreesToRadians": Int64(1)}, + expected=0.017453292519943295, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$degreesToRadians": DOUBLE_HALF}, + expected=0.008726646259971648, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$degreesToRadians": 2.0}, + expected=0.03490658503988659, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$degreesToRadians": DECIMAL128_HALF}, + expected=Decimal128("0.008726646259971647884618453842443065"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$degreesToRadians": Decimal128("-2")}, + expected=Decimal128("-0.03490658503988659153847381536977226"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$degreesToRadians": DECIMAL128_ZERO}, + expected=Decimal128("0E-35"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$degreesToRadians": DOUBLE_MAX}, + expected=3.1375664143845866e306, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$degreesToRadians": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$degreesToRadians": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$degreesToRadians": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$degreesToRadians": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$degreesToRadians": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "double_infinity", + expression={"$degreesToRadians": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$degreesToRadians": FLOAT_NEGATIVE_INFINITY}, + expected=FLOAT_NEGATIVE_INFINITY, + msg="Should return -Infinity for double -Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$degreesToRadians": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$degreesToRadians": DECIMAL128_NEGATIVE_INFINITY}, + expected=DECIMAL128_NEGATIVE_INFINITY, + msg="Should return Decimal128 -Infinity for Decimal128 -Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$degreesToRadians": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$degreesToRadians": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "single_element_array", + expression={"$degreesToRadians": [DOUBLE_HALF]}, + expected=0.008726646259971648, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "double_near_max", + expression={"$degreesToRadians": DOUBLE_NEAR_MAX}, + expected=1.7453292519943295e306, + msg="Should return the double result for a double near the largest double", + ), + ExpressionTestCase( + "double_min_subnormal", + expression={"$degreesToRadians": DOUBLE_MIN_SUBNORMAL}, + expected=0.0, + msg="Should return 0.0 for the smallest subnormal double", + ), + ExpressionTestCase( + "decimal128_max", + expression={"$degreesToRadians": DECIMAL128_MAX}, + expected=Decimal128("1.745329251994329576923690768488613E+6143"), + msg="Should return the Decimal128 result for the largest Decimal128", + ), + ExpressionTestCase( + "decimal128_min_positive", + expression={"$degreesToRadians": DECIMAL128_MIN_POSITIVE}, + expected=Decimal128("0E-6176"), + msg="Should return a Decimal128 zero for the smallest positive Decimal128", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(DEGREESTORADIANS_BASIC_TESTS)) +def test_degreesToRadians_basic(collection, test): + """Test $degreesToRadians results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_bson_type_validation.py new file mode 100644 index 000000000..30ffabac0 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_bson_type_validation.py @@ -0,0 +1,62 @@ +""" +BSON type validation tests for $degreesToRadians expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +DEGREESTORADIANS_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$degreesToRadians should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(DEGREESTORADIANS_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(DEGREESTORADIANS_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_degreesToRadians_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $degreesToRadians rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$degreesToRadians": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_degreesToRadians_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $degreesToRadians accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$degreesToRadians": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$degreesToRadians should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_errors.py new file mode 100644 index 000000000..de9b5a1d7 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/degreesToRadians/test_degreesToRadians_errors.py @@ -0,0 +1,73 @@ +""" +$degreesToRadians errors. + +Tests that $degreesToRadians rejects a wrong argument count (16020) and non-numeric +input (28765). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DOUBLE_HALF, +) + +DEGREESTORADIANS_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "arity_zero_args", + expression={"$degreesToRadians": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$degreesToRadians": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$degreesToRadians": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$degreesToRadians": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$degreesToRadians": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$degreesToRadians": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(DEGREESTORADIANS_ERRORS_TESTS)) +def test_degreesToRadians_errors(collection, test): + """Test $degreesToRadians errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_basic.py new file mode 100644 index 000000000..3b3c08a77 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_basic.py @@ -0,0 +1,195 @@ +""" +$radiansToDegrees results. + +Tests $radiansToDegrees results for int, long, double and Decimal128 input, null and +missing input, NaN, -0.0, overflow and underflow at the double and Decimal128 limits and +infinite input. Expected values are MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_MAX, + DECIMAL128_MIN_POSITIVE, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_MIN_SUBNORMAL, + DOUBLE_NEAR_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +RADIANSTODEGREES_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$radiansToDegrees": 1}, + expected=57.29577951308232, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$radiansToDegrees": Int64(1)}, + expected=57.29577951308232, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$radiansToDegrees": DOUBLE_HALF}, + expected=28.64788975654116, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$radiansToDegrees": 2.0}, + expected=114.59155902616465, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$radiansToDegrees": DECIMAL128_HALF}, + expected=Decimal128("28.64788975654116043839907740705258"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$radiansToDegrees": Decimal128("-2")}, + expected=Decimal128("-114.5915590261646417535963096282103"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$radiansToDegrees": DECIMAL128_ZERO}, + expected=Decimal128("0E-32"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$radiansToDegrees": DOUBLE_MAX}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$radiansToDegrees": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$radiansToDegrees": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$radiansToDegrees": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$radiansToDegrees": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$radiansToDegrees": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "double_infinity", + expression={"$radiansToDegrees": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$radiansToDegrees": FLOAT_NEGATIVE_INFINITY}, + expected=FLOAT_NEGATIVE_INFINITY, + msg="Should return -Infinity for double -Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$radiansToDegrees": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$radiansToDegrees": DECIMAL128_NEGATIVE_INFINITY}, + expected=DECIMAL128_NEGATIVE_INFINITY, + msg="Should return Decimal128 -Infinity for Decimal128 -Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$radiansToDegrees": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$radiansToDegrees": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "single_element_array", + expression={"$radiansToDegrees": [DOUBLE_HALF]}, + expected=28.64788975654116, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "double_near_max", + expression={"$radiansToDegrees": DOUBLE_NEAR_MAX}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for a double near the largest double", + ), + ExpressionTestCase( + "double_min_subnormal", + expression={"$radiansToDegrees": DOUBLE_MIN_SUBNORMAL}, + expected=2.8e-322, + msg="Should return the double result for the smallest subnormal double", + ), + ExpressionTestCase( + "decimal128_max", + expression={"$radiansToDegrees": DECIMAL128_MAX}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for the largest Decimal128", + ), + ExpressionTestCase( + "decimal128_min_positive", + expression={"$radiansToDegrees": DECIMAL128_MIN_POSITIVE}, + expected=Decimal128("5.7E-6175"), + msg="Should return the Decimal128 result for the smallest positive Decimal128", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(RADIANSTODEGREES_BASIC_TESTS)) +def test_radiansToDegrees_basic(collection, test): + """Test $radiansToDegrees results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_bson_type_validation.py new file mode 100644 index 000000000..1b8a42706 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_bson_type_validation.py @@ -0,0 +1,62 @@ +""" +BSON type validation tests for $radiansToDegrees expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +RADIANSTODEGREES_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$radiansToDegrees should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(RADIANSTODEGREES_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(RADIANSTODEGREES_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_radiansToDegrees_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $radiansToDegrees rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$radiansToDegrees": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_radiansToDegrees_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $radiansToDegrees accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$radiansToDegrees": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$radiansToDegrees should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_errors.py new file mode 100644 index 000000000..49b635bbb --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/radiansToDegrees/test_radiansToDegrees_errors.py @@ -0,0 +1,73 @@ +""" +$radiansToDegrees errors. + +Tests that $radiansToDegrees rejects a wrong argument count (16020) and non-numeric +input (28765). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DOUBLE_HALF, +) + +RADIANSTODEGREES_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "arity_zero_args", + expression={"$radiansToDegrees": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$radiansToDegrees": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$radiansToDegrees": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$radiansToDegrees": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$radiansToDegrees": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$radiansToDegrees": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(RADIANSTODEGREES_ERRORS_TESTS)) +def test_radiansToDegrees_errors(collection, test): + """Test $radiansToDegrees errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_basic.py new file mode 100644 index 000000000..b44aa2549 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_basic.py @@ -0,0 +1,156 @@ +""" +$sin results. + +Tests $sin results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and multiples of a quarter turn. Expected values are MongoDB's. +""" + +import math + +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_HALF, + DECIMAL128_NAN, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_NAN, + MISSING, +) + +SIN_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$sin": 1}, + expected=0.8414709848078965, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$sin": Int64(1)}, + expected=0.8414709848078965, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$sin": DOUBLE_HALF}, + expected=0.479425538604203, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$sin": 2.0}, + expected=0.9092974268256817, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$sin": DECIMAL128_HALF}, + expected=Decimal128("0.4794255386042030002732879352155714"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$sin": Decimal128("-2")}, + expected=Decimal128("-0.9092974268256816953960198659117449"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$sin": DECIMAL128_ZERO}, + expected=Decimal128("0"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$sin": DOUBLE_MAX}, + expected=0.004961954789184062, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$sin": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$sin": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$sin": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$sin": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$sin": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "single_element_array", + expression={"$sin": [DOUBLE_HALF]}, + expected=0.479425538604203, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "half_pi", + expression={"$sin": math.pi / 2}, + expected=1.0, + msg="Should return the double result for a quarter-turn angle", + ), + ExpressionTestCase( + "pi", + expression={"$sin": math.pi}, + expected=1.2246467991473532e-16, + msg="Should return the double result for a half-turn angle", + ), + ExpressionTestCase( + "three_halves_pi", + expression={"$sin": 3 * math.pi / 2}, + expected=-1.0, + msg="Should return the double result for a three-quarter-turn angle", + ), + ExpressionTestCase( + "two_pi", + expression={"$sin": 2 * math.pi}, + expected=-2.4492935982947064e-16, + msg="Should return the double result for a full-turn angle", + ), + ExpressionTestCase( + "decimal128_pi", + expression={"$sin": Decimal128("3.141592653589793238462643383279503")}, + expected=Decimal128("-1.158028306006248941790250554076922E-34"), + msg="Should return the Decimal128 result for a Decimal128 half-turn angle", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(SIN_BASIC_TESTS)) +def test_sin_basic(collection, test): + """Test $sin results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_bson_type_validation.py new file mode 100644 index 000000000..dbe821364 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_bson_type_validation.py @@ -0,0 +1,58 @@ +""" +BSON type validation tests for $sin expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +SIN_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$sin should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(SIN_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(SIN_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_sin_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $sin rejects invalid BSON types.""" + result = execute_expression_with_insert(collection, {"$sin": "$value"}, {"value": sample_value}) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_sin_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $sin accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert(collection, {"$sin": "$value"}, {"value": sample_value}) + assertNotError(result, msg=f"$sin should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_errors.py new file mode 100644 index 000000000..736fc8605 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sin/test_sin_errors.py @@ -0,0 +1,116 @@ +""" +$sin errors. + +Tests that $sin rejects a wrong argument count (16020), non-numeric input (28765) and +out-of-range input (50989). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, + TRIG_INPUT_OUT_OF_RANGE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DECIMAL128_INFINITY, + DECIMAL128_NEGATIVE_INFINITY, + DOUBLE_HALF, + FLOAT_INFINITY, + FLOAT_NEGATIVE_INFINITY, +) + +SIN_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "double_infinity", + expression={"$sin": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity with error 50989", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$sin": FLOAT_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double -Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$sin": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$sin": DECIMAL128_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 -Infinity with error 50989", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$sin": "$v"}, + doc={"v": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$sin": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$sin": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$sin": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$sin": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$sin": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$sin": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$sin": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(SIN_ERRORS_TESTS)) +def test_sin_errors(collection, test): + """Test $sin errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_basic.py new file mode 100644 index 000000000..93432faa9 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_basic.py @@ -0,0 +1,166 @@ +""" +$sinh results. + +Tests $sinh results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and infinite input. Expected values are MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +SINH_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$sinh": 1}, + expected=1.1752011936438014, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$sinh": Int64(1)}, + expected=1.1752011936438014, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$sinh": DOUBLE_HALF}, + expected=0.5210953054937474, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$sinh": 2.0}, + expected=3.626860407847019, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$sinh": DECIMAL128_HALF}, + expected=Decimal128("0.5210953054937473616224256264114915"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$sinh": Decimal128("-2")}, + expected=Decimal128("-3.626860407847018767668213982801262"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$sinh": DECIMAL128_ZERO}, + expected=Decimal128("0E-40"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$sinh": DOUBLE_MAX}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$sinh": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$sinh": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$sinh": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$sinh": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$sinh": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "double_infinity", + expression={"$sinh": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$sinh": FLOAT_NEGATIVE_INFINITY}, + expected=FLOAT_NEGATIVE_INFINITY, + msg="Should return -Infinity for double -Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$sinh": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$sinh": DECIMAL128_NEGATIVE_INFINITY}, + expected=DECIMAL128_NEGATIVE_INFINITY, + msg="Should return Decimal128 -Infinity for Decimal128 -Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$sinh": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=FLOAT_INFINITY, + msg="Should return Infinity for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$sinh": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=DECIMAL128_INFINITY, + msg="Should return Decimal128 Infinity for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "single_element_array", + expression={"$sinh": [DOUBLE_HALF]}, + expected=0.5210953054937474, + msg="Should return the double result for a single-element argument array", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(SINH_BASIC_TESTS)) +def test_sinh_basic(collection, test): + """Test $sinh results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_bson_type_validation.py new file mode 100644 index 000000000..b6c62cb39 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_bson_type_validation.py @@ -0,0 +1,62 @@ +""" +BSON type validation tests for $sinh expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +SINH_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$sinh should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(SINH_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(SINH_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_sinh_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $sinh rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$sinh": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_sinh_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $sinh accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$sinh": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$sinh should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_errors.py new file mode 100644 index 000000000..0af0b9b99 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/sinh/test_sinh_errors.py @@ -0,0 +1,72 @@ +""" +$sinh errors. + +Tests that $sinh rejects a wrong argument count (16020) and non-numeric input (28765). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DOUBLE_HALF, +) + +SINH_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "arity_zero_args", + expression={"$sinh": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$sinh": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$sinh": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$sinh": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$sinh": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$sinh": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(SINH_ERRORS_TESTS)) +def test_sinh_errors(collection, test): + """Test $sinh errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_basic.py new file mode 100644 index 000000000..4d1e625d4 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_basic.py @@ -0,0 +1,156 @@ +""" +$tan results. + +Tests $tan results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and multiples of a quarter turn. Expected values are MongoDB's. +""" + +import math + +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_HALF, + DECIMAL128_NAN, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_NAN, + MISSING, +) + +TAN_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$tan": 1}, + expected=1.5574077246549023, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$tan": Int64(1)}, + expected=1.5574077246549023, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$tan": DOUBLE_HALF}, + expected=0.5463024898437905, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$tan": 2.0}, + expected=-2.185039863261519, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$tan": DECIMAL128_HALF}, + expected=Decimal128("0.5463024898437905132551794657802854"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$tan": Decimal128("-2")}, + expected=Decimal128("2.185039863261518991643306102313683"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$tan": DECIMAL128_ZERO}, + expected=Decimal128("0E-40"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$tan": DOUBLE_MAX}, + expected=-0.004962015874444895, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$tan": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$tan": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$tan": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$tan": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$tan": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "single_element_array", + expression={"$tan": [DOUBLE_HALF]}, + expected=0.5463024898437905, + msg="Should return the double result for a single-element argument array", + ), + ExpressionTestCase( + "half_pi", + expression={"$tan": math.pi / 2}, + expected=1.633123935319537e16, + msg="Should return the double result for a quarter-turn angle", + ), + ExpressionTestCase( + "pi", + expression={"$tan": math.pi}, + expected=-1.2246467991473532e-16, + msg="Should return the double result for a half-turn angle", + ), + ExpressionTestCase( + "three_halves_pi", + expression={"$tan": 3 * math.pi / 2}, + expected=5443746451065123.0, + msg="Should return the double result for a three-quarter-turn angle", + ), + ExpressionTestCase( + "two_pi", + expression={"$tan": 2 * math.pi}, + expected=-2.4492935982947064e-16, + msg="Should return the double result for a full-turn angle", + ), + ExpressionTestCase( + "decimal128_pi", + expression={"$tan": Decimal128("3.141592653589793238462643383279503")}, + expected=Decimal128("1.158028306006248941790250554076922E-34"), + msg="Should return the Decimal128 result for a Decimal128 half-turn angle", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(TAN_BASIC_TESTS)) +def test_tan_basic(collection, test): + """Test $tan results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_bson_type_validation.py new file mode 100644 index 000000000..787c00dcf --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_bson_type_validation.py @@ -0,0 +1,58 @@ +""" +BSON type validation tests for $tan expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +TAN_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$tan should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(TAN_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(TAN_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_tan_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $tan rejects invalid BSON types.""" + result = execute_expression_with_insert(collection, {"$tan": "$value"}, {"value": sample_value}) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_tan_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $tan accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert(collection, {"$tan": "$value"}, {"value": sample_value}) + assertNotError(result, msg=f"$tan should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_errors.py new file mode 100644 index 000000000..a80cbe607 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tan/test_tan_errors.py @@ -0,0 +1,116 @@ +""" +$tan errors. + +Tests that $tan rejects a wrong argument count (16020), non-numeric input (28765) and +out-of-range input (50989). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, + TRIG_INPUT_OUT_OF_RANGE_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DECIMAL128_INFINITY, + DECIMAL128_NEGATIVE_INFINITY, + DOUBLE_HALF, + FLOAT_INFINITY, + FLOAT_NEGATIVE_INFINITY, +) + +TAN_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "double_infinity", + expression={"$tan": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity with error 50989", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$tan": FLOAT_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double -Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$tan": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity with error 50989", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$tan": DECIMAL128_NEGATIVE_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 -Infinity with error 50989", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$tan": "$v"}, + doc={"v": FLOAT_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject double Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$tan": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + error_code=TRIG_INPUT_OUT_OF_RANGE_ERROR, + msg="Should reject Decimal128 Infinity read from a field with error 50989", + ), + ExpressionTestCase( + "arity_zero_args", + expression={"$tan": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$tan": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$tan": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$tan": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$tan": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$tan": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(TAN_ERRORS_TESTS)) +def test_tan_errors(collection, test): + """Test $tan errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_basic.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_basic.py new file mode 100644 index 000000000..3434091b6 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_basic.py @@ -0,0 +1,166 @@ +""" +$tanh results. + +Tests $tanh results for int, long, double and Decimal128 input, null and missing input, +NaN, -0.0 and infinite input. Expected values are MongoDB's. +""" + +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_HALF, + DECIMAL128_INFINITY, + DECIMAL128_NAN, + DECIMAL128_NEGATIVE_INFINITY, + DECIMAL128_ZERO, + DOUBLE_HALF, + DOUBLE_MAX, + DOUBLE_NEGATIVE_ZERO, + FLOAT_INFINITY, + FLOAT_NAN, + FLOAT_NEGATIVE_INFINITY, + MISSING, +) + +TANH_BASIC_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "int_one", + expression={"$tanh": 1}, + expected=0.7615941559557649, + msg="Should return the double result for an int input", + ), + ExpressionTestCase( + "long_one", + expression={"$tanh": Int64(1)}, + expected=0.7615941559557649, + msg="Should return the double result for a long input", + ), + ExpressionTestCase( + "double_half", + expression={"$tanh": DOUBLE_HALF}, + expected=0.46211715726000974, + msg="Should return the double result for a double input", + ), + ExpressionTestCase( + "double_two", + expression={"$tanh": 2.0}, + expected=0.9640275800758169, + msg="Should return the double result for a double input of 2", + ), + ExpressionTestCase( + "decimal128_half", + expression={"$tanh": DECIMAL128_HALF}, + expected=Decimal128("0.4621171572600097585023184836436726"), + msg="Should return the Decimal128 result for a Decimal128 input", + ), + ExpressionTestCase( + "decimal128_negative_two", + expression={"$tanh": Decimal128("-2")}, + expected=Decimal128("-0.9640275800758168839464137241009232"), + msg="Should return the Decimal128 result for a Decimal128 input of -2", + ), + ExpressionTestCase( + "decimal128_zero", + expression={"$tanh": DECIMAL128_ZERO}, + expected=Decimal128("0"), + msg="Should return a Decimal128 zero for Decimal128 zero", + ), + ExpressionTestCase( + "double_max", + expression={"$tanh": DOUBLE_MAX}, + expected=1.0, + msg="Should return the double result for the largest double", + ), + ExpressionTestCase( + "null", + expression={"$tanh": None}, + expected=None, + msg="Should return null for null input", + ), + ExpressionTestCase( + "missing", + expression={"$tanh": MISSING}, + expected=None, + msg="Should return null for a missing field", + ), + ExpressionTestCase( + "double_nan", + expression={"$tanh": FLOAT_NAN}, + expected=pytest.approx(FLOAT_NAN, nan_ok=True), + msg="Should return NaN for double NaN", + ), + ExpressionTestCase( + "decimal128_nan", + expression={"$tanh": DECIMAL128_NAN}, + expected=DECIMAL128_NAN, + msg="Should return Decimal128 NaN for Decimal128 NaN", + ), + ExpressionTestCase( + "negative_zero", + expression={"$tanh": DOUBLE_NEGATIVE_ZERO}, + expected=-0.0, + msg="Should return -0.0 for -0.0", + ), + ExpressionTestCase( + "double_infinity", + expression={"$tanh": FLOAT_INFINITY}, + expected=1.0, + msg="Should return the double result for double Infinity", + ), + ExpressionTestCase( + "double_negative_infinity", + expression={"$tanh": FLOAT_NEGATIVE_INFINITY}, + expected=-1.0, + msg="Should return the double result for double -Infinity", + ), + ExpressionTestCase( + "decimal128_infinity", + expression={"$tanh": DECIMAL128_INFINITY}, + expected=Decimal128("1"), + msg="Should return the Decimal128 result for Decimal128 Infinity", + ), + ExpressionTestCase( + "decimal128_negative_infinity", + expression={"$tanh": DECIMAL128_NEGATIVE_INFINITY}, + expected=Decimal128("-1"), + msg="Should return the Decimal128 result for Decimal128 -Infinity", + ), + ExpressionTestCase( + "double_infinity_field", + expression={"$tanh": "$v"}, + doc={"v": FLOAT_INFINITY}, + expected=1.0, + msg="Should return the double result for double Infinity read from a field", + ), + ExpressionTestCase( + "decimal128_infinity_field", + expression={"$tanh": "$v"}, + doc={"v": DECIMAL128_INFINITY}, + expected=Decimal128("1"), + msg="Should return the Decimal128 result for Decimal128 Infinity read from a field", + ), + ExpressionTestCase( + "single_element_array", + expression={"$tanh": [DOUBLE_HALF]}, + expected=0.46211715726000974, + msg="Should return the double result for a single-element argument array", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(TANH_BASIC_TESTS)) +def test_tanh_basic(collection, test): + """Test $tanh results for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_bson_type_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_bson_type_validation.py new file mode 100644 index 000000000..8996396b3 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_bson_type_validation.py @@ -0,0 +1,62 @@ +""" +BSON type validation tests for $tanh expression. + +Verifies that the single numeric input accepts the four numeric types plus null, +and rejects every other BSON type. +""" + +import pytest + +from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import ( + assert_expression_result, + execute_expression_with_insert, +) +from documentdb_tests.framework.assertions import assertNotError +from documentdb_tests.framework.bson_type_validator import ( + BsonType, + BsonTypeTestCase, + generate_bson_acceptance_test_cases, + generate_bson_rejection_test_cases, +) +from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR + +TANH_BSON_PARAMS = [ + BsonTypeTestCase( + id="input", + msg="$tanh should reject non-numeric input", + keyword="input", + valid_types=[ + BsonType.DOUBLE, + BsonType.INT, + BsonType.LONG, + BsonType.DECIMAL, + BsonType.NULL, + ], + default_error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + ), +] + +REJECTION_CASES = generate_bson_rejection_test_cases(TANH_BSON_PARAMS) +ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(TANH_BSON_PARAMS) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES) +def test_tanh_bson_type_rejected(collection, bson_type, sample_value, spec): + """Verifies $tanh rejects invalid BSON types.""" + result = execute_expression_with_insert( + collection, {"$tanh": "$value"}, {"value": sample_value} + ) + assert_expression_result( + result, + error_code=spec.expected_code(bson_type), + msg=f"{spec.msg}: {bson_type.value}", + ) + + +@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) +def test_tanh_bson_type_accepted(collection, bson_type, sample_value, spec): + """Verifies $tanh accepts valid numeric BSON types (and null).""" + result = execute_expression_with_insert( + collection, {"$tanh": "$value"}, {"value": sample_value} + ) + assertNotError(result, msg=f"$tanh should accept {bson_type.value}") diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_errors.py b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_errors.py new file mode 100644 index 000000000..4776d6500 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/trigonometry/tanh/test_tanh_errors.py @@ -0,0 +1,72 @@ +""" +$tanh errors. + +Tests that $tanh rejects a wrong argument count (16020) and non-numeric input (28765). +""" + +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 ( + EXPRESSION_TYPE_MISMATCH_ERROR, + NON_NUMERIC_TYPE_MISMATCH_ERROR, +) +from documentdb_tests.framework.parametrize import pytest_params +from documentdb_tests.framework.test_constants import ( + DOUBLE_HALF, +) + +TANH_ERRORS_TESTS: list[ExpressionTestCase] = [ + ExpressionTestCase( + "arity_zero_args", + expression={"$tanh": []}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject an empty argument array with error 16020", + ), + ExpressionTestCase( + "arity_two_args", + expression={"$tanh": [1, 2]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two arguments with error 16020", + ), + ExpressionTestCase( + "arity_three_args", + expression={"$tanh": [1, 2, 3]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject three arguments with error 16020", + ), + ExpressionTestCase( + "arity_two_nulls", + expression={"$tanh": [None, None]}, + error_code=EXPRESSION_TYPE_MISMATCH_ERROR, + msg="Should reject two null arguments with error 16020", + ), + ExpressionTestCase( + "nested_array_arg", + expression={"$tanh": [[DOUBLE_HALF]]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject an array argument inside the argument array with error 28765", + ), + ExpressionTestCase( + "array_field", + expression={"$tanh": "$v"}, + doc={"v": [DOUBLE_HALF]}, + error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR, + msg="Should reject a field holding an array with error 28765", + ), +] + + +@pytest.mark.parametrize("test", pytest_params(TANH_ERRORS_TESTS)) +def test_tanh_errors(collection, test): + """Test $tanh errors for literal and field-path arguments.""" + 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 + ) diff --git a/documentdb_tests/framework/error_codes.py b/documentdb_tests/framework/error_codes.py index 0a6544559..1df86850b 100644 --- a/documentdb_tests/framework/error_codes.py +++ b/documentdb_tests/framework/error_codes.py @@ -399,6 +399,9 @@ CLUSTERED_INFINITY_DUPLICATE_ERROR = 50826 RESUME_AFTER_START_AFTER_CONFLICT_ERROR = 50865 PLAN_CACHE_STATS_COLLECTION_NOT_FOUND_ERROR = 50933 +TRIG_INPUT_OUT_OF_RANGE_ERROR = 50989 +TWO_NUMERIC_ARGS_FIRST_TYPE_ERROR = 51044 +TWO_NUMERIC_ARGS_SECOND_TYPE_ERROR = 51045 LOOKUP_SUB_PIPELINE_NOT_ALLOWED_ERROR = 51047 REGEX_OPTIONS_BEFORE_REGEX_FLAGS_ERROR = 51074 REGEX_FLAGS_AND_OPTIONS_CONFLICT_ERROR = 51075