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

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


@pytest.mark.parametrize("test", pytest_params(TESTS))
def test_lte_bson_wiring(collection, test):
"""Parametrized test for $lte 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,68 @@
"""
Edge case tests for $lte operator.

Covers deeply nested field paths with NaN, large array element matching,
empty string ordering, and 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

EDGE_CASE_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="deeply_nested_field_with_nan",
filter={"a.b.c.d.e": {"$lte": 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="$lte on deeply nested field; NaN does not satisfy $lte",
),
QueryTestCase(
id="large_array_element_match",
filter={"a": {"$lte": 0}},
doc=[
{"_id": 1, "a": list(range(1, 1001)) + [0]},
{"_id": 2, "a": list(range(1, 1001))},
],
expected=[{"_id": 1, "a": list(range(1, 1001)) + [0]}],
msg="$lte matches element in a large (1001-element) array",
),
QueryTestCase(
id="empty_string_lte_itself",
filter={"a": {"$lte": ""}},
doc=[{"_id": 1, "a": ""}, {"_id": 2, "a": "a"}],
expected=[{"_id": 1, "a": ""}],
msg="empty string matches $lte for itself",
),
QueryTestCase(
id="null_query_matches_null_and_missing",
filter={"a": {"$lte": None}},
doc=[{"_id": 1, "a": 5}, {"_id": 2, "a": None}, {"_id": 3}],
expected=[{"_id": 2, "a": None}, {"_id": 3}],
msg="$lte null matches null and missing fields (null <= null)",
),
QueryTestCase(
id="null_field_not_lte_numeric",
filter={"a": {"$lte": 5}},
doc=[{"_id": 1, "a": None}],
expected=[],
msg="null field does not match $lte with numeric query",
),
]


@pytest.mark.parametrize("test", pytest_params(EDGE_CASE_TESTS))
def test_lte_edge_cases(collection, test):
"""Parametrized test for $lte 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,147 @@
"""
Tests for $lte 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": {"$lte": 3}},
doc=[{"_id": 1, "a": [3, 7, 12]}, {"_id": 2, "a": [10, 20]}],
expected=[{"_id": 1, "a": [3, 7, 12]}],
msg="$lte matches if any array element satisfies condition (including equal)",
),
QueryTestCase(
id="array_no_element_match",
filter={"a": {"$lte": 2}},
doc=[{"_id": 1, "a": [10, 20]}],
expected=[],
msg="$lte on array with no element less than or equal to query",
),
QueryTestCase(
id="array_index_zero",
filter={"arr.0": {"$lte": 10}},
doc=[{"_id": 1, "arr": [10, 30]}, {"_id": 2, "arr": [25, 5]}],
expected=[{"_id": 1, "arr": [10, 30]}],
msg="$lte on array index 0 matches equal value",
),
QueryTestCase(
id="numeric_key_on_object",
filter={"a.0.b": {"$lte": 3}},
doc=[{"_id": 1, "a": {"0": {"b": 3}}}, {"_id": 2, "a": {"0": {"b": 10}}}],
expected=[{"_id": 1, "a": {"0": {"b": 3}}}],
msg="$lte with numeric key on object (not array)",
),
QueryTestCase(
id="id_objectid",
filter={"_id": {"$lte": ObjectId("507f1f77bcf86cd799439012")}},
doc=[
{"_id": ObjectId("507f1f77bcf86cd799439011"), "a": 1},
{"_id": ObjectId("507f1f77bcf86cd799439012"), "a": 2},
{"_id": ObjectId("507f1f77bcf86cd799439013"), "a": 3},
],
expected=[
{"_id": ObjectId("507f1f77bcf86cd799439011"), "a": 1},
{"_id": ObjectId("507f1f77bcf86cd799439012"), "a": 2},
],
msg="$lte on _id with ObjectId includes equal",
),
QueryTestCase(
id="array_of_embedded_docs_dot_notation",
filter={"a.b": {"$lte": 3}},
doc=[
{"_id": 1, "a": [{"b": 3}, {"b": 7}]},
{"_id": 2, "a": [{"b": 10}, {"b": 20}]},
],
expected=[{"_id": 1, "a": [{"b": 3}, {"b": 7}]}],
msg="$lte on array of embedded docs via dot notation matches equal",
),
]

ARRAY_VALUE_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="array_lte_array_element_comparison",
filter={"a": {"$lte": [1, 3]}},
doc=[{"_id": 1, "a": [1, 2]}, {"_id": 2, "a": [1, 3]}],
expected=[{"_id": 1, "a": [1, 2]}, {"_id": 2, "a": [1, 3]}],
msg="$lte array includes equal and lesser arrays",
),
QueryTestCase(
id="shorter_array_prefix_lte_longer",
filter={"a": {"$lte": [1, 2, 3]}},
doc=[{"_id": 1, "a": [1, 2]}],
expected=[{"_id": 1, "a": [1, 2]}],
msg="$lte shorter array prefix is less than or equal to longer",
),
QueryTestCase(
id="empty_array_lte_nonempty_array",
filter={"a": {"$lte": [1]}},
doc=[{"_id": 1, "a": []}, {"_id": 2, "a": [1]}],
expected=[{"_id": 1, "a": []}, {"_id": 2, "a": [1]}],
msg="$lte empty array and equal array both match",
),
QueryTestCase(
id="array_with_null_element_lte_scalar",
filter={"a": {"$lte": 5}},
doc=[{"_id": 1, "a": [None, 5]}],
expected=[{"_id": 1, "a": [None, 5]}],
msg="$lte matches array with element 5 <= 5",
),
QueryTestCase(
id="empty_array_not_lte_scalar",
filter={"a": {"$lte": 5}},
doc=[{"_id": 1, "a": []}],
expected=[],
msg="$lte empty array does not match scalar query",
),
]

EMBEDDED_DOC_TESTS: list[QueryTestCase] = [
QueryTestCase(
id="embedded_field_lte",
filter={"carrier.fee": {"$lte": 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": 5}},
],
expected=[
{"_id": 1, "item": "nuts", "carrier": {"name": "Shipit", "fee": 3}},
{"_id": 2, "item": "bolts", "carrier": {"name": "Shipit", "fee": 4}},
],
msg="$lte on embedded field returns docs with fee <= 4",
),
QueryTestCase(
id="embedded_field_missing_excluded",
filter={"carrier.fee": {"$lte": 10}},
doc=[
{"_id": 1, "carrier": {"fee": 3}},
{"_id": 2, "item": "no carrier"},
],
expected=[{"_id": 1, "carrier": {"fee": 3}}],
msg="$lte 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_lte_field_lookup(collection, test):
"""Parametrized test for $lte 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