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

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


@pytest.mark.parametrize("test", pytest_params(TESTS))
def test_lt_bson_wiring(collection, test):
"""Parametrized test for $lt 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
Comment thread
vic-tsang marked this conversation as resolved.
)
assertSuccess(result, test.expected, ignore_doc_order=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Edge case tests for $lt operator.

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

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

_ARR_WITH_ZERO = list(range(1, 1001)) + [0]
_ARR_WITHOUT = list(range(1, 1001))

EDGE_CASE_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="deeply_nested_field_with_nan",
filter={"a.b.c.d.e": {"$lt": 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": 1, "a": {"b": {"c": {"d": {"e": 5}}}}}],
msg="$lt on deeply nested field; NaN does not satisfy $lt",
),
QueryTestCase(
id="large_array_element_match",
filter={"a": {"$lt": 1}},
doc=[{"_id": 1, "a": _ARR_WITH_ZERO}, {"_id": 2, "a": _ARR_WITHOUT}],
expected=[{"_id": 1, "a": _ARR_WITH_ZERO}],
msg="$lt matches element in a large (1001-element) array",
),
QueryTestCase(
id="empty_string_lt_nonempty",
filter={"a": {"$lt": "a"}},
doc=[{"_id": 1, "a": ""}, {"_id": 2, "a": "b"}],
expected=[{"_id": 1, "a": ""}],
msg="empty string is less than any non-empty string",
),
QueryTestCase(
id="null_query_no_match",
filter={"a": {"$lt": None}},
doc=[{"_id": 1, "a": 5}, {"_id": 2, "a": None}, {"_id": 3}],
expected=[],
msg="$lt null returns no documents",
),
QueryTestCase(
id="null_field_not_lt_numeric",
filter={"a": {"$lt": 5}},
doc=[{"_id": 1, "a": None}],
expected=[],
msg="null field does not match $lt with numeric query",
),
]


@pytest.mark.parametrize("test", pytest_params(EDGE_CASE_TESTS))
def test_lt_edge_cases(collection, test):
"""Parametrized test for $lt edge cases."""
collection.insert_many(test.doc)
result = execute_command(collection, {"find": collection.name, "filter": test.filter})
assertSuccess(result, test.expected, ignore_doc_order=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
"""
Tests for $lt field lookup and array value comparison.

Covers array element matching, array index access,
numeric key disambiguation, _id with ObjectId, array of embedded documents,
whole-array comparison, empty array behavior, and embedded document dot-notation.
"""

import pytest
from bson import ObjectId

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

FIELD_LOOKUP_TESTS: list[QueryTestCase] = [
Comment thread
vic-tsang marked this conversation as resolved.
QueryTestCase(
id="array_element_matching",
filter={"a": {"$lt": 5}},
doc=[{"_id": 1, "a": [3, 7, 12]}, {"_id": 2, "a": [10, 20]}],
expected=[{"_id": 1, "a": [3, 7, 12]}],
msg="$lt matches if any array element satisfies condition",
),
QueryTestCase(
id="array_no_element_match",
filter={"a": {"$lt": 5}},
doc=[{"_id": 1, "a": [10, 20]}],
expected=[],
msg="$lt on array with no element less than query",
),
QueryTestCase(
id="array_index_zero",
filter={"arr.0": {"$lt": 20}},
doc=[{"_id": 1, "arr": [10, 30]}, {"_id": 2, "arr": [25, 5]}],
expected=[{"_id": 1, "arr": [10, 30]}],
msg="$lt on array index 0",
),
QueryTestCase(
id="numeric_key_on_object",
filter={"a.0.b": {"$lt": 5}},
doc=[{"_id": 1, "a": {"0": {"b": 3}}}, {"_id": 2, "a": {"0": {"b": 10}}}],
expected=[{"_id": 1, "a": {"0": {"b": 3}}}],
msg="$lt with numeric key on object (not array)",
),
QueryTestCase(
id="id_objectid",
filter={"_id": {"$lt": ObjectId("507f1f77bcf86cd799439012")}},
doc=[
{"_id": ObjectId("507f1f77bcf86cd799439011"), "a": 1},
{"_id": ObjectId("507f1f77bcf86cd799439013"), "a": 2},
],
expected=[{"_id": ObjectId("507f1f77bcf86cd799439011"), "a": 1}],
msg="$lt on _id with ObjectId",
),
QueryTestCase(
id="array_of_embedded_docs_dot_notation",
filter={"a.b": {"$lt": 5}},
doc=[
{"_id": 1, "a": [{"b": 3}, {"b": 7}]},
{"_id": 2, "a": [{"b": 10}, {"b": 20}]},
],
expected=[{"_id": 1, "a": [{"b": 3}, {"b": 7}]}],
msg="$lt on array of embedded docs via dot notation",
),
]

ARRAY_VALUE_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="array_lt_array_element_comparison",
filter={"a": {"$lt": [1, 3]}},
doc=[{"_id": 1, "a": [1, 2]}],
expected=[{"_id": 1, "a": [1, 2]}],
msg="$lt array [1,2] < [1,3] via array comparison",
),
QueryTestCase(
id="shorter_array_prefix_lt_longer",
filter={"a": {"$lt": [1, 2, 3]}},
doc=[{"_id": 1, "a": [1, 2]}],
expected=[{"_id": 1, "a": [1, 2]}],
msg="$lt shorter array prefix is less than longer",
),
QueryTestCase(
id="empty_array_lt_nonempty_array",
filter={"a": {"$lt": [1]}},
doc=[{"_id": 1, "a": []}],
expected=[{"_id": 1, "a": []}],
msg="$lt empty array is less than non-empty array",
),
QueryTestCase(
id="array_with_null_element_lt_scalar",
filter={"a": {"$lt": 10}},
doc=[{"_id": 1, "a": [None, 5]}],
expected=[{"_id": 1, "a": [None, 5]}],
msg="$lt matches array with element 5 < 10",
),
QueryTestCase(
id="empty_array_not_lt_scalar",
filter={"a": {"$lt": 5}},
doc=[{"_id": 1, "a": []}],
expected=[],
msg="$lt empty array does not match scalar query",
),
]

EMBEDDED_DOC_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="embedded_field_lt",
filter={"carrier.fee": {"$lt": 4}},
doc=[
{"_id": 1, "item": "nuts", "carrier": {"name": "Shipit", "fee": 3}},
{"_id": 2, "item": "bolts", "carrier": {"name": "Shipit", "fee": 4}},
{"_id": 3, "item": "washers", "carrier": {"name": "Shipit", "fee": 1}},
],
expected=[
{"_id": 1, "item": "nuts", "carrier": {"name": "Shipit", "fee": 3}},
{"_id": 3, "item": "washers", "carrier": {"name": "Shipit", "fee": 1}},
],
msg="$lt on embedded field returns docs with fee < 4",
),
QueryTestCase(
id="embedded_field_missing_excluded",
filter={"carrier.fee": {"$lt": 10}},
doc=[
{"_id": 1, "carrier": {"fee": 3}},
{"_id": 2, "item": "no carrier"},
],
expected=[{"_id": 1, "carrier": {"fee": 3}}],
msg="$lt on embedded field excludes docs missing the embedded path",
),
]

ALL_TESTS = FIELD_LOOKUP_TESTS + ARRAY_VALUE_TESTS + EMBEDDED_DOC_TESTS


@pytest.mark.parametrize("test", pytest_params(ALL_TESTS))
def test_lt_field_lookup(collection, test):
"""Parametrized test for $lt field lookup and array comparison."""
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