Skip to content
Merged
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,77 @@
"""
Tests for $gt argument handling.

Covers argument count variations and error cases.
"""

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 ( # noqa: E501
assert_expression_result,
execute_expression,
)
from documentdb_tests.framework.error_codes import EXPRESSION_TYPE_MISMATCH_ERROR
from documentdb_tests.framework.parametrize import pytest_params

GT_ARG_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"no_args",
expression={"$gt": []},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for empty arguments",
),
ExpressionTestCase(
"single_arg",
expression={"$gt": [1]},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for single argument",
),
ExpressionTestCase(
"non_array_arg",
expression={"$gt": 1},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for non-array argument",
),
ExpressionTestCase(
"non_array_string",
expression={"$gt": "string"},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for string argument",
),
ExpressionTestCase(
"three_args",
expression={"$gt": [3, 2, 1]},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for three arguments",
),
ExpressionTestCase(
"two_args_gt",
expression={"$gt": [2, 1]},
expected=True,
msg="Should return true when first > second",
),
ExpressionTestCase(
"two_args_eq",
expression={"$gt": [1, 1]},
expected=False,
msg="Should return false when equal",
),
ExpressionTestCase(
"two_args_lt",
expression={"$gt": [1, 2]},
expected=False,
msg="Should return false when first < second",
),
]


@pytest.mark.parametrize("test", pytest_params(GT_ARG_TESTS))
def test_gt_argument_handling(collection, test):
"""Test $gt argument count variations."""
result = execute_expression(collection, test.expression)
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,173 @@
"""
Tests for $gt numeric boundaries and double precision.

Covers INT32/INT64 boundaries, double subnormals, and large number precision edge cases.
"""

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 ( # noqa: E501
assert_expression_result,
execute_expression,
)
from documentdb_tests.framework.parametrize import pytest_params
from documentdb_tests.framework.test_constants import (
DOUBLE_MIN_NEGATIVE_SUBNORMAL,
DOUBLE_MIN_SUBNORMAL,
DOUBLE_NEAR_MAX,
DOUBLE_NEAR_MIN,
INT32_MAX,
INT32_MIN,
INT64_MAX,
INT64_MIN,
)

INT32_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"int32_max_gt_max_minus1",
expression={"$gt": [INT32_MAX, INT32_MAX - 1]},
Comment thread
vic-tsang marked this conversation as resolved.
expected=True,
msg="INT32_MAX > INT32_MAX-1",
),
ExpressionTestCase(
"int32_max_minus1_not_gt_max",
expression={"$gt": [INT32_MAX - 1, INT32_MAX]},
expected=False,
msg="INT32_MAX-1 not > INT32_MAX",
),
ExpressionTestCase(
"int32_max_self",
expression={"$gt": [INT32_MAX, INT32_MAX]},
expected=False,
msg="INT32_MAX not > INT32_MAX",
),
ExpressionTestCase(
"int32_min_plus1_gt_min",
expression={"$gt": [INT32_MIN + 1, INT32_MIN]},
Comment thread
vic-tsang marked this conversation as resolved.
expected=True,
msg="INT32_MIN+1 > INT32_MIN",
),
ExpressionTestCase(
"int32_min_self",
expression={"$gt": [INT32_MIN, INT32_MIN]},
expected=False,
msg="INT32_MIN not > INT32_MIN",
),
ExpressionTestCase(
"long_above_int32_gt_int32_max",
expression={"$gt": [Int64(INT32_MAX + 1), INT32_MAX]},
expected=True,
msg="long(INT32_MAX+1) > int(INT32_MAX)",
),
ExpressionTestCase(
"int32_max_not_gt_long_above",
expression={"$gt": [INT32_MAX, Int64(INT32_MAX + 1)]},
expected=False,
msg="int(INT32_MAX) not > long(INT32_MAX+1)",
),
ExpressionTestCase(
"long_below_int32_gt_int32_min",
expression={"$gt": [Int64(INT32_MIN - 1), INT32_MIN]},
expected=False,
msg="long(INT32_MIN-1) not > int(INT32_MIN)",
),
]

INT64_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"int64_max_gt_max_minus1",
expression={"$gt": [INT64_MAX, Int64(INT64_MAX - 1)]},
Comment thread
vic-tsang marked this conversation as resolved.
expected=True,
msg="INT64_MAX > INT64_MAX-1",
),
ExpressionTestCase(
"int64_max_self",
expression={"$gt": [INT64_MAX, INT64_MAX]},
expected=False,
msg="INT64_MAX not > INT64_MAX",
),
ExpressionTestCase(
"int64_min_plus1_gt_min",
expression={"$gt": [Int64(INT64_MIN + 1), INT64_MIN]},
expected=True,
msg="INT64_MIN+1 > INT64_MIN",
),
ExpressionTestCase(
"int64_min_self",
expression={"$gt": [INT64_MIN, INT64_MIN]},
expected=False,
msg="INT64_MIN not > INT64_MIN",
),
]

DOUBLE_PRECISION_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"near_max_self",
expression={"$gt": [DOUBLE_NEAR_MAX, DOUBLE_NEAR_MAX]},
expected=False,
msg="DOUBLE_NEAR_MAX not > itself",
),
ExpressionTestCase(
"subnormal_gt_zero",
expression={"$gt": [DOUBLE_MIN_SUBNORMAL, 0.0]},
expected=True,
msg="min subnormal > 0.0",
),
ExpressionTestCase(
"zero_gt_subnormal",
expression={"$gt": [0.0, DOUBLE_MIN_SUBNORMAL]},
expected=False,
msg="0.0 not > min subnormal",
),
ExpressionTestCase(
"neg_subnormal_gt_zero",
expression={"$gt": [DOUBLE_MIN_NEGATIVE_SUBNORMAL, 0.0]},
expected=False,
msg="neg subnormal not > 0.0",
),
ExpressionTestCase(
"zero_gt_neg_subnormal",
expression={"$gt": [0.0, DOUBLE_MIN_NEGATIVE_SUBNORMAL]},
expected=True,
msg="0.0 > neg subnormal",
),
ExpressionTestCase(
"near_min_gt_neg_subnormal",
expression={"$gt": [DOUBLE_NEAR_MIN, DOUBLE_MIN_NEGATIVE_SUBNORMAL]},
expected=True,
msg="DOUBLE_NEAR_MIN > neg subnormal",
),
]

