Skip to content

Commit 3b02e59

Browse files
ShaneHarveyajdavis
authored andcommitted
Add errorCodeName assertion to transaction tests
1 parent 0967c7e commit 3b02e59

File tree

6 files changed

+180
-14
lines changed

6 files changed

+180
-14
lines changed

test/test_transactions.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import sys
2121

2222
from bson import json_util, py3compat
23-
from pymongo.errors import PyMongoError
23+
from pymongo.errors import OperationFailure, PyMongoError
2424
from pymongo.read_concern import ReadConcern
2525
from pymongo.read_preferences import (make_read_preference,
2626
read_pref_mode_from_name)
@@ -236,13 +236,20 @@ def check_events(self, test, listener, session_ids):
236236
self.assertEqual(actual, expected)
237237

238238

239-
def expect_error(expected_result):
239+
def expect_error_message(expected_result):
240240
if isinstance(expected_result, dict):
241241
return expected_result['errorContains']
242242

243243
return False
244244

245245

246+
def expect_error_code(expected_result):
247+
if isinstance(expected_result, dict):
248+
return expected_result['errorCodeName']
249+
250+
return False
251+
252+
246253
def end_sessions(sessions):
247254
for s in sessions.values():
248255
# Aborts the transaction if it's open.
@@ -285,13 +292,18 @@ def run_scenario(self):
285292

286293
for op in test['operations']:
287294
expected_result = op.get('result')
288-
if expect_error(expected_result):
295+
if expect_error_message(expected_result):
289296
with self.assertRaises(PyMongoError) as context:
290297
self.run_operation(sessions, collection, op.copy())
291298

292299
self.assertIn(expected_result['errorContains'].lower(),
293300
str(context.exception).lower())
301+
elif expect_error_code(expected_result):
302+
with self.assertRaises(OperationFailure) as context:
303+
self.run_operation(sessions, collection, op.copy())
294304

305+
self.assertEqual(expected_result['errorCodeName'],
306+
context.exception.details.get('codeName'))
295307
else:
296308
result = self.run_operation(sessions, collection, op.copy())
297309
if 'result' in op:

test/transactions/abort.json

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"$numberLong": "1"
7373
},
7474
"stmtId": 0,
75+
"startTransaction": true,
7576
"autocommit": false,
7677
"writeConcern": null
7778
},
@@ -115,6 +116,7 @@
115116
"$numberLong": "2"
116117
},
117118
"stmtId": 0,
119+
"startTransaction": true,
118120
"autocommit": false,
119121
"writeConcern": null
120122
},
@@ -187,6 +189,7 @@
187189
"$numberLong": "1"
188190
},
189191
"stmtId": 0,
192+
"startTransaction": true,
190193
"autocommit": false,
191194
"writeConcern": null
192195
},
@@ -439,7 +442,19 @@
439442
"session": "session0"
440443
},
441444
"result": {
442-
"errorContains": "duplicate key error"
445+
"errorCodeName": "DuplicateKey"
446+
}
447+
},
448+
{
449+
"name": "insertOne",
450+
"arguments": {
451+
"document": {
452+
"_id": 1
453+
},
454+
"session": "session0"
455+
},
456+
"result": {
457+
"errorCodeName": "NoSuchTransaction"
443458
}
444459
},
445460
{
@@ -503,7 +518,14 @@
503518
{
504519
"command_started_event": {
505520
"command": {
506-
"abortTransaction": 1,
521+
"insert": "test",
522+
"documents": [
523+
{
524+
"_id": 1
525+
}
526+
],
527+
"ordered": true,
528+
"readConcern": null,
507529
"lsid": "session0",
508530
"txnNumber": {
509531
"$numberLong": "1"
@@ -513,6 +535,23 @@
513535
"autocommit": false,
514536
"writeConcern": null
515537
},
538+
"command_name": "insert",
539+
"database_name": "transaction-tests"
540+
}
541+
},
542+
{
543+
"command_started_event": {
544+
"command": {
545+
"abortTransaction": 1,
546+
"lsid": "session0",
547+
"txnNumber": {
548+
"$numberLong": "1"
549+
},
550+
"stmtId": 3,
551+
"startTransaction": null,
552+
"autocommit": false,
553+
"writeConcern": null
554+
},
516555
"command_name": "abortTransaction",
517556
"database_name": "admin"
518557
}

test/transactions/bulk.json

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@
160160
}
161161
}
162162
}
163+
},
164+
{
165+
"name": "updateMany",
166+
"arguments": {
167+
"filter": {
168+
"_id": {
169+
"$gte": 2
170+
}
171+
},
172+
"update": {
173+
"$set": {
174+
"z": 1
175+
}
176+
}
177+
}
163178
}
164179
],
165180
"session": "session0"
@@ -174,8 +189,8 @@
174189
"6": 6,
175190
"7": 7
176191
},
177-
"matchedCount": 3,
178-
"modifiedCount": 3,
192+
"matchedCount": 5,
193+
"modifiedCount": 5,
179194
"upsertedCount": 1,
180195
"upsertedIds": {
181196
"2": 2
@@ -427,7 +442,24 @@
427442
{
428443
"command_started_event": {
429444
"command": {
430-
"commitTransaction": 1,
445+
"update": "test",
446+
"updates": [
447+
{
448+
"q": {
449+
"_id": {
450+
"$gte": 2
451+
}
452+
},
453+
"u": {
454+
"$set": {
455+
"z": 1
456+
}
457+
},
458+
"multi": true,
459+
"upsert": false
460+
}
461+
],
462+
"ordered": true,
431463
"lsid": "session0",
432464
"txnNumber": {
433465
"$numberLong": "1"
@@ -437,6 +469,23 @@
437469
"autocommit": false,
438470
"writeConcern": null
439471
},
472+
"command_name": "update",
473+
"database_name": "transaction-tests"
474+
}
475+
},
476+
{
477+
"command_started_event": {
478+
"command": {
479+
"commitTransaction": 1,
480+
"lsid": "session0",
481+
"txnNumber": {
482+
"$numberLong": "1"
483+
},
484+
"stmtId": 16,
485+
"startTransaction": null,
486+
"autocommit": false,
487+
"writeConcern": null
488+
},
440489
"command_name": "commitTransaction",
441490
"database_name": "admin"
442491
}
@@ -451,10 +500,12 @@
451500
},
452501
{
453502
"_id": 2,
454-
"y": 2
503+
"y": 2,
504+
"z": 1
455505
},
456506
{
457-
"_id": 5
507+
"_id": 5,
508+
"z": 1
458509
}
459510
]
460511
}

