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,157 @@
"""
Tests for $gte BSON type wiring.

A representative sample of types to confirm $gte 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(
id="double",
filter={"a": {"$gte": 5.0}},
doc=[{"_id": 1, "a": 1.0}, {"_id": 2, "a": 5.0}, {"_id": 3, "a": 10.0}],
expected=[{"_id": 2, "a": 5.0}, {"_id": 3, "a": 10.0}],
msg="$gte with double returns docs with value >= 5.0",
),
QueryTestCase(
id="int",
filter={"a": {"$gte": 5}},
doc=[{"_id": 1, "a": 1}, {"_id": 2, "a": 5}, {"_id": 3, "a": 10}],
expected=[{"_id": 2, "a": 5}, {"_id": 3, "a": 10}],
msg="$gte with int returns docs with value >= 5",
),
QueryTestCase(
id="long",
filter={"a": {"$gte": Int64(5)}},
doc=[
{"_id": 1, "a": Int64(1)},
{"_id": 2, "a": Int64(5)},
{"_id": 3, "a": Int64(10)},
],
expected=[{"_id": 2, "a": Int64(5)}, {"_id": 3, "a": Int64(10)}],
msg="$gte with long returns docs with value >= 5",
),
QueryTestCase(
id="decimal128",
filter={"a": {"$gte": Decimal128("5")}},
doc=[
{"_id": 1, "a": Decimal128("1")},
{"_id": 2, "a": Decimal128("5")},
{"_id": 3, "a": Decimal128("10")},
],
expected=[{"_id": 2, "a": Decimal128("5")}, {"_id": 3, "a": Decimal128("10")}],
msg="$gte with decimal128 returns docs with value >= 5",
),
QueryTestCase(
id="string",
filter={"a": {"$gte": "banana"}},
doc=[
{"_id": 1, "a": "apple"},
{"_id": 2, "a": "banana"},
{"_id": 3, "a": "cherry"},
],
expected=[{"_id": 2, "a": "banana"}, {"_id": 3, "a": "cherry"}],
msg="$gte with string returns docs with value >= 'banana'",
),
QueryTestCase(
id="date",
filter={"a": {"$gte": 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": 2, "a": datetime(2023, 1, 1, tzinfo=timezone.utc)},
{"_id": 3, "a": datetime(2025, 1, 1, tzinfo=timezone.utc)},
],
msg="$gte with date returns docs with equal or later dates",
),
QueryTestCase(
id="timestamp",
filter={"a": {"$gte": Timestamp(2000, 1)}},
doc=[
{"_id": 1, "a": Timestamp(1000, 1)},
{"_id": 2, "a": Timestamp(2000, 1)},
{"_id": 3, "a": Timestamp(3000, 1)},
],
expected=[{"_id": 2, "a": Timestamp(2000, 1)}, {"_id": 3, "a": Timestamp(3000, 1)}],
msg="$gte with timestamp returns docs with equal or larger timestamp",
),
QueryTestCase(
id="objectid",
filter={"a": {"$gte": ObjectId("507f1f77bcf86cd799439012")}},
doc=[
{"_id": 1, "a": ObjectId("507f1f77bcf86cd799439011")},
{"_id": 2, "a": ObjectId("507f1f77bcf86cd799439012")},
{"_id": 3, "a": ObjectId("507f1f77bcf86cd799439013")},
],
expected=[
{"_id": 2, "a": ObjectId("507f1f77bcf86cd799439012")},
{"_id": 3, "a": ObjectId("507f1f77bcf86cd799439013")},
],
msg="$gte with ObjectId returns docs with equal or later ObjectId",
),
QueryTestCase(
id="boolean",
filter={"a": {"$gte": False}},
doc=[{"_id": 1, "a": False}, {"_id": 2, "a": True}],
expected=[{"_id": 1, "a": False}, {"_id": 2, "a": True}],
msg="$gte with boolean false returns both true and false",
),
QueryTestCase(
id="bindata",
filter={"a": {"$gte": Binary(b"\x05\x06", 128)}},
doc=[
{"_id": 1, "a": Binary(b"\x01\x02", 128)},
{"_id": 2, "a": Binary(b"\x05\x06", 128)},
{"_id": 3, "a": Binary(b"\x09\x0a", 128)},
],
expected=[
{"_id": 2, "a": Binary(b"\x05\x06", 128)},
{"_id": 3, "a": Binary(b"\x09\x0a", 128)},
],
msg="$gte with BinData returns docs with equal or larger binary",
),
QueryTestCase(
id="minkey",
filter={"a": {"$gte": MinKey()}},
doc=[{"_id": 1, "a": MinKey()}, {"_id": 2, "a": 1}],
expected=[{"_id": 1, "a": MinKey()}, {"_id": 2, "a": 1}],
msg="$gte with MinKey returns all docs",
),
QueryTestCase(
id="maxkey",
filter={"a": {"$gte": MaxKey()}},
doc=[
{"_id": 1, "a": 1},
{"_id": 2, "a": "hello"},
{"_id": 3, "a": MaxKey()},
],
expected=[{"_id": 3, "a": MaxKey()}],
msg="$gte with MaxKey returns only MaxKey doc",
),
]


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

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

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": {"$gte": 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="$gte on deeply nested field path; NaN does not satisfy $gte",
),
QueryTestCase(
id="large_array_element_match",
filter={"a": {"$gte": 1001}},
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="$gte matches element in a large (1001-element) array",
),
]

NULL_MISSING_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="null_query_matches_null_and_missing",
filter={"a": {"$gte": None}},
doc=[{"_id": 1, "a": 5}, {"_id": 2, "a": None}, {"_id": 3}],
expected=[{"_id": 2, "a": None}, {"_id": 3}],
msg="$gte null matches null and missing fields (null >= null)",
),
QueryTestCase(
id="null_field_not_gte_numeric",
filter={"a": {"$gte": 5}},
doc=[{"_id": 1, "a": None}],
expected=[],
msg="null field does not match $gte with numeric query",
),
]

TYPE_BRACKETING_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="gte_false_no_cross_type_int_zero",
filter={"a": {"$gte": False}},
doc=[{"_id": 1, "a": 0}],
expected=[],
msg="int 0 does not match $gte false (different BSON types)",
),
QueryTestCase(
id="gte_true_no_cross_type_int_one",
filter={"a": {"$gte": True}},
doc=[{"_id": 1, "a": 1}],
expected=[],
msg="int 1 does not match $gte true (different BSON types)",
),
QueryTestCase(
id="gte_int_zero_no_cross_type_false",
filter={"a": {"$gte": 0}},
doc=[{"_id": 1, "a": False}],
expected=[],
msg="false does not match $gte 0 (different BSON types)",
),
QueryTestCase(
id="gte_int_one_no_cross_type_true",
filter={"a": {"$gte": 1}},
doc=[{"_id": 1, "a": True}],
expected=[],
msg="true does not match $gte 1 (different BSON types)",
),
QueryTestCase(
id="gte_int_zero_no_cross_type_string",
filter={"a": {"$gte": 0}},
doc=[{"_id": 1, "a": "0"}],
expected=[],
msg="string '0' does not match $gte int 0 (different BSON types)",
),
QueryTestCase(
id="gte_string_no_cross_type_int",
filter={"a": {"$gte": "0"}},
doc=[{"_id": 1, "a": 0}],
expected=[],
msg="int 0 does not match $gte string '0' (different BSON types)",
),
QueryTestCase(
id="gte_null_no_cross_type_string",
filter={"a": {"$gte": None}},
doc=[{"_id": 1, "a": "hello"}],
expected=[],
msg="string does not match $gte null (different BSON types)",
Comment thread
vic-tsang marked this conversation as resolved.
),
QueryTestCase(
id="gte_false_no_cross_type_null",
filter={"a": {"$gte": False}},
doc=[{"_id": 1, "a": None}],
expected=[],
msg="null does not match $gte false (different BSON types)",
),
QueryTestCase(
id="gte_int_zero_no_cross_type_null",
filter={"a": {"$gte": 0}},
doc=[{"_id": 1, "a": None}],
expected=[],
msg="null does not match $gte int 0 (different BSON types)",
),
QueryTestCase(
id="gte_null_no_cross_type_bool",
filter={"a": {"$gte": None}},
doc=[{"_id": 1, "a": True}],
expected=[],
msg="bool true does not match $gte null (different BSON types)",
),
QueryTestCase(
id="bool_false_not_gte_true",
filter={"a": {"$gte": True}},
doc=[{"_id": 1, "a": False}],
expected=[],
msg="bool false does not match $gte true (false < true)",
),
]

ALL_PARAMETRIZED_TESTS = MISC_EDGE_CASE_TESTS + NULL_MISSING_TESTS + TYPE_BRACKETING_TESTS


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

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