LARGE_NUMBER_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"int64_beyond_double_precision",
expression={"$gt": [Int64(9007199254740993), Int64(9007199254740992)]},
expected=True,
msg="int64 beyond double precision",
),
ExpressionTestCase(
"dec_34_digit",
expression={
"$gt": [
Decimal128("9999999999999999999999999999999999"),
Decimal128("9999999999999999999999999999999998"),
]
},
expected=True,
msg="34-digit decimal comparison",
),
]

ALL_TESTS = INT32_TESTS + INT64_TESTS + DOUBLE_PRECISION_TESTS + LARGE_NUMBER_TESTS


@pytest.mark.parametrize("test", pytest_params(ALL_TESTS))
def test_gt_boundary_precision(collection, test):
"""Test $gt boundary values and precision."""
result = execute_expression(collection, test.expression)
assert_expression_result(result, expected=test.expected, msg=test.msg)
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""
Representative BSON comparison engine wiring tests for $gt.

A small sample of cross-type and special value comparisons to confirm $gt
delegates to the BSON comparison engine correctly. Not an exhaustive matrix —
full BSON ordering coverage lives in /core/data_types/bson_types/.
"""

from datetime import datetime, timezone

import pytest
from bson import Decimal128, Int64, MaxKey, MinKey

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 ( # noqa: E501
assert_expression_result,
execute_expression,
)
from documentdb_tests.framework.parametrize import pytest_params
from documentdb_tests.framework.test_constants import DOUBLE_NEGATIVE_ZERO, FLOAT_NAN

BSON_WIRING_TESTS: list[ExpressionTestCase] = [
# Cross-type ordering: number < string < object < array
Comment thread
vic-tsang marked this conversation as resolved.
ExpressionTestCase(
"string_gt_number", expression={"$gt": ["a", 1]}, expected=True, msg="string > number"
),
ExpressionTestCase(
"number_not_gt_string",
expression={"$gt": [1, "a"]},
expected=False,
msg="number not > string",
),
ExpressionTestCase(
"object_gt_string",
expression={"$gt": [{"a": 1}, "a"]},
expected=True,
msg="object > string",
),
ExpressionTestCase(
"string_not_gt_object",
expression={"$gt": ["a", {"a": 1}]},
expected=False,
msg="string not > object",
),
ExpressionTestCase(
"array_gt_object",
expression={"$gt": [[1], {"a": 1}]},
expected=True,
msg="array > object",
),
ExpressionTestCase(
"object_not_gt_array",
expression={"$gt": [{"a": 1}, [1]]},
expected=False,
msg="object not > array",
),
# bool vs number (bool > number in BSON ordering)
ExpressionTestCase(
"bool_gt_number",
expression={"$gt": [True, 1]},
expected=True,
msg="bool > number",
),
ExpressionTestCase(
"number_not_gt_bool",
expression={"$gt": [1, True]},
expected=False,
msg="number not > bool",
),
# date vs string (date > string in BSON ordering)
ExpressionTestCase(
"date_gt_string",
expression={"$gt": [datetime(2024, 1, 1, tzinfo=timezone.utc), "z"]},
expected=True,
msg="date > string",
),
ExpressionTestCase(
"string_not_gt_date",
expression={"$gt": ["z", datetime(2024, 1, 1, tzinfo=timezone.utc)]},
expected=False,
msg="string not > date",
),
# MinKey / MaxKey extremes
ExpressionTestCase(
"maxkey_gt_number", expression={"$gt": [MaxKey(), 1]}, expected=True, msg="MaxKey > number"
),
ExpressionTestCase(
"minkey_not_gt_number",
expression={"$gt": [MinKey(), 1]},
expected=False,
msg="MinKey not > number",
),
# Numeric equivalence across types
ExpressionTestCase(
"int_not_gt_equivalent_long",
expression={"$gt": [1, Int64(1)]},
expected=False,
msg="int(1) not > long(1)",
),
ExpressionTestCase(
"int_not_gt_equivalent_decimal",
expression={"$gt": [1, Decimal128("1")]},
expected=False,
msg="int(1) not > decimal(1)",
),
# Negative zero == positive zero
ExpressionTestCase(
"zero_not_gt_neg_zero",
expression={"$gt": [0.0, DOUBLE_NEGATIVE_ZERO]},
expected=False,
msg="0.0 not > -0.0",
),
# NaN ordering
ExpressionTestCase(
"nan_not_gt_nan",
expression={"$gt": [FLOAT_NAN, FLOAT_NAN]},
expected=False,
msg="NaN not > NaN",
),
ExpressionTestCase(
"zero_gt_nan", expression={"$gt": [0, FLOAT_NAN]}, expected=True, msg="0 > NaN"
),
]

Comment thread
vic-tsang marked this conversation as resolved.

@pytest.mark.parametrize("test", pytest_params(BSON_WIRING_TESTS))
def test_gt_bson_wiring(collection, test):
"""Smoke test: confirm $gt is wired to the BSON comparison engine."""
result = execute_expression(collection, test.expression)
assert_expression_result(result, expected=test.expected, msg=test.msg)
Loading
Loading