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,166 @@
"""Tests for $exp at representable-range boundaries, including overflow and underflow."""

import pytest
from bson import Decimal128

from documentdb_tests.compatibility.tests.core.operator.expressions.utils.expression_test_case import ( # noqa: E501
ExpressionTestCase,
)
from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import (
assert_expression_result,
execute_expression_with_insert,
)
from documentdb_tests.framework.parametrize import pytest_params
from documentdb_tests.framework.test_constants import (
DECIMAL128_INFINITY,
DECIMAL128_LARGE_EXPONENT,
DECIMAL128_MAX,
DECIMAL128_MIN,
DECIMAL128_SMALL_EXPONENT,
DOUBLE_MIN_NEGATIVE_SUBNORMAL,
DOUBLE_MIN_SUBNORMAL,
DOUBLE_NEAR_MAX,
DOUBLE_NEAR_MIN,
DOUBLE_ZERO,
FLOAT_INFINITY,
INT32_MAX,
INT64_MAX,
INT64_MIN,
)

# Property [Overflow]: $exp overflows to infinity past the largest representable exponent and
# underflows to zero for large negative inputs.
EXP_OVERFLOW_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"just_below_overflow",
doc={"value": 709},
expression={"$exp": ["$value"]},
expected=pytest.approx(8.218407461554972e307),
msg="$exp should return a large finite double just below the overflow boundary",
),
ExpressionTestCase(
"overflow",
doc={"value": 710},
expression={"$exp": ["$value"]},
expected=FLOAT_INFINITY,
msg="$exp should return infinity past the overflow boundary",
),
ExpressionTestCase(
"underflow",
doc={"value": -750},
expression={"$exp": ["$value"]},
expected=DOUBLE_ZERO,
msg="$exp should underflow to zero for a large negative input",
),
]

# Property [Integer Boundaries]: $exp of extreme integer inputs overflows to infinity or
# underflows to zero.
EXP_INTEGER_BOUNDARY_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"int32_max",
doc={"value": INT32_MAX},
expression={"$exp": ["$value"]},
expected=FLOAT_INFINITY,
msg="$exp should return infinity for INT32_MAX",
),
ExpressionTestCase(
"int64_max",
doc={"value": INT64_MAX},
expression={"$exp": ["$value"]},
expected=FLOAT_INFINITY,
msg="$exp should return infinity for INT64_MAX",
),
ExpressionTestCase(
"int64_min",
doc={"value": INT64_MIN},
expression={"$exp": ["$value"]},
expected=DOUBLE_ZERO,
msg="$exp should underflow to zero for INT64_MIN",
),
]

# Property [Double Boundaries]: $exp near the double subnormal range returns one, and a large
# double overflows to infinity.
EXP_DOUBLE_BOUNDARY_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"double_min_subnormal",
doc={"value": DOUBLE_MIN_SUBNORMAL},
expression={"$exp": ["$value"]},
expected=1.0,
msg="$exp should return one for the minimum subnormal double",
),
ExpressionTestCase(
"double_min_negative_subnormal",
doc={"value": DOUBLE_MIN_NEGATIVE_SUBNORMAL},
expression={"$exp": ["$value"]},
expected=1.0,
msg="$exp should return one for the minimum negative subnormal double",
),
ExpressionTestCase(
"double_near_min",
doc={"value": DOUBLE_NEAR_MIN},
expression={"$exp": ["$value"]},
expected=1.0,
msg="$exp should return one for a near-zero positive double",
),
ExpressionTestCase(
"double_near_max",
doc={"value": DOUBLE_NEAR_MAX},
expression={"$exp": ["$value"]},
expected=FLOAT_INFINITY,
msg="$exp should overflow to infinity for a large double",
),
Comment thread
danielfrankcom marked this conversation as resolved.
]

# Property [Decimal128 Boundaries]: $exp of extreme decimal128 exponents overflows to infinity or
# underflows to zero, preserving the decimal type.
EXP_DECIMAL_BOUNDARY_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"decimal_small_exponent",
doc={"value": DECIMAL128_SMALL_EXPONENT},
expression={"$exp": ["$value"]},
expected=Decimal128("1"),
msg="$exp should return one for a decimal128 with a small exponent",
),
ExpressionTestCase(
"decimal_large_exponent",
doc={"value": DECIMAL128_LARGE_EXPONENT},
expression={"$exp": ["$value"]},
expected=DECIMAL128_INFINITY,
msg="$exp should overflow to infinity for a decimal128 with a large exponent",
),
ExpressionTestCase(
"decimal_max",
doc={"value": DECIMAL128_MAX},
expression={"$exp": ["$value"]},
expected=DECIMAL128_INFINITY,
msg="$exp should overflow to infinity for decimal128 max",
),
ExpressionTestCase(
"decimal_min",
doc={"value": DECIMAL128_MIN},
expression={"$exp": ["$value"]},
expected=Decimal128("0E-6176"),
msg="$exp should underflow to zero for decimal128 min",
),
]

EXP_BOUNDARY_ALL_TESTS = (
EXP_OVERFLOW_TESTS
+ EXP_INTEGER_BOUNDARY_TESTS
+ EXP_DOUBLE_BOUNDARY_TESTS
+ EXP_DECIMAL_BOUNDARY_TESTS
)


