Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Required] TEST_COVERAGE.md §2/§7 also want DECIMAL128_NEGATIVE_ZERO, a +0.0 double input, DOUBLE_MIN_SUBNORMAL and the Decimal128 boundary constants for each operator. Right now Decimal128 -0 is only in $atan2, the subnormal and Decimal128 limits are only in the two angle converters, and no single-input operator has a +0.0 double case (a Decimal128 zero was added instead).

DOUBLE_HALF,
DOUBLE_NEGATIVE_ZERO,
FLOAT_NAN,
MISSING,
)

ACOS_BASIC_TESTS: list[ExpressionTestCase] = [

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Could you group the cases with # Property [...]: comments (e.g. Numeric Types, Null/Missing, Non-Finite, Domain Bounds, Domain Errors, Arity), as docs/REVIEW.md §4 asks? This applies to all the new files.

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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nitpick] The "just inside the bound" cases are only on the +1 side. Could you add -0.9999999999999999 and Decimal128("-0.9999999999999999999999999999999999") for $acos, $asin and $atanh?

"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."""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Required] Thanks for adding field paths and the one-element array form. TEST_COVERAGE.md §3 also asks each operator for a nested expression input (e.g. {$sin: {$add: [...]}}), an object expression input, and a composite array path ("$a.b" over an array of documents). None of the 15 operators has these yet.

[Nitpick] This docstring says "literal and field-path arguments", but this list has no field-path case. The same is true for the asin, atanh, cos, sin and tan basic files, and for the atan2 errors file. Adding one field-path success case to each basic file would fix both points; otherwise please trim the docstrings. The acosh basic module docstring also lists -0.0, which is only in the errors file.

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
)
Original file line number Diff line number Diff line change
@@ -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}")
Original file line number Diff line number Diff line change
@@ -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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The domain-error cases still use only double and Decimal128 input. Could you add an int and a long out-of-domain case for each of $acos, $asin, $atanh and $acosh, e.g. {$acos: 2}, {$acos: Int64(-2)}, {$atanh: 2}, {$acosh: 0}, {$acosh: Int64(0)}, each expecting 50989?

"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
)
Loading
Loading