Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

from typing import Any

from documentdb_tests.compatibility.tests.core.cursors.commands.utils.cursor_test_case import (
open_cursor,
open_find_cursors,
Expand All @@ -17,11 +19,15 @@
def test_getMore_batch_from_find(collection):
"""Test getMore returns subsequent batches from a find cursor."""
collection.insert_many([{"_id": i, "v": i} for i in range(10)])
(cursor_id,) = open_find_cursors(collection, 1, batch_size=3)
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 4},
)
cursors = open_find_cursors(collection, 1, batch_size=3)
if isinstance(cursors, Exception):
result = cursors
else:
(cursor_id,) = cursors
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 4},
)
assertResult(
result,
expected={
Expand All @@ -39,11 +45,15 @@ def test_getMore_batch_size_zero_tailable_keeps_open(collection):
"""Test getMore with batchSize=0 on tailable cursor keeps it open."""
capped = CappedCollection().resolve(collection.database, collection)
capped.insert_many([{"_id": i} for i in range(5)])
cursor_id = open_cursor(capped, {"tailable": True, "batchSize": 2})
result = execute_command(
collection,
{"getMore": cursor_id, "collection": capped.name, "batchSize": 0},
)
opened = open_cursor(capped, {"tailable": True, "batchSize": 2})
if isinstance(opened, Exception):
result = opened
else:
cursor_id = opened
result = execute_command(
collection,
{"getMore": cursor_id, "collection": capped.name, "batchSize": 0},
)
assertResult(
result,
expected={"ok": Eq(1.0), "cursor": {"nextBatch": Len(3), "id": Gte(1)}},
Expand All @@ -58,16 +68,22 @@ def test_getMore_batch_size_independent(collection):
"""Test each getMore call sets its own batchSize independently."""
docs = [{"_id": i, "v": i} for i in range(10)]
collection.insert_many(docs)
(cursor_id,) = open_find_cursors(collection, 1, batch_size=2)
result1 = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 3},
)
cursor_id = result1["cursor"]["id"]
result2 = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 5},
)
cursors = open_find_cursors(collection, 1, batch_size=2)
if isinstance(cursors, Exception):
result2 = cursors
else:
(cursor_id,) = cursors
result1 = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 3},
)
if isinstance(result1, Exception):
result2 = result1
else:
result2 = execute_command(
collection,
{"getMore": result1["cursor"]["id"], "collection": collection.name, "batchSize": 5},
)
assertResult(
result2,
expected={
Expand All @@ -86,11 +102,14 @@ def test_getMore_find_limit_caps_total(collection):
docs = [{"_id": i, "v": i} for i in range(10)]
collection.insert_many(docs)
find_result = execute_command(collection, {"find": collection.name, "batchSize": 2, "limit": 5})
cursor_id = find_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 10},
)
if isinstance(find_result, Exception):
result = find_result
else:
cursor_id = find_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 10},
)
assertResult(
result,
expected={"ok": Eq(1.0), "cursor": {"nextBatch": Len(3), "id": Eq(INT64_ZERO)}},
Expand All @@ -105,21 +124,34 @@ def test_getMore_independent_cursors(collection):
"""Test two cursors on the same collection maintain independent positions."""
docs = [{"_id": i, "v": i} for i in range(10)]
collection.insert_many(docs)
(cursor_a,) = open_find_cursors(collection, 1, batch_size=3)
(cursor_b,) = open_find_cursors(collection, 1, batch_size=5)
result_a = execute_command(
collection,
{"getMore": cursor_a, "collection": collection.name, "batchSize": 2},
)
result_b = execute_command(
collection,
{"getMore": cursor_b, "collection": collection.name, "batchSize": 2},
)
# Cursor A started at position 3, cursor B started at position 5.
combined = {
"a": result_a["cursor"]["nextBatch"],
"b": result_b["cursor"]["nextBatch"],
}
cursors_a = open_find_cursors(collection, 1, batch_size=3)
cursors_b = open_find_cursors(collection, 1, batch_size=5)
combined: Any
if isinstance(cursors_a, Exception):
combined = cursors_a
elif isinstance(cursors_b, Exception):
combined = cursors_b
else:
(cursor_a,) = cursors_a
(cursor_b,) = cursors_b
result_a = execute_command(
collection,
{"getMore": cursor_a, "collection": collection.name, "batchSize": 2},
)
result_b = execute_command(
collection,
{"getMore": cursor_b, "collection": collection.name, "batchSize": 2},
)
if isinstance(result_a, Exception):
combined = result_a
elif isinstance(result_b, Exception):
combined = result_b
else:
# Cursor A started at position 3, cursor B started at position 5.
combined = {
"a": result_a["cursor"]["nextBatch"],
"b": result_b["cursor"]["nextBatch"],
}
assertResult(
combined,
expected={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,11 @@ def test_getMore_batch_size_coercion(collection, test_case: CursorCommandTestCas
cursors = open_find_cursors(
collection, test_case.cursor_count, batch_size=test_case.find_batch_size
)
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
if isinstance(cursors, Exception):
result = cursors
else:
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
assertResult(
result,
expected=test_case.expected,
Expand All @@ -473,11 +476,15 @@ def test_getMore_16mib_batch_limit(collection):
big_val = "x" * 1_100_000
docs = [{"_id": i, "data": big_val} for i in range(20)]
collection.insert_many(docs)
(cursor_id,) = open_find_cursors(collection, 1, batch_size=2)
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name},
)
cursors = open_find_cursors(collection, 1, batch_size=2)
if isinstance(cursors, Exception):
result = cursors
else:
(cursor_id,) = cursors
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name},
)
assertResult(
result,
expected={"ok": Eq(1.0), "cursor": {"nextBatch": Len(15)}},
Expand All @@ -493,11 +500,15 @@ def test_getMore_16mib_batch_size_wins(collection):
big_val = "x" * 1_100_000
docs = [{"_id": i, "data": big_val} for i in range(20)]
collection.insert_many(docs)
(cursor_id,) = open_find_cursors(collection, 1, batch_size=2)
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 3},
)
cursors = open_find_cursors(collection, 1, batch_size=2)
if isinstance(cursors, Exception):
result = cursors
else:
(cursor_id,) = cursors
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 3},
)
assertResult(
result,
expected={"ok": Eq(1.0), "cursor": {"nextBatch": Len(3)}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ def test_getMore_comment(collection, test_case: CursorCommandTestCase):
cursors = open_find_cursors(
collection, test_case.cursor_count, batch_size=test_case.find_batch_size
)
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
if isinstance(cursors, Exception):
result = cursors
else:
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
assertResult(
result,
expected=test_case.expected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ def test_getMore_aggregate_cursor(collection):
collection,
{"aggregate": collection.name, "pipeline": [], "cursor": {"batchSize": 2}},
)
cursor_id = agg_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 3},
)
if isinstance(agg_result, Exception):
result = agg_result
else:
cursor_id = agg_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name, "batchSize": 3},
)
assertResult(
result,
expected={"ok": Eq(1.0), "cursor": {"nextBatch": Len(3)}},
Expand All @@ -45,11 +48,14 @@ def test_getMore_view_cursor(collection):
view_name = f"{collection.name}_view"
execute_command(collection, {"create": view_name, "viewOn": collection.name, "pipeline": []})
find_result = execute_command(collection, {"find": view_name, "batchSize": 2})
cursor_id = find_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": view_name},
)
if isinstance(find_result, Exception):
result = find_result
else:
cursor_id = find_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": view_name},
)
assertResult(
result,
expected={"ok": Eq(1.0), "cursor": {"nextBatch": Len(3)}},
Expand All @@ -75,11 +81,14 @@ def test_getMore_timeseries_cursor(collection):
},
)
find_result = execute_command(collection, {"find": ts_name, "batchSize": 2})
cursor_id = find_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": ts_name, "batchSize": 3},
)
if isinstance(find_result, Exception):
result = find_result
else:
cursor_id = find_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": ts_name, "batchSize": 3},
)
assertResult(
result,
expected={"ok": Eq(1.0), "cursor": {"nextBatch": Len(3)}},
Expand All @@ -98,11 +107,14 @@ def test_getMore_list_collections_cursor(collection):
collection,
{"listCollections": 1, "cursor": {"batchSize": 1}},
)
cursor_id = list_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": "$cmd.listCollections"},
)
if isinstance(list_result, Exception):
result = list_result
else:
cursor_id = list_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": "$cmd.listCollections"},
)
assertResult(
result,
expected={"ok": Eq(1.0)},
Expand All @@ -128,11 +140,14 @@ def test_getMore_list_indexes_cursor(collection):
collection,
{"listIndexes": collection.name, "cursor": {"batchSize": 1}},
)
cursor_id = list_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name},
)
if isinstance(list_result, Exception):
result = list_result
else:
cursor_id = list_result["cursor"]["id"]
result = execute_command(
collection,
{"getMore": cursor_id, "collection": collection.name},
)
assertResult(
result,
expected={"ok": Eq(1.0)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ def test_getMore_field_type_errors(collection, test_case: CursorCommandTestCase)
cursors = open_find_cursors(
collection, test_case.cursor_count, batch_size=test_case.find_batch_size
)
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
if isinstance(cursors, Exception):
result = cursors
else:
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
assertResult(
result,
expected=test_case.expected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,11 @@ def test_getMore_field_value_errors(collection, test_case: CursorCommandTestCase
cursors = open_find_cursors(
collection, test_case.cursor_count, batch_size=test_case.find_batch_size
)
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
if isinstance(cursors, Exception):
result = cursors
else:
ctx = CursorCommandContext.from_collection(collection, cursors=cursors)
result = execute_command(collection, test_case.build_command(ctx))
assertResult(
result,
expected=test_case.expected,
Expand Down
Loading
Loading