userver: en/testsuite/protobuf/pytest_plugin.py Source File
Loading...
Searching...
No Matches
pytest_plugin.py
1import google.protobuf.message
2
3from testsuite.plugins.assertrepr_compare import CompareVisitor
4from testsuite.protobuf.matching import ProtobufDictsImpl
5from testsuite.protobuf.utils import message_to_dict
6
7
8def _is_proto(value):
9 return isinstance(value, google.protobuf.message.Message)
10
11
12def _is_proto_dict(value):
13 return isinstance(value, ProtobufDictsImpl)
14
15
16def _protobuf_predicate(left, right):
17 return _is_proto(left) and _is_proto(right)
18
19
20def _protobuf_visitor(left, right, reporter):
21 return message_to_dict(left), message_to_dict(right)
22
23
24def _proto_dicts_predicate(left, right):
25 return (_is_proto(left) and _is_proto_dict(right)) or (
26 _is_proto_dict(left) and _is_proto(right)
27 )
28
29
30def _proto_dicts_visitor(left, right, reporter):
31 if _is_proto(left):
32 left = message_to_dict(left)
33 right = right._impl
34 if isinstance(right, ProtobufDictsImpl):
35 right = right._impl
36 else:
37 right = message_to_dict(right)
38 if isinstance(left, ProtobufDictsImpl):
39 left = left._impl
40
41 return left, right
42
43
44def pytest_register_compare_visitors() -> list[CompareVisitor]:
45 return [
46 CompareVisitor(predicate=_protobuf_predicate, visit=_protobuf_visitor),
47 CompareVisitor(
48 predicate=_proto_dicts_predicate, visit=_proto_dicts_visitor
49 ),
50 ]