Skip to content

Commit 866cfcd

Browse files
authored
Merge branch 'main' into forstaia/read_concern/query_and_write
2 parents 6fa1aef + 4608471 commit 866cfcd

16 files changed

Lines changed: 1823 additions & 262 deletions

documentdb_tests/compatibility/tests/core/query_and_write/commands/find/test_find_basic_queries.py

Lines changed: 0 additions & 140 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Tests for find command with multiple options combined."""
2+
3+
from __future__ import annotations
4+
5+
import pytest
6+
7+
from documentdb_tests.compatibility.tests.core.utils.command_test_case import (
8+
CommandContext,
9+
CommandTestCase,
10+
)
11+
from documentdb_tests.framework.assertions import assertResult
12+
from documentdb_tests.framework.executor import execute_command
13+
from documentdb_tests.framework.parametrize import pytest_params
14+
15+
# Property [Combined Operations]: find correctly applies filter, sort, projection,
16+
# skip, limit, and let/$expr together in a single command.
17+
FIND_COMBINED_TESTS: list[CommandTestCase] = [
18+
CommandTestCase(
19+
"filter_sort_projection_skip_limit",
20+
docs=[{"_id": i, "a": i % 3, "b": i * 10, "c": f"val_{i}"} for i in range(10)],
21+
command=lambda ctx: {
22+
"find": ctx.collection,
23+
"filter": {"a": 0},
24+
"sort": {"b": -1},
25+
"projection": {"b": 1},
26+
"skip": 1,
27+
"limit": 2,
28+
},
29+
expected=[{"_id": 6, "b": 60}, {"_id": 3, "b": 30}],
30+
msg="find should combine filter, sort, projection, skip, and limit correctly.",
31+
),
32+
CommandTestCase(
33+
"let_expr_sort_projection",
34+
docs=[
35+
{"_id": 1, "score": 80, "name": "Alice"},
36+
{"_id": 2, "score": 95, "name": "Bob"},
37+
{"_id": 3, "score": 70, "name": "Charlie"},
38+
],
39+
command=lambda ctx: {
40+
"find": ctx.collection,
41+
"filter": {"$expr": {"$gte": ["$score", "$$threshold"]}},
42+
"let": {"threshold": 80},
43+
"sort": {"score": -1},
44+
"projection": {"name": 1, "score": 1},
45+
},
46+
expected=[
47+
{"_id": 2, "name": "Bob", "score": 95},
48+
{"_id": 1, "name": "Alice", "score": 80},
49+
],
50+
msg="find should combine let, $expr, sort, and projection.",
51+
),
52+
]
53+
54+
55+
@pytest.mark.parametrize("test", pytest_params(FIND_COMBINED_TESTS))
56+
def test_find_combined(database_client, collection, test):
57+
"""Test find command combined operations."""
58+
collection = test.prepare(database_client, collection)
59+
ctx = CommandContext.from_collection(collection)
60+
result = execute_command(collection, test.build_command(ctx))
61+
assertResult(
62+
result,
63+
expected=test.build_expected(ctx),
64+
error_code=test.error_code,
65+
msg=test.msg,
66+
)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""Tests for find command core behavior and response structure."""
2+
3+
from __future__ import annotations
4+
5+
import pytest
6+
from bson import Int64
7+
8+
from documentdb_tests.compatibility.tests.core.utils.command_test_case import (
9+
CommandContext,
10+
CommandTestCase,
11+
)
12+
from documentdb_tests.framework.assertions import assertProperties, assertResult
13+
from documentdb_tests.framework.executor import execute_command
14+
from documentdb_tests.framework.parametrize import pytest_params
15+
from documentdb_tests.framework.property_checks import Eq, Exists, IsType
16+
17+
# Property [Primary Operation]: find returns matching documents from a collection,
18+
# or all documents when no filter is specified.
19+
FIND_CORE_TESTS: list[CommandTestCase] = [
20+
CommandTestCase(
21+
"all_documents_no_filter",
22+
docs=[{"_id": 1, "a": 10}, {"_id": 2, "a": 20}, {"_id": 3, "a": 30}],
23+
command=lambda ctx: {"find": ctx.collection, "sort": {"_id": 1}},
24+
expected=[{"_id": 1, "a": 10}, {"_id": 2, "a": 20}, {"_id": 3, "a": 30}],
25+
msg="find should return all documents when no filter specified.",
26+
),
27+
CommandTestCase(
28+
"filter_returns_matching",
29+
docs=[{"_id": 1, "a": 10}, {"_id": 2, "a": 20}, {"_id": 3, "a": 10}],
30+
command=lambda ctx: {"find": ctx.collection, "filter": {"a": 10}, "sort": {"_id": 1}},
31+
expected=[{"_id": 1, "a": 10}, {"_id": 3, "a": 10}],
32+
msg="find should return only documents matching the filter.",
33+
),
34+
CommandTestCase(
35+
"empty_collection",
36+
docs=[],
37+
command=lambda ctx: {"find": ctx.collection},
38+
expected=[],
39+
msg="find should return empty result for empty collection.",
40+
),
41+
CommandTestCase(
42+
"nonexistent_collection",
43+
docs=None,
44+
command=lambda ctx: {"find": ctx.collection},
45+
expected=[],
46+
msg="find should return empty result for non-existent collection.",
47+
),
48+
CommandTestCase(
49+
"empty_filter_returns_all",
50+
docs=[{"_id": 1, "x": "a"}, {"_id": 2, "x": "b"}],
51+
command=lambda ctx: {"find": ctx.collection, "filter": {}, "sort": {"_id": 1}},
52+
expected=[{"_id": 1, "x": "a"}, {"_id": 2, "x": "b"}],
53+
msg="find should return all documents with empty filter.",
54+
),
55+
CommandTestCase(
56+
"multiple_conditions_implicit_and",
57+
docs=[{"_id": 1, "a": 1, "b": 2}, {"_id": 2, "a": 1, "b": 3}],
58+
command=lambda ctx: {"find": ctx.collection, "filter": {"a": 1, "b": 2}},
59+
expected=[{"_id": 1, "a": 1, "b": 2}],
60+
msg="find should use implicit AND for multiple filter conditions.",
61+
),
62+
CommandTestCase(
63+
"nested_field_dot_notation",
64+
docs=[{"_id": 1, "obj": {"x": 10}}, {"_id": 2, "obj": {"x": 20}}],
65+
command=lambda ctx: {"find": ctx.collection, "filter": {"obj.x": 10}},
66+
expected=[{"_id": 1, "obj": {"x": 10}}],
67+
msg="find should match nested fields using dot notation.",
68+
),
69+
]
70+
71+
72+
@pytest.mark.parametrize("test", pytest_params(FIND_CORE_TESTS))
73+
def test_find_core_behavior(database_client, collection, test):
74+
"""Test find command core behavior."""
75+
collection = test.prepare(database_client, collection)
76+
ctx = CommandContext.from_collection(collection)
77+
result = execute_command(collection, test.build_command(ctx))
78+
assertResult(
79+
result,
80+
expected=test.build_expected(ctx),
81+
error_code=test.error_code,
82+
msg=test.msg,
83+
)
84+
85+
86+
def test_find_response_structure(collection):
87+
"""Test find response contains cursor with firstBatch, id, and ns fields."""
88+
collection.insert_one({"_id": 1, "a": 1})
89+
result = execute_command(collection, {"find": collection.name})
90+
assertResult(
91+
result,
92+
expected={
93+
"cursor.firstBatch": Exists(),
94+
"cursor.id": IsType("long"),
95+
"cursor.ns": IsType("string"),
96+
},
97+
raw_res=True,
98+
msg="find should return cursor with firstBatch, id, and ns.",
99+
)
100+
101+
102+
def test_find_cursor_id_zero_when_exhausted(collection):
103+
"""Test cursor id is 0 when all results fit in first batch."""
104+
collection.insert_many([{"_id": i} for i in range(3)])
105+
result = execute_command(collection, {"find": collection.name})
106+
assertProperties(
107+
result,
108+
{"cursor.id": Eq(Int64(0))},
109+
raw_res=True,
110+
msg="find should return cursor id 0 when all results returned.",
111+
)

0 commit comments

Comments
 (0)