|
42 | 42 | CommandSucceededEvent)
|
43 | 43 | from pymongo.read_concern import ReadConcern
|
44 | 44 | from pymongo.read_preferences import ReadPreference
|
45 |
| -from pymongo.results import BulkWriteResult |
| 45 | +from pymongo.results import BulkWriteResult, InsertManyResult, InsertOneResult |
46 | 46 | from pymongo.write_concern import WriteConcern
|
47 | 47 |
|
48 | 48 | from test import client_context, unittest, IntegrationTest
|
@@ -189,11 +189,13 @@ def _create_entity(self, entity_spec):
|
189 | 189 | # Add logic to respect the following fields
|
190 | 190 | # - uriOptions
|
191 | 191 | # - useMultipleMongoses
|
| 192 | + uri_options = spec.get('uriOptions', {}) |
192 | 193 | observe_events = spec.get('observeEvents')
|
193 | 194 | ignore_commands = spec.get('ignoreCommandMonitoringEvents', [])
|
194 | 195 | if observe_events:
|
195 | 196 | listener = EventListenerUtil(observe_events, ignore_commands)
|
196 |
| - client = rs_or_single_client(event_listeners=[listener]) |
| 197 | + client = rs_or_single_client( |
| 198 | + event_listeners=[listener], **uri_options) |
197 | 199 | else:
|
198 | 200 | listener = None
|
199 | 201 | client = rs_or_single_client()
|
@@ -321,11 +323,15 @@ def _operation_matchesHexBytes(self, spec, actual, key_to_compare):
|
321 | 323 | raise NotImplementedError
|
322 | 324 |
|
323 | 325 | def _operation_unsetOrMatches(self, spec, actual, key_to_compare):
|
| 326 | + if key_to_compare is None and not actual: |
| 327 | + # top-level document can be None when unset |
| 328 | + return |
| 329 | + |
324 | 330 | if key_to_compare not in actual:
|
325 | 331 | # we add a dummy value for the compared key to pass map size check
|
326 |
| - actual[key_to_compare] = None |
| 332 | + actual[key_to_compare] = 'dummyValue' |
327 | 333 | return
|
328 |
| - self._test_class.assertEqual(spec, actual[key_to_compare]) |
| 334 | + self.match_result(spec, actual[key_to_compare], in_recursive_call=True) |
329 | 335 |
|
330 | 336 | def _operation_sessionLsid(self, spec, actual):
|
331 | 337 | raise NotImplementedError
|
@@ -353,21 +359,30 @@ def _evaluate_if_special_operation(self, expectation, actual,
|
353 | 359 | if not isinstance(expectation, abc.Mapping):
|
354 | 360 | return False
|
355 | 361 |
|
356 |
| - if len(expectation) == 1: |
357 |
| - field_name, spec = next(iteritems(expectation)) |
358 |
| - elif key_to_compare is not None: |
359 |
| - field_name, spec = key_to_compare, expectation[key_to_compare] |
360 |
| - else: |
361 |
| - return False |
362 |
| - |
363 |
| - if not (isinstance(spec, abc.Mapping) and len(spec) == 1): |
364 |
| - return False |
| 362 | + is_special_op, opname, spec = False, False, False |
365 | 363 |
|
366 |
| - opname, payload = next(iteritems(spec)) |
367 |
| - if opname.startswith('$$'): |
| 364 | + if key_to_compare is not None: |
| 365 | + if key_to_compare.startswith('$$'): |
| 366 | + is_special_op = True |
| 367 | + opname = key_to_compare |
| 368 | + spec = expectation[key_to_compare] |
| 369 | + key_to_compare = None |
| 370 | + else: |
| 371 | + nested = expectation[key_to_compare] |
| 372 | + if isinstance(nested, abc.Mapping) and len(nested) == 1: |
| 373 | + opname, spec = next(iteritems(nested)) |
| 374 | + if opname.startswith('$$'): |
| 375 | + is_special_op = True |
| 376 | + elif len(expectation) == 1: |
| 377 | + opname, spec = next(iteritems(expectation)) |
| 378 | + if opname.startswith('$$'): |
| 379 | + is_special_op = True |
| 380 | + key_to_compare = None |
| 381 | + |
| 382 | + if is_special_op: |
368 | 383 | self._evaluate_special_operation(
|
369 | 384 | opname=opname,
|
370 |
| - spec=payload, |
| 385 | + spec=spec, |
371 | 386 | actual=actual,
|
372 | 387 | key_to_compare=key_to_compare)
|
373 | 388 | return True
|
@@ -546,10 +561,15 @@ def process_error(self, exception, spec):
|
546 | 561 | raise NotImplementedError
|
547 | 562 |
|
548 | 563 | if error_labels_contain:
|
549 |
| - raise NotImplementedError |
| 564 | + labels = [err_label for err_label in error_labels_contain |
| 565 | + if exception.has_error_label(err_label)] |
| 566 | + self.assertEqual(labels, error_labels_contain) |
550 | 567 |
|
551 | 568 | if error_labels_omit:
|
552 |
| - raise NotImplementedError |
| 569 | + for err_label in error_labels_omit: |
| 570 | + if exception.has_error_label(err_label): |
| 571 | + self.fail("Exception '%s' unexpectedly had label '%s'" % ( |
| 572 | + exception, err_label)) |
553 | 573 |
|
554 | 574 | if expect_result:
|
555 | 575 | if isinstance(exception, BulkWriteError):
|
@@ -607,21 +627,21 @@ def _collectionOperation_find(self, target, *args, **kwargs):
|
607 | 627 |
|
608 | 628 | def _collectionOperation_findOneAndReplace(self, target, *args, **kwargs):
|
609 | 629 | self.__raise_if_unsupported('findOneAndReplace', target, Collection)
|
610 |
| - find_cursor = target.find_one_and_replace(*args, **kwargs) |
611 |
| - return list(find_cursor) |
| 630 | + return target.find_one_and_replace(*args, **kwargs) |
612 | 631 |
|
613 | 632 | def _collectionOperation_findOneAndUpdate(self, target, *args, **kwargs):
|
614 | 633 | self.__raise_if_unsupported('findOneAndReplace', target, Collection)
|
615 |
| - find_cursor = target.find_one_and_update(*args, **kwargs) |
616 |
| - return list(find_cursor) |
| 634 | + return target.find_one_and_update(*args, **kwargs) |
617 | 635 |
|
618 | 636 | def _collectionOperation_insertMany(self, target, *args, **kwargs):
|
619 | 637 | self.__raise_if_unsupported('insertMany', target, Collection)
|
620 |
| - return target.insert_many(*args, **kwargs) |
| 638 | + result = target.insert_many(*args, **kwargs) |
| 639 | + return {idx: _id for idx, _id in enumerate(result.inserted_ids)} |
621 | 640 |
|
622 | 641 | def _collectionOperation_insertOne(self, target, *args, **kwargs):
|
623 | 642 | self.__raise_if_unsupported('insertOne', target, Collection)
|
624 |
| - return target.insert_one(*args, **kwargs) |
| 643 | + result = target.insert_one(*args, **kwargs) |
| 644 | + return {'insertedId': result.inserted_id} |
625 | 645 |
|
626 | 646 | def _sessionOperation_withTransaction(self, target, *args, **kwargs):
|
627 | 647 | self.__raise_if_unsupported('withTransaction', target, ClientSession)
|
@@ -677,9 +697,6 @@ def run_entity_operation(self, spec):
|
677 | 697 | if expect_error:
|
678 | 698 | return self.process_error(exc, expect_error)
|
679 | 699 | raise
|
680 |
| - else: |
681 |
| - if isinstance(result, Cursor): |
682 |
| - result = list(result) |
683 | 700 |
|
684 | 701 | if 'expectResult' in spec:
|
685 | 702 | self.match_evaluator.match_result(spec['expectResult'], result)
|
|
0 commit comments