-
Notifications
You must be signed in to change notification settings - Fork 45
/
test_validate_yaml.py
62 lines (51 loc) 路 1.8 KB
/
test_validate_yaml.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
import pytest
from typesystem import Integer, Object, Schema, ValidationError, validate_yaml
from typesystem.base import Message, Position
def test_validate_yaml():
validator = Object(properties=Integer())
text = "a: 123\nb: 456\n"
value = validate_yaml(text, validator=validator)
assert value == {"a": 123, "b": 456}
validator = Object(properties=Integer())
text = "a: 123\nb: abc\n"
with pytest.raises(ValidationError) as exc_info:
validate_yaml(text, validator=validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="Must be a number.",
code="type",
index=["b"],
start_position=Position(line_no=2, column_no=4, char_index=10),
end_position=Position(line_no=2, column_no=6, char_index=12),
)
]
class Validator(Schema):
a = Integer()
b = Integer()
text = "a: 123\nb: abc\n"
with pytest.raises(ValidationError) as exc_info:
validate_yaml(text, validator=Validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="Must be a number.",
code="type",
index=["b"],
start_position=Position(line_no=2, column_no=4, char_index=10),
end_position=Position(line_no=2, column_no=6, char_index=12),
)
]
text = "a: 123"
with pytest.raises(ValidationError) as exc_info:
validate_yaml(text, validator=Validator)
exc = exc_info.value
assert exc.messages() == [
Message(
text="The field 'b' is required.",
code="required",
index=["b"],
start_position=Position(line_no=1, column_no=1, char_index=0),
end_position=Position(line_no=1, column_no=6, char_index=5),
)
]