|
20 | 20 | import sys |
21 | 21 |
|
22 | 22 | from bson import json_util, py3compat |
23 | | -from pymongo.errors import PyMongoError |
| 23 | +from pymongo.errors import OperationFailure, PyMongoError |
24 | 24 | from pymongo.read_concern import ReadConcern |
25 | 25 | from pymongo.read_preferences import (make_read_preference, |
26 | 26 | read_pref_mode_from_name) |
@@ -236,13 +236,20 @@ def check_events(self, test, listener, session_ids): |
236 | 236 | self.assertEqual(actual, expected) |
237 | 237 |
|
238 | 238 |
|
239 | | -def expect_error(expected_result): |
| 239 | +def expect_error_message(expected_result): |
240 | 240 | if isinstance(expected_result, dict): |
241 | 241 | return expected_result['errorContains'] |
242 | 242 |
|
243 | 243 | return False |
244 | 244 |
|
245 | 245 |
|
| 246 | +def expect_error_code(expected_result): |
| 247 | + if isinstance(expected_result, dict): |
| 248 | + return expected_result['errorCodeName'] |
| 249 | + |
| 250 | + return False |
| 251 | + |
| 252 | + |
246 | 253 | def end_sessions(sessions): |
247 | 254 | for s in sessions.values(): |
248 | 255 | # Aborts the transaction if it's open. |
@@ -285,13 +292,18 @@ def run_scenario(self): |
285 | 292 |
|
286 | 293 | for op in test['operations']: |
287 | 294 | expected_result = op.get('result') |
288 | | - if expect_error(expected_result): |
| 295 | + if expect_error_message(expected_result): |
289 | 296 | with self.assertRaises(PyMongoError) as context: |
290 | 297 | self.run_operation(sessions, collection, op.copy()) |
291 | 298 |
|
292 | 299 | self.assertIn(expected_result['errorContains'].lower(), |
293 | 300 | 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()) |
294 | 304 |
|
| 305 | + self.assertEqual(expected_result['errorCodeName'], |
| 306 | + context.exception.details.get('codeName')) |
295 | 307 | else: |
296 | 308 | result = self.run_operation(sessions, collection, op.copy()) |
297 | 309 | if 'result' in op: |
|
0 commit comments