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
1 change: 1 addition & 0 deletions docs/testing/TEST_COVERAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Example path: `documentdb_tests/compatibility/tests/core/operator/expressions/ar
- Date + NaN (should fail)
- Date + non-numeric types (should fail)
- **Overflow**: Date + LONG_MAX (should fail)
- **Timezone awareness**: Use `CodecOptions(tz_aware=True, tzinfo=timezone.utc)` to verify timezone-aware datetime decoding, e.g. `datetime(2024, 1, 1, tzinfo=timezone.utc)`

---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""
Tests for $gt BSON type wiring.

A representative sample of types to confirm $gt is wired up to the
BSON comparison engine correctly (not exhaustive cross-type matrix).
"""

from datetime import datetime, timezone

import pytest
from bson import Binary, Decimal128, Int64, MaxKey, MinKey, ObjectId, Timestamp
from bson.codec_options import CodecOptions

from documentdb_tests.compatibility.tests.core.operator.query.utils.query_test_case import (
QueryTestCase,
)
from documentdb_tests.framework.assertions import assertSuccess
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params

TESTS: list[QueryTestCase] = [
QueryTestCase(
Comment thread
vic-tsang marked this conversation as resolved.
id="double",
filter={"a": {"$gt": 5.0}},
doc=[{"_id": 1, "a": 1.0}, {"_id": 2, "a": 5.0}, {"_id": 3, "a": 10.0}],
expected=[{"_id": 3, "a": 10.0}],
msg="$gt with double returns docs with value > 5.0",
),
QueryTestCase(
id="int",
filter={"a": {"$gt": 5}},
doc=[{"_id": 1, "a": 1}, {"_id": 2, "a": 5}, {"_id": 3, "a": 10}],
expected=[{"_id": 3, "a": 10}],
msg="$gt with int returns docs with value > 5",
),
QueryTestCase(
id="long",
filter={"a": {"$gt": Int64(5)}},
doc=[
{"_id": 1, "a": Int64(1)},
{"_id": 2, "a": Int64(5)},
{"_id": 3, "a": Int64(10)},
],
expected=[{"_id": 3, "a": Int64(10)}],
msg="$gt with long returns docs with value > 5",
),
QueryTestCase(
id="decimal128",
filter={"a": {"$gt": Decimal128("5")}},
doc=[
{"_id": 1, "a": Decimal128("1")},
{"_id": 2, "a": Decimal128("5")},
{"_id": 3, "a": Decimal128("10")},
],
expected=[{"_id": 3, "a": Decimal128("10")}],
msg="$gt with decimal128 returns docs with value > 5",
),
QueryTestCase(
id="string",
filter={"a": {"$gt": "banana"}},
doc=[
{"_id": 1, "a": "apple"},
{"_id": 2, "a": "banana"},
{"_id": 3, "a": "cherry"},
],
expected=[{"_id": 3, "a": "cherry"}],
msg="$gt with string returns docs with value > 'banana'",
),
QueryTestCase(
id="date",
filter={"a": {"$gt": datetime(2023, 1, 1, tzinfo=timezone.utc)}},
doc=[
{"_id": 1, "a": datetime(2020, 1, 1, tzinfo=timezone.utc)},
{"_id": 2, "a": datetime(2023, 1, 1, tzinfo=timezone.utc)},
{"_id": 3, "a": datetime(2025, 1, 1, tzinfo=timezone.utc)},
],
expected=[{"_id": 3, "a": datetime(2025, 1, 1, tzinfo=timezone.utc)}],
msg="$gt with date returns docs with later dates",
),
QueryTestCase(
id="timestamp",
filter={"a": {"$gt": Timestamp(2000, 1)}},
doc=[
{"_id": 1, "a": Timestamp(1000, 1)},
{"_id": 2, "a": Timestamp(3000, 1)},
],
expected=[{"_id": 2, "a": Timestamp(3000, 1)}],
msg="$gt with timestamp returns docs with larger timestamp",
),
QueryTestCase(
id="objectid",
filter={"a": {"$gt": ObjectId("507f1f77bcf86cd799439012")}},
doc=[
{"_id": 1, "a": ObjectId("507f1f77bcf86cd799439011")},
{"_id": 2, "a": ObjectId("507f1f77bcf86cd799439013")},
],
expected=[{"_id": 2, "a": ObjectId("507f1f77bcf86cd799439013")}],
msg="$gt with ObjectId returns docs with later ObjectId",
),
QueryTestCase(
id="boolean",
filter={"a": {"$gt": False}},
doc=[{"_id": 1, "a": False}, {"_id": 2, "a": True}],
expected=[{"_id": 2, "a": True}],
msg="$gt with boolean false returns doc with true",
),
QueryTestCase(
id="bindata",
filter={"a": {"$gt": Binary(b"\x05\x06", 128)}},
doc=[
{"_id": 1, "a": Binary(b"\x01\x02", 128)},
{"_id": 2, "a": Binary(b"\x09\x0a", 128)},
],
expected=[{"_id": 2, "a": Binary(b"\x09\x0a", 128)}],
msg="$gt with BinData returns docs with larger binary",
),
QueryTestCase(
id="minkey",
filter={"a": {"$gt": MinKey()}},
doc=[{"_id": 1, "a": MinKey()}, {"_id": 2, "a": 1}],
expected=[{"_id": 2, "a": 1}],
msg="$gt with MinKey returns all non-MinKey docs",
),
QueryTestCase(
id="maxkey",
filter={"a": {"$gt": MaxKey()}},
doc=[
{"_id": 1, "a": 1},
{"_id": 2, "a": "hello"},
{"_id": 3, "a": MaxKey()},
],
expected=[],
msg="$gt with MaxKey returns nothing",
),
]


@pytest.mark.parametrize("test", pytest_params(TESTS))
def test_gt_bson_wiring(collection, test):
"""Parametrized test for $gt BSON type wiring."""
collection.insert_many(test.doc)
codec = CodecOptions(tz_aware=True, tzinfo=timezone.utc)
result = execute_command(
collection, {"find": collection.name, "filter": test.filter}, codec_options=codec
)
assertSuccess(result, test.expected, ignore_doc_order=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
"""
Edge case tests for $gt operator.

Covers deeply nested field paths with NaN, large array element matching,
empty string ordering, null/missing field handling, and cross-type bracketing.
"""

from datetime import datetime, timezone

import pytest

from documentdb_tests.compatibility.tests.core.operator.query.utils.query_test_case import (
QueryTestCase,
)
from documentdb_tests.framework.assertions import assertSuccess
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params

MISC_EDGE_CASE_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="deeply_nested_field_with_nan",
filter={"a.b.c.d.e": {"$gt": 10}},
doc=[
{"_id": 1, "a": {"b": {"c": {"d": {"e": 5}}}}},
{"_id": 2, "a": {"b": {"c": {"d": {"e": 15}}}}},
{"_id": 3, "a": {"b": {"c": {"d": {"e": float("nan")}}}}},
],
expected=[{"_id": 2, "a": {"b": {"c": {"d": {"e": 15}}}}}],
msg="$gt on deeply nested field path; NaN does not satisfy $gt",
),
QueryTestCase(
id="large_array_element_match",
filter={"a": {"$gt": 1000}},
doc=[
{"_id": 1, "a": list(range(0, 1000)) + [1001]},
{"_id": 2, "a": list(range(0, 1000))},
],
expected=[{"_id": 1, "a": list(range(0, 1000)) + [1001]}],
msg="$gt matches element in a large (1001-element) array",
),
QueryTestCase(
id="empty_string_gt_nothing",
filter={"a": {"$gt": ""}},
doc=[{"_id": 1, "a": ""}, {"_id": 2, "a": "a"}],
expected=[{"_id": 2, "a": "a"}],
msg="non-empty string is greater than empty string",
),
]

NULL_MISSING_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="gt_null_query_returns_nothing",
filter={"a": {"$gt": None}},
doc=[{"_id": 1, "a": 5}, {"_id": 2, "a": None}, {"_id": 3}],
expected=[],
msg="$gt null returns no documents",
),
QueryTestCase(
id="null_field_not_gt_numeric_query",
filter={"a": {"$gt": 5}},
doc=[{"_id": 1, "a": None}],
expected=[],
msg="null field does not match $gt with numeric query",
),
QueryTestCase(
id="missing_field_not_gt_numeric_query",
filter={"a": {"$gt": 5}},
doc=[{"_id": 1, "b": 10}],
expected=[],
msg="missing field does not match $gt numeric query",
),
QueryTestCase(
id="gt_null_on_missing_field_no_match",
filter={"a": {"$gt": None}},
doc=[{"_id": 1, "b": 10}],
expected=[],
msg="$gt null on missing field returns no match",
),
QueryTestCase(
id="gt_null_on_non_null_field_no_match",
filter={"a": {"$gt": None}},
doc=[{"_id": 1, "a": 5}],
expected=[],
msg="$gt null on non-null field returns no match (type bracketing)",
),
]

TYPE_BRACKETING_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="bool_false_field_not_gt_int_0_query",
filter={"a": {"$gt": 0}},
doc=[{"_id": 1, "a": False}],
expected=[],
msg="boolean false is not > int 0 (different type brackets)",
),
QueryTestCase(
id="bool_true_field_not_gt_int_1_query",
filter={"a": {"$gt": 1}},
doc=[{"_id": 1, "a": True}],
expected=[],
msg="boolean true is not > int 1 (different type brackets)",
),
QueryTestCase(
id="date_field_not_gt_numeric_query",
filter={"a": {"$gt": 0}},
doc=[{"_id": 1, "a": datetime(2025, 1, 1, tzinfo=timezone.utc)}],
expected=[],
msg="date field does not match $gt numeric query (different type brackets)",
),
QueryTestCase(
id="null_field_not_gt_string_query",
filter={"a": {"$gt": "abc"}},
doc=[{"_id": 1, "a": None}],
expected=[],
msg="null field does not match $gt string query (different type brackets)",
),
QueryTestCase(
id="object_not_gt_null",
filter={"a": {"$gt": None}},
doc=[{"_id": 1, "a": {}}],
expected=[],
msg="empty object does not match $gt null (different BSON types)",
),
]

ALL_PARAMETRIZED_TESTS = MISC_EDGE_CASE_TESTS + NULL_MISSING_TESTS + TYPE_BRACKETING_TESTS


@pytest.mark.parametrize("test", pytest_params(ALL_PARAMETRIZED_TESTS))
def test_gt_edge_cases(collection, test):
"""Parametrized test for $gt edge cases.

Covers nested fields, large arrays, null/missing, and type bracketing.
"""
collection.insert_many(test.doc)
result = execute_command(collection, {"find": collection.name, "filter": test.filter})
assertSuccess(result, test.expected, ignore_doc_order=True)
Loading
Loading