|
| 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