Skip to content

Commit c194109

Browse files
ajdavisShaneHarvey
authored andcommitted
Test aggregate in transactions
1 parent 34ab244 commit c194109

File tree

2 files changed

+209
-6
lines changed

2 files changed

+209
-6
lines changed

test/test_transactions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ def test_transaction_options_validation(self):
8888

8989
# TODO: factor the following function with test_crud.py.
9090
def check_result(self, expected_result, result):
91-
if isinstance(result, Cursor) or isinstance(result, CommandCursor):
92-
self.assertEqual(list(result), expected_result)
93-
94-
elif isinstance(result, _WriteResult):
91+
if isinstance(result, _WriteResult):
9592
for res in expected_result:
9693
prop = camel_to_snake(res)
9794
# SPEC-869: Only BulkWriteResult has upserted_count.
@@ -178,7 +175,7 @@ def run_operation(self, sessions, collection, operation):
178175
if arg_name == "fieldName":
179176
arguments["key"] = arguments.pop(arg_name)
180177
# Aggregate uses "batchSize", while find uses batch_size.
181-
elif arg_name == "batchSize" and operation == "aggregate":
178+
elif arg_name == "batchSize" and name == "aggregate":
182179
continue
183180
# Requires boolean returnDocument.
184181
elif arg_name == "returnDocument":
@@ -197,11 +194,14 @@ def run_operation(self, sessions, collection, operation):
197194

198195
result = cmd(**arguments)
199196

200-
if operation == "aggregate":
197+
if name == "aggregate":
201198
if arguments["pipeline"] and "$out" in arguments["pipeline"][-1]:
202199
out = collection.database[arguments["pipeline"][-1]["$out"]]
203200
return out.find()
204201

202+
if isinstance(result, Cursor) or isinstance(result, CommandCursor):
203+
return list(result)
204+
205205
return result
206206

207207
# TODO: factor with test_command_monitoring.py

test/transactions/reads.json

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,209 @@
300300
}
301301
}
302302
},
303+
{
304+
"description": "aggregate",
305+
"operations": [
306+
{
307+
"name": "startTransaction",
308+
"arguments": {
309+
"session": "session0"
310+
}
311+
},
312+
{
313+
"name": "aggregate",
314+
"arguments": {
315+
"pipeline": [
316+
{
317+
"$project": {
318+
"_id": 1
319+
}
320+
}
321+
],
322+
"batchSize": 3,
323+
"session": "session0"
324+
},
325+
"result": [
326+
{
327+
"_id": 1
328+
},
329+
{
330+
"_id": 2
331+
},
332+
{
333+
"_id": 3
334+
},
335+
{
336+
"_id": 4
337+
}
338+
]
339+
},
340+
{
341+
"name": "aggregate",
342+
"arguments": {
343+
"pipeline": [
344+
{
345+
"$project": {
346+
"_id": 1
347+
}
348+
}
349+
],
350+
"batchSize": 3,
351+
"session": "session0"
352+
},
353+
"result": [
354+
{
355+
"_id": 1
356+
},
357+
{
358+
"_id": 2
359+
},
360+
{
361+
"_id": 3
362+
},
363+
{
364+
"_id": 4
365+
}
366+
]
367+
},
368+
{
369+
"name": "commitTransaction",
370+
"arguments": {
371+
"session": "session0"
372+
}
373+
}
374+
],
375+
"expectations": [
376+
{
377+
"command_started_event": {
378+
"command": {
379+
"aggregate": "test",
380+
"pipeline": [
381+
{
382+
"$project": {
383+
"_id": 1
384+
}
385+
}
386+
],
387+
"cursor": {
388+
"batchSize": 3
389+
},
390+
"readConcern": null,
391+
"lsid": "session0",
392+
"txnNumber": {
393+
"$numberLong": "1"
394+
},
395+
"stmtId": 0,
396+
"startTransaction": true,
397+
"autocommit": false
398+
},
399+
"command_name": "aggregate",
400+
"database_name": "transaction-tests"
401+
}
402+
},
403+
{
404+
"command_started_event": {
405+
"command": {
406+
"getMore": {
407+
"$numberLong": "42"
408+
},
409+
"collection": "test",
410+
"batchSize": 3,
411+
"lsid": "session0",
412+
"txnNumber": {
413+
"$numberLong": "1"
414+
},
415+
"stmtId": 1,
416+
"startTransaction": null,
417+
"autocommit": false
418+
},
419+
"command_name": "getMore",
420+
"database_name": "transaction-tests"
421+
}
422+
},
423+
{
424+
"command_started_event": {
425+
"command": {
426+
"aggregate": "test",
427+
"pipeline": [
428+
{
429+
"$project": {
430+
"_id": 1
431+
}
432+
}
433+
],
434+
"cursor": {
435+
"batchSize": 3
436+
},
437+
"lsid": "session0",
438+
"txnNumber": {
439+
"$numberLong": "1"
440+
},
441+
"stmtId": 2,
442+
"startTransaction": null,
443+
"autocommit": false
444+
},
445+
"command_name": "aggregate",
446+
"database_name": "transaction-tests"
447+
}
448+
},
449+
{
450+
"command_started_event": {
451+
"command": {
452+
"getMore": {
453+
"$numberLong": "42"
454+
},
455+
"collection": "test",
456+
"batchSize": 3,
457+
"lsid": "session0",
458+
"txnNumber": {
459+
"$numberLong": "1"
460+
},
461+
"stmtId": 3,
462+
"startTransaction": null,
463+
"autocommit": false
464+
},
465+
"command_name": "getMore",
466+
"database_name": "transaction-tests"
467+
}
468+
},
469+
{
470+
"command_started_event": {
471+
"command": {
472+
"commitTransaction": 1,
473+
"lsid": "session0",
474+
"txnNumber": {
475+
"$numberLong": "1"
476+
},
477+
"stmtId": 4,
478+
"startTransaction": null,
479+
"autocommit": false,
480+
"writeConcern": null
481+
},
482+
"command_name": "commitTransaction",
483+
"database_name": "admin"
484+
}
485+
}
486+
],
487+
"outcome": {
488+
"collection": {
489+
"data": [
490+
{
491+
"_id": 1
492+
},
493+
{
494+
"_id": 2
495+
},
496+
{
497+
"_id": 3
498+
},
499+
{
500+
"_id": 4
501+
}
502+
]
503+
}
504+
}
505+
},
303506
{
304507
"description": "distinct",
305508
"operations": [

0 commit comments

Comments
 (0)