test/transactions/causal-consistency.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
"session": "session0"
209209
},
210210
"result": {
211-
"errorContains": "WriteConflict"
211+
"errorCodeName": "WriteConflict"
212212
}
213213
},
214214
{

test/transactions/reads.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
"command_started_event": {
210210
"command": {
211211
"find": "test",
212+
"batchSize": 3,
212213
"readConcern": {
213214
"level": "snapshot"
214215
},
@@ -230,6 +231,8 @@
230231
"getMore": {
231232
"$numberLong": "42"
232233
},
234+
"collection": "test",
235+
"batchSize": 3,
233236
"lsid": "session0",
234237
"txnNumber": {
235238
"$numberLong": "1"
@@ -246,6 +249,7 @@
246249
"command_started_event": {
247250
"command": {
248251
"find": "test",
252+
"batchSize": 3,
249253
"lsid": "session0",
250254
"txnNumber": {
251255
"$numberLong": "1"
@@ -264,6 +268,8 @@
264268
"getMore": {
265269
"$numberLong": "42"
266270
},
271+
"collection": "test",
272+
"batchSize": 3,
267273
"lsid": "session0",
268274
"txnNumber": {
269275
"$numberLong": "1"
@@ -347,6 +353,7 @@
347353
"command_started_event": {
348354
"command": {
349355
"distinct": "test",
356+
"key": "_id",
350357
"lsid": "session0",
351358
"readConcern": {
352359
"level": "snapshot"

test/transactions/update.json

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@
5858
"upsertedCount": 0
5959
}
6060
},
61+
{
62+
"name": "updateMany",
63+
"arguments": {
64+
"filter": {
65+
"_id": {
66+
"$gte": 3
67+
}
68+
},
69+
"update": {
70+
"$set": {
71+
"z": 1
72+
}
73+
},
74+
"session": "session0"
75+
},
76+
"result": {
77+
"matchedCount": 2,
78+
"modifiedCount": 2,
79+
"upsertedCount": 0
80+
}
81+
},
6182
{
6283
"name": "commitTransaction",
6384
"arguments": {
@@ -134,7 +155,24 @@
134155
{
135156
"command_started_event": {
136157
"command": {
137-
"commitTransaction": 1,
158+
"update": "test",
159+
"updates": [
160+
{
161+
"q": {
162+
"_id": {
163+
"$gte": 3
164+
}
165+
},
166+
"u": {
167+
"$set": {
168+
"z": 1
169+
}
170+
},
171+
"multi": true,
172+
"upsert": false
173+
}
174+
],
175+
"ordered": true,
138176
"lsid": "session0",
139177
"txnNumber": {
140178
"$numberLong": "1"
@@ -144,6 +182,23 @@
144182
"autocommit": false,
145183
"writeConcern": null
146184
},
185+
"command_name": "update",
186+
"database_name": "transaction-tests"
187+
}
188+
},
189+
{
190+
"command_started_event": {
191+
"command": {
192+
"commitTransaction": 1,
193+
"lsid": "session0",
194+
"txnNumber": {
195+
"$numberLong": "1"
196+
},
197+
"stmtId": 3,
198+
"startTransaction": null,
199+
"autocommit": false,
200+
"writeConcern": null
201+
},
147202
"command_name": "commitTransaction",
148203
"database_name": "admin"
149204
}
@@ -159,11 +214,13 @@
159214
"_id": 2
160215
},
161216
{
162-
"_id": 3
217+
"_id": 3,
218+
"z": 1
163219
},
164220
{
165221
"_id": 4,
166-
"y": 1
222+
"y": 1,
223+
"z": 1
167224
}
168225
]
169226
}

0 commit comments

Comments
 (0)