3from testsuite
import matching
6def _default_regex_match(doc: dict):
7 return matching.RegexString(doc[
'pattern'])
10def _default_partial_dict_match(doc: dict):
11 return matching.PartialDict(doc[
'value'])
14def _match_unordered_list(doc: dict):
17 key = _make_keys_getter(doc[
'keys'])
19 key = _make_key_getter(doc[
'key'])
22 return matching.unordered_list(items, key=key)
25def _match_list_of(doc):
26 return matching.ListOf(value=doc.get(
'item', matching.any_value))
29def _match_dict_of(doc):
30 return matching.DictOf(
31 key=doc.get(
'key', matching.any_value),
32 value=doc.get(
'value', matching.any_value),
36def pytest_register_matching_hooks():
38 'any-value': matching.any_value,
39 'any-float': matching.any_float,
40 'any-integer': matching.any_integer,
41 'any-numeric': matching.any_numeric,
42 'positive-float': matching.positive_float,
43 'positive-integer': matching.positive_integer,
44 'positive-numeric': matching.positive_numeric,
45 'negative-float': matching.negative_float,
46 'negative-integer': matching.negative_integer,
47 'negative-numeric': matching.negative_numeric,
48 'non-negative-float': matching.non_negative_float,
49 'non-negative-integer': matching.non_negative_integer,
50 'non-negative-numeric': matching.non_negative_numeric,
51 'any-string': matching.any_string,
52 'uuid-string': matching.uuid_string,
53 'objectid-string': matching.objectid_string,
54 'datetime-string': matching.datetime_string,
55 'regex': _default_regex_match,
57 'any-dict': matching.any_dict,
58 'dict-of': _match_dict_of,
59 'partial-dict': _default_partial_dict_match,
61 'any-list': matching.any_list,
62 'list-of': _match_list_of,
63 'unordered-list': _match_unordered_list,
64 'unordered_list': _match_unordered_list,
69 def pytest_register_matching_hooks(self):
75 self._matching_hooks = {}
78 def matching_hooks(self):
79 return self._matching_hooks
81 def pytest_sessionstart(self, session):
83 session.config.pluginmanager.hook.pytest_register_matching_hooks()
86 self._matching_hooks.update(hook)
88 def pytest_addhooks(self, pluginmanager):
89 pluginmanager.add_hookspecs(Hookspec)
92def pytest_configure(config):
93 config.pluginmanager.register(MatchingPlugin(),
'matching_params')
96@pytest.fixture(scope='session')
97def operator_match(request, pytestconfig):
98 plugin = pytestconfig.pluginmanager.get_plugin(
'matching_params')
100 def match(doc: dict):
101 match_type = doc.get(
'type')
103 hook = plugin.matching_hooks[match_type]
105 raise RuntimeError(f
'Unknown match type {match_type}')
113@pytest.fixture(scope='session')
114def match_operator(operator_match):
115 def _wrapper(doc: dict):
116 match = doc[
'$match']
117 if isinstance(match, str):
118 match = {
'type': match}
119 return operator_match(match)
124def _make_keys_getter(keys):
125 key_getters = tuple(_make_key_getter(key)
for key
in keys)
128 return tuple(getter(value)
for getter
in key_getters)
133def _make_key_getter(path):
134 if isinstance(path, str):