forked from bufbuild/protoc-gen-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harness.py
69 lines (58 loc) · 2.54 KB
/
harness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
import inspect
from python.protoc_gen_validate.validator import validate, validate_all, ValidationFailed
from tests.harness.harness_pb2 import TestCase, TestResult
from tests.harness.cases.bool_pb2 import *
from tests.harness.cases.bytes_pb2 import *
from tests.harness.cases.enums_pb2 import *
from tests.harness.cases.enums_pb2 import *
from tests.harness.cases.messages_pb2 import *
from tests.harness.cases.numbers_pb2 import *
from tests.harness.cases.oneofs_pb2 import *
from tests.harness.cases.repeated_pb2 import *
from tests.harness.cases.strings_pb2 import *
from tests.harness.cases.maps_pb2 import *
from tests.harness.cases.wkt_any_pb2 import *
from tests.harness.cases.wkt_duration_pb2 import *
from tests.harness.cases.wkt_nested_pb2 import *
from tests.harness.cases.wkt_wrappers_pb2 import *
from tests.harness.cases.wkt_timestamp_pb2 import *
from tests.harness.cases.kitchen_sink_pb2 import *
message_classes = {}
for k, v in inspect.getmembers(sys.modules[__name__], inspect.isclass):
if 'DESCRIPTOR' in dir(v):
message_classes[v.DESCRIPTOR.full_name] = v
if __name__ == "__main__":
read = sys.stdin.buffer.read()
testcase = TestCase()
testcase.ParseFromString(read)
test_class = message_classes[testcase.message.TypeName()]
test_msg = test_class()
testcase.message.Unpack(test_msg)
try:
result = TestResult()
valid = validate(test_msg)
result.Valid = True
except ValidationFailed as e:
result.Valid = False
result.Reasons[:] = [repr(e)]
try:
result_all = TestResult()
valid = validate_all(test_msg)
result_all.Valid = True
except ValidationFailed as e:
result_all.Valid = False
result_all.Reasons[:] = [repr(e)]
if result.Valid != result_all.Valid:
raise ValueError(f"validation results mismatch, validate: {result.Valid}, "
f"validate_all: {result_all.Valid}")
if not result.Valid:
reason = list(result.Reasons)[0] # ValidationFailed("reason")
reason = reason[18:-2] # reason
reason_all = list(result_all.Reasons)[0] # ValidationFailed("reason1\nreason2\n...reason")
reason_all = reason_all[18:-2] # reason1\nreason2\n...reason
if not reason_all.startswith(reason):
raise ValueError(f"different first message, validate: {reason}, "
f"validate_all: {reason_all}")
sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8')
sys.stdout.write(result.SerializeToString().decode("utf-8"))