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,128 @@
"""
Tests for $cmp argument handling.

Covers argument validation (error cases), field references, return type verification,
and nested $cmp expressions.
"""

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_with_insert,
)
from documentdb_tests.framework.error_codes import EXPRESSION_TYPE_MISMATCH_ERROR
from documentdb_tests.framework.parametrize import pytest_params

ARG_TESTS: list[ExpressionTestCase] = [
Comment thread
vic-tsang marked this conversation as resolved.
ExpressionTestCase(
"no_args",
expression={"$cmp": []},
doc={"a": 1},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for empty arguments",
),
ExpressionTestCase(
"single_arg",
expression={"$cmp": ["$a"]},
doc={"a": 1},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for single argument",
),
ExpressionTestCase(
"non_array_int",
expression={"$cmp": 1},
doc={"a": 1},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for non-array int argument",
),
ExpressionTestCase(
"non_array_string",
expression={"$cmp": "string"},
doc={"a": 1},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for non-array string argument",
),
ExpressionTestCase(
"non_array_object",
expression={"$cmp": {"a": 1}},
doc={"a": 1},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for non-array object argument",
),
ExpressionTestCase(
"non_array_boolean",
expression={"$cmp": True},
doc={"a": 1},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for non-array boolean argument",
),
ExpressionTestCase(
"three_args",
expression={"$cmp": ["$a", "$b", "$c"]},
doc={"a": 1, "b": 2, "c": 3},
error_code=EXPRESSION_TYPE_MISMATCH_ERROR,
msg="Should error for three arguments",
),
]


FIELD_REF_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"field_ref_lt",
expression={"$cmp": ["$qty", 250]},
doc={"qty": 200},
expected=-1,
msg="Field reference less than literal",
),
ExpressionTestCase(
"field_ref_eq",
expression={"$cmp": ["$qty", 250]},
doc={"qty": 250},
expected=0,
msg="Field reference equals literal",
),
ExpressionTestCase(
"field_ref_gt",
expression={"$cmp": ["$qty", 250]},
doc={"qty": 300},
expected=1,
msg="Field reference greater than literal",
),
]


RETURN_TYPE_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"return_type_neg1",
expression={"$type": {"$cmp": [1, 2]}},
doc={"a": 1},
expected="int",
msg="$cmp should return BSON int type",
),
]


NESTED_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"nested_cmp",
expression={"$cmp": [{"$cmp": [1, 2]}, -1]},
doc={"a": 1},
expected=0,
msg="Nested $cmp(-1) equals -1",
),
]

ALL_TESTS = ARG_TESTS + FIELD_REF_TESTS + RETURN_TYPE_TESTS + NESTED_TESTS


@pytest.mark.parametrize("test", pytest_params(ALL_TESTS))
def test_cmp_argument_handling(collection, test):
"""Test $cmp argument validation, field references, return type, and nesting."""
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,106 @@
"""
Tests for $cmp boundary values and precision.

Covers cross-type boundary values, large number precision at double/long boundary,
and overflow adjacency.
"""

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_MAX_SAFE_INTEGER,
DOUBLE_NEGATIVE_ZERO,
DOUBLE_PRECISION_LOSS,
INT32_MAX,
INT32_MIN,
INT32_OVERFLOW,
INT32_UNDERFLOW,
INT64_MAX,
INT64_MIN,
)

BOUNDARY_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"int32_max_self",
expression={"$cmp": [INT32_MAX, INT32_MAX]},
expected=0,
msg="INT32_MAX equals itself",
),
ExpressionTestCase(
"int32_max_vs_overflow",
expression={"$cmp": [INT32_MAX, INT32_OVERFLOW]},
expected=-1,
msg="INT32_MAX < INT32_OVERFLOW (promoted to long)",
),
ExpressionTestCase(
"int32_min_vs_underflow",
expression={"$cmp": [INT32_MIN, INT32_UNDERFLOW]},
expected=1,
msg="INT32_MIN > INT32_UNDERFLOW (promoted to long)",
),
ExpressionTestCase(
"int32_max_vs_int64_max",
expression={"$cmp": [INT32_MAX, INT64_MAX]},
expected=-1,
msg="INT32_MAX < INT64_MAX",
),
ExpressionTestCase(
"int64_min_vs_int32_min",
expression={"$cmp": [INT64_MIN, INT32_MIN]},
expected=-1,
msg="INT64_MIN < INT32_MIN",
),
Comment thread
vic-tsang marked this conversation as resolved.
Comment thread
vic-tsang marked this conversation as resolved.
ExpressionTestCase(
"int64_max_self",
expression={"$cmp": [INT64_MAX, INT64_MAX]},
expected=0,
msg="INT64_MAX equals itself",
),
]


