Utility class that implements methods for evaluating matches as per the unified test format specification.
| 373 | |
| 374 | |
| 375 | class MatchEvaluatorUtil: |
| 376 | """Utility class that implements methods for evaluating matches as per |
| 377 | the unified test format specification. |
| 378 | """ |
| 379 | |
| 380 | def __init__(self, test_class): |
| 381 | self.test = test_class |
| 382 | |
| 383 | def _operation_exists(self, spec, actual, key_to_compare): |
| 384 | if spec is True: |
| 385 | if key_to_compare is None: |
| 386 | assert actual is not None |
| 387 | else: |
| 388 | self.test.assertIn(key_to_compare, actual) |
| 389 | elif spec is False: |
| 390 | if key_to_compare is None: |
| 391 | assert actual is None |
| 392 | else: |
| 393 | self.test.assertNotIn(key_to_compare, actual) |
| 394 | else: |
| 395 | self.test.fail(f"Expected boolean value for $$exists operator, got {spec}") |
| 396 | |
| 397 | def __type_alias_to_type(self, alias): |
| 398 | if alias not in BSON_TYPE_ALIAS_MAP: |
| 399 | self.test.fail(f"Unrecognized BSON type alias {alias}") |
| 400 | return BSON_TYPE_ALIAS_MAP[alias] |
| 401 | |
| 402 | def _operation_type(self, spec, actual, key_to_compare): |
| 403 | if isinstance(spec, abc.MutableSequence): |
| 404 | permissible_types = tuple( |
| 405 | [t for alias in spec for t in self.__type_alias_to_type(alias)] |
| 406 | ) |
| 407 | else: |
| 408 | permissible_types = self.__type_alias_to_type(spec) |
| 409 | value = actual[key_to_compare] if key_to_compare else actual |
| 410 | self.test.assertIsInstance(value, permissible_types) |
| 411 | |
| 412 | def _operation_matchesEntity(self, spec, actual, key_to_compare): |
| 413 | expected_entity = self.test.entity_map[spec] |
| 414 | self.test.assertEqual(expected_entity, actual[key_to_compare]) |
| 415 | |
| 416 | def _operation_matchesHexBytes(self, spec, actual, key_to_compare): |
| 417 | expected = binascii.unhexlify(spec) |
| 418 | value = actual[key_to_compare] if key_to_compare else actual |
| 419 | self.test.assertEqual(value, expected) |
| 420 | |
| 421 | def _operation_unsetOrMatches(self, spec, actual, key_to_compare): |
| 422 | if key_to_compare is None and not actual: |
| 423 | # top-level document can be None when unset |
| 424 | return |
| 425 | |
| 426 | if key_to_compare not in actual: |
| 427 | # we add a dummy value for the compared key to pass map size check |
| 428 | actual[key_to_compare] = "dummyValue" |
| 429 | return |
| 430 | self.match_result(spec, actual[key_to_compare], in_recursive_call=True) |
| 431 | |
| 432 | def _operation_sessionLsid(self, spec, actual, key_to_compare): |