@pytest.mark.parametrize("test_case", pytest_params(EXP_BOUNDARY_ALL_TESTS))
def test_exp_boundaries(collection, test_case: ExpressionTestCase):
"""Test $exp representable-range boundary cases."""
result = execute_expression_with_insert(collection, test_case.expression, test_case.doc)
assert_expression_result(
result,
expected=test_case.expected,
error_code=test_case.error_code,
msg=test_case.msg,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Tests for $exp type and arity errors."""

from datetime import datetime, timezone

import pytest
from bson import Binary, Code, MaxKey, MinKey, ObjectId, Regex, Timestamp

from documentdb_tests.compatibility.tests.core.operator.expressions.utils.expression_test_case import ( # noqa: E501
ExpressionTestCase,
)
from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import (
assert_expression_result,
execute_expression_with_insert,
)
from documentdb_tests.framework.error_codes import (
EXPRESSION_TYPE_MISMATCH_ERROR,
NON_NUMERIC_TYPE_MISMATCH_ERROR,
)
from documentdb_tests.framework.parametrize import pytest_params

# Property [Type Strictness]: $exp rejects non-numeric input types.
EXP_TYPE_ERROR_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
f"type_{tid}",
doc={"value": val},
expression={"$exp": ["$value"]},
error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR,
msg=f"$exp should reject a {tid} input",
)
for tid, val in [
("string", "abc"),
("bool", True),
("array", [1, 2]),
("object", {"a": 1}),
("datetime", datetime(2024, 1, 1, tzinfo=timezone.utc)),
("objectid", ObjectId("507f1f77bcf86cd799439011")),
("regex", Regex("abc")),
("binary", Binary(b"data")),
("minkey", MinKey()),
("maxkey", MaxKey()),
("timestamp", Timestamp(1, 1)),
("code", Code("function(){}")),
]
]

# Property [Arity]: $exp requires exactly one argument.
EXP_ARITY_ERROR_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"arity_zero",
doc={},
expression={"$exp": []},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="$exp should reject zero arguments",
),
ExpressionTestCase(
"arity_two",
doc={},
expression={"$exp": [1, 2]},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="$exp should reject two arguments",
),
]

EXP_ERROR_ALL_TESTS = EXP_TYPE_ERROR_TESTS + EXP_ARITY_ERROR_TESTS


@pytest.mark.parametrize("test_case", pytest_params(EXP_ERROR_ALL_TESTS))
def test_exp_errors(collection, test_case: ExpressionTestCase):
"""Test $exp type and arity error cases."""
result = execute_expression_with_insert(collection, test_case.expression, test_case.doc)
assert_expression_result(
result,
expected=test_case.expected,
error_code=test_case.error_code,
msg=test_case.msg,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Tests for $exp argument forms, literal input, field paths, and nested expression input."""

import pytest

from documentdb_tests.compatibility.tests.core.operator.expressions.utils.expression_test_case import ( # noqa: E501
ExpressionTestCase,
)
from documentdb_tests.compatibility.tests.core.operator.expressions.utils.utils import (
assert_expression_result,
execute_expression_with_insert,
)
from documentdb_tests.framework.error_codes import NON_NUMERIC_TYPE_MISMATCH_ERROR
from documentdb_tests.framework.parametrize import pytest_params

# Property [Argument Form]: $exp accepts its single argument bare or wrapped in a one-element
# array.
EXP_ARGUMENT_FORM_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"form_array",
doc={"value": 1},
expression={"$exp": ["$value"]},
expected=pytest.approx(2.718281828459045),
msg="$exp should accept its argument wrapped in a one-element array",
),
ExpressionTestCase(
"form_bare",
doc={"value": 1},
expression={"$exp": "$value"},
expected=pytest.approx(2.718281828459045),
msg="$exp should accept its argument without an array wrapper",
),
]

# Property [Literal Input]: $exp evaluates an inline literal argument, not only document fields.
EXP_LITERAL_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"literal_input",
doc={},
expression={"$exp": [1]},
expected=pytest.approx(2.718281828459045),
msg="$exp should return e for an inline literal argument",
),
]

# Property [Expression Input]: $exp evaluates a nested expression argument before exponentiating.
EXP_EXPRESSION_INPUT_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"nested_exp",
doc={},
expression={"$exp": {"$exp": 1}},
expected=pytest.approx(15.154262241479262),
msg="$exp should evaluate a nested $exp expression argument",
),
]

# Property [Field Path Input]: $exp resolves a field path argument. A dotted path into a nested
# object yields the referenced value; a path over an array (an array-of-objects path, or a numeric
# component applied over an array) resolves to an array, which $exp rejects as a non-numeric type.
EXP_FIELD_PATH_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"nested_field_path",
doc={"a": {"b": 1}},
expression={"$exp": "$a.b"},
expected=pytest.approx(2.718281828459045),
msg="$exp should resolve a dotted field path into a nested object",
),
ExpressionTestCase(
"composite_array_field_path",
doc={"a": [{"b": 1}, {"b": 2}]},
expression={"$exp": "$a.b"},
error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR,
msg="$exp should reject a field path that resolves to an array from an array of objects",
),
ExpressionTestCase(
"array_index_field_path",
doc={"a": [1, 2]},
expression={"$exp": "$a.0"},
error_code=NON_NUMERIC_TYPE_MISMATCH_ERROR,
msg="$exp should reject a numeric path component over an array, which resolves non-numeric",
),
]

EXP_INPUT_FORM_TESTS = (
EXP_ARGUMENT_FORM_TESTS + EXP_LITERAL_TESTS + EXP_EXPRESSION_INPUT_TESTS + EXP_FIELD_PATH_TESTS
)


@pytest.mark.parametrize("test_case", pytest_params(EXP_INPUT_FORM_TESTS))
def test_exp_input_forms(collection, test_case: ExpressionTestCase):
"""Test $exp argument form, literal, field path, and nested expression input cases."""
result = execute_expression_with_insert(collection, test_case.expression, test_case.doc)
assert_expression_result(
result,
expected=test_case.expected,
error_code=test_case.error_code,
msg=test_case.msg,
)
Loading
Loading