LARGE_NUMBER_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"beyond_double_precision",
expression={"$cmp": [Int64(DOUBLE_PRECISION_LOSS), float(DOUBLE_PRECISION_LOSS)]},
expected=1,
msg="Int64(2^53+1) > double(2^53+1) — beyond double precision",
),
ExpressionTestCase(
"exactly_representable",
expression={"$cmp": [Int64(DOUBLE_MAX_SAFE_INTEGER), float(DOUBLE_MAX_SAFE_INTEGER)]},
expected=0,
msg="Int64(2^53) vs double(2^53) — exactly representable",
),
ExpressionTestCase(
"dec128_preserves_precision",
expression={"$cmp": [Decimal128(str(DOUBLE_PRECISION_LOSS)), Int64(DOUBLE_PRECISION_LOSS)]},
expected=0,
msg="Decimal128(2^53+1) equals Int64(2^53+1)",
),
ExpressionTestCase(
"neg_zero_vs_pos_zero",
expression={"$cmp": [DOUBLE_NEGATIVE_ZERO, 0]},
expected=0,
msg="-0.0 equals 0 (negative zero is equal to positive zero)",
),
]


ALL_TESTS = BOUNDARY_TESTS + LARGE_NUMBER_TESTS


@pytest.mark.parametrize("test", pytest_params(ALL_TESTS))
def test_literal(collection, test):
"""Test $cmp across numeric boundary values and cross-type precision edges."""
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,83 @@
"""
Tests for $cmp expression type smoke tests.

Covers literal, field path, expression operator, and array/object expression inputs.
"""

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,
execute_expression_with_insert,
)
from documentdb_tests.framework.parametrize import pytest_params

LITERAL_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"literal_lt",
expression={"$cmp": [5, 10]},
expected=-1,
msg="Literal 5 < 10",
),
ExpressionTestCase(
"literal_gt",
expression={"$cmp": [10, 5]},
expected=1,
msg="Literal 10 > 5",
),
ExpressionTestCase(
"literal_eq",
expression={"$cmp": [7, 7]},
expected=0,
msg="Literal 7 == 7",
),
ExpressionTestCase(
"literal_nested_expr",
expression={"$cmp": [{"$add": [1, 2]}, 4]},
expected=-1,
msg="Literal $add(1,2)=3 < 4",
),
]


@pytest.mark.parametrize("test", pytest_params(LITERAL_TESTS))
def test_cmp_expression_literal(collection, test):
"""Test $cmp with literal expression inputs."""
result = execute_expression(collection, test.expression)
assert_expression_result(result, expected=test.expected, msg=test.msg)


INSERT_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"field_path",
expression={"$cmp": ["$a", "$b"]},
doc={"a": 10, "b": 5},
expected=1,
msg="Field path $a(10) > $b(5)",
),
ExpressionTestCase(
"expression_operator",
expression={"$cmp": [{"$abs": "$x"}, 3]},
doc={"x": -5},
expected=1,
msg="abs(-5)=5 > 3",
),
ExpressionTestCase(
"array_expression",
expression={"$cmp": [["$x", "$y"], [1, 2]]},
doc={"x": 1, "y": 2},
expected=0,
msg="Array expression [1,2] == [1,2]",
),
]


@pytest.mark.parametrize("test", pytest_params(INSERT_TESTS))
def test_cmp_expression_with_insert(collection, test):
"""Test $cmp with field path and expression inputs."""
result = execute_expression_with_insert(collection, test.expression, test.doc)
assert_expression_result(result, expected=test.expected, msg=test.msg)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Tests for $cmp field lookup and array index paths.

Covers array vs scalar in BSON order and deep nested path resolution.
"""

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_with_insert,
)
from documentdb_tests.framework.parametrize import pytest_params

ALL_TESTS: list[ExpressionTestCase] = [
ExpressionTestCase(
"array_vs_scalar",
expression={"$cmp": ["$a", "$b"]},
doc={"a": [1, 2, 3], "b": 5},
expected=1,
msg="Array > number in BSON order",
),
ExpressionTestCase(
"array_element_by_element",
expression={"$cmp": ["$a", "$b"]},
doc={"a": [1, 2, 3], "b": [1, 2, 4]},
expected=-1,
msg="[1,2,3] < [1,2,4] element-by-element",
),
ExpressionTestCase(
"deep_nested_path",
expression={"$cmp": ["$a.b.c.d", 1]},
doc={"a": {"b": {"c": {"d": 1}}}},
expected=0,
msg="Deep nested path $a.b.c.d resolves to 1",
),
ExpressionTestCase(
"deep_nested_missing",
expression={"$cmp": ["$a.b.c.d", 1]},
doc={"a": {"x": 1}},
expected=-1,
msg="Missing intermediate field in deep path treated as null",
),
]


@pytest.mark.parametrize("test", pytest_params(ALL_TESTS))
def test_cmp_field_lookup(collection, test):
"""Test $cmp field lookup and array index paths."""
result = execute_expression_with_insert(collection, test.expression, test.doc)
assert_expression_result(result, expected=test.expected, msg=test.msg)
Loading
Loading