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
Expand Up @@ -96,15 +96,21 @@ def test_listIndexes_getMore_retrieves_remaining(collection_with_3_indexes):
collection_with_3_indexes,
{"listIndexes": collection_with_3_indexes.name, "cursor": {"batchSize": 1}},
)
cursor_id = result["cursor"]["id"]

get_more_result = execute_command(
collection_with_3_indexes,
{"getMore": cursor_id, "collection": collection_with_3_indexes.name},
)
if isinstance(result, Exception):
outcome = result
else:
get_more_result = execute_command(
collection_with_3_indexes,
{"getMore": result["cursor"]["id"], "collection": collection_with_3_indexes.name},
)
outcome = (
get_more_result
if isinstance(get_more_result, Exception)
else {"cursor": {"firstBatch": get_more_result["cursor"]["nextBatch"]}, "ok": 1.0}
)

assertSuccess(
{"cursor": {"firstBatch": get_more_result["cursor"]["nextBatch"]}, "ok": 1.0},
outcome,
[{"v": 2, "key": {"a": 1}, "name": "a_1"}, {"v": 2, "key": {"b": 1}, "name": "b_1"}],
)

Expand Down Expand Up @@ -141,19 +147,33 @@ def test_listIndexes_batchSize_1_iterate_all(collection_with_3_indexes):
collection_with_3_indexes,
{"listIndexes": collection_with_3_indexes.name, "cursor": {"batchSize": 1}},
)
all_indexes = list(result["cursor"]["firstBatch"])
cursor_id = result["cursor"]["id"]

while cursor_id != 0:
get_more_result = execute_command(
collection_with_3_indexes,
{"getMore": cursor_id, "collection": collection_with_3_indexes.name, "batchSize": 1},
if isinstance(result, Exception):
outcome = result
else:
all_indexes = list(result["cursor"]["firstBatch"])
cursor_id = result["cursor"]["id"]

failure = None
while cursor_id != 0:
get_more_result = execute_command(
collection_with_3_indexes,
{
"getMore": cursor_id,
"collection": collection_with_3_indexes.name,
"batchSize": 1,
},
)
if isinstance(get_more_result, Exception):
failure = get_more_result
break
all_indexes.extend(get_more_result["cursor"]["nextBatch"])
cursor_id = get_more_result["cursor"]["id"]
outcome = (
failure if failure is not None else {"cursor": {"firstBatch": all_indexes}, "ok": 1.0}
)
all_indexes.extend(get_more_result["cursor"]["nextBatch"])
cursor_id = get_more_result["cursor"]["id"]

assertSuccess(
{"cursor": {"firstBatch": all_indexes}, "ok": 1.0},
outcome,
[
{"v": 2, "key": {"_id": 1}, "name": "_id_"},
{"v": 2, "key": {"a": 1}, "name": "a_1"},
Expand All @@ -180,22 +200,36 @@ def test_listIndexes_batchSize_10_with_64_indexes(collection_with_64_indexes):
collection_with_64_indexes,
{"listIndexes": collection_with_64_indexes.name, "cursor": {"batchSize": 10}},
)
all_indexes = list(result["cursor"]["firstBatch"])
cursor_id = result["cursor"]["id"]

while cursor_id != 0:
get_more_result = execute_command(
collection_with_64_indexes,
{"getMore": cursor_id, "collection": collection_with_64_indexes.name, "batchSize": 10},
if isinstance(result, Exception):
outcome = result
else:
all_indexes = list(result["cursor"]["firstBatch"])
cursor_id = result["cursor"]["id"]

failure = None
while cursor_id != 0:
get_more_result = execute_command(
collection_with_64_indexes,
{
"getMore": cursor_id,
"collection": collection_with_64_indexes.name,
"batchSize": 10,
},
)
if isinstance(get_more_result, Exception):
failure = get_more_result
break
all_indexes.extend(get_more_result["cursor"]["nextBatch"])
cursor_id = get_more_result["cursor"]["id"]
outcome = (
failure if failure is not None else {"cursor": {"firstBatch": all_indexes}, "ok": 1.0}
)
all_indexes.extend(get_more_result["cursor"]["nextBatch"])
cursor_id = get_more_result["cursor"]["id"]

expected = [{"v": 2, "key": {"_id": 1}, "name": "_id_"}]
for i in range(63):
expected.append({"v": 2, "key": {f"f{i}": 1}, "name": f"f{i}_1"})
assertSuccess(
{"cursor": {"firstBatch": all_indexes}, "ok": 1.0},
outcome,
expected,
msg="Total indexes across all batches should equal 64",
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def test_changeStream_required_image_event_read_error(collection, test_case):
opened = execute_command(
collection, change_stream_command(collection, pipeline=test_case.pipeline)
)
collection.update_one({"_id": 1}, {"$set": {"a": 2}})
result = execute_command(collection, get_more_command(collection, opened["cursor"]["id"]))
if isinstance(opened, Exception):
result = opened
else:
collection.update_one({"_id": 1}, {"$set": {"a": 2}})
result = execute_command(collection, get_more_command(collection, opened["cursor"]["id"]))
assertResult(result, error_code=test_case.error_code, msg=test_case.msg, raw_res=True)
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,14 @@ def __post_init__(self):
)


def _emit_event(collection, test_case: ChangeStreamEventTestCase) -> dict[str, Any]:
def _emit_event(collection, test_case: ChangeStreamEventTestCase) -> dict[str, Any] | Exception:
"""Open a stream, perform the test case mutation, and return the matching event.

The mutation happens after the stream is open, so its event is delivered by
the first getMore.
the first getMore. If opening the stream or reading the batch fails, the
engine's exception is returned so the test's single assertion surfaces it.
If the batch has no event of the expected type, the getMore reply is
returned instead, so the assertion reports what the stream delivered.
"""
mutation = test_case.mutation
assert mutation is not None # guaranteed by __post_init__
Expand All @@ -443,14 +446,19 @@ def _emit_event(collection, test_case: ChangeStreamEventTestCase) -> dict[str, A
opened = execute_command(
collection, change_stream_command(collection, pipeline=test_case.pipeline)
)
if isinstance(opened, Exception):
return opened
mutation.apply(collection)
if test_case.post_mutation is not None:
test_case.post_mutation(collection)
batch = execute_command(collection, get_more_command(collection, opened["cursor"]["id"]))[
"cursor"
]["nextBatch"]
batch_result = execute_command(collection, get_more_command(collection, opened["cursor"]["id"]))
if isinstance(batch_result, Exception):
return batch_result
batch = batch_result["cursor"]["nextBatch"]
matching = [e for e in batch if e["operationType"] == mutation.operation_type]
assert matching, f"no {mutation.operation_type} event in batch: {batch!r}"
if not matching:
reply: dict[str, Any] = batch_result
return reply
event: dict[str, Any] = matching[0]
return event

Expand Down
Loading
Loading