-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_errors.py
107 lines (83 loc) · 2.89 KB
/
test_errors.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import enum
import dataclasses
from typing import List, Mapping, Optional
try:
from typing import Literal
except ImportError: # pragma: nocover
HAVE_LITERAL = False
else:
HAVE_LITERAL = True
import pytest
from validobj.validation import parse_input
from validobj.errors import (
UnionValidationError,
AlternativeDisplay,
NotAnEnumItemError,
WrongListItemError,
WrongFieldError,
WrongKeysError,
WrongLiteralError,
)
@dataclasses.dataclass
class Fieldset:
a: int
b: int
class E(enum.Enum):
FIELD1 = 'field1'
FIELD2 = 'field2'
def test_alternative_display():
ad = str(
AlternativeDisplay(
bad_item='bad',
alternatives=['could', 'be', 'good'],
display_all_alternatives=True,
)
)
assert 'could' in ad
ad = str(AlternativeDisplay(bad_item=['x'], alternatives=[], header='Hello'))
assert 'Hello' in ad
def test_mismatch_message():
with pytest.raises(WrongKeysError) as exinfo:
parse_input({'a': 1, 'x': 2, 'y': 2}, Fieldset)
assert 'x' in str(exinfo.value)
with pytest.raises(WrongKeysError) as exinfo:
parse_input({'a': 1, 'x': 2, 'b': 2}, Fieldset)
assert 'x' in str(exinfo.value)
with pytest.raises(WrongKeysError) as exinfo:
parse_input({'a': 1}, Fieldset)
assert 'b' in str(exinfo.value)
e = WrongKeysError(missing={'a'}, unknown={'x'}, valid={'a', 'b'})
assert 'x' in str(e)
def test_union_error_message():
with pytest.raises(UnionValidationError) as exinfo:
parse_input('FIELD3', Optional[E])
assert 'FIELD1' in str(exinfo.value)
assert any(isinstance(c, NotAnEnumItemError) for c in exinfo.value.causes)
def test_enum_error():
e = str(NotAnEnumItemError('FIELD3', E))
assert 'FIELD1' in e
def test_correct_items():
with pytest.raises(WrongListItemError) as exinfo:
parse_input([1, 'x', 2], List[int])
assert exinfo.value.wrong_index == 1
with pytest.raises(WrongFieldError) as exinfo:
parse_input({'a': 1, 'b': 'x'}, Mapping[str, int])
assert exinfo.value.wrong_field == 'b'
with pytest.raises(WrongFieldError) as exinfo:
parse_input({1: 2, 'a': 1}, Mapping[str, int])
assert exinfo.value.wrong_field == 1
with pytest.raises(WrongFieldError) as exinfo:
parse_input({'a': 'uno', 'b': 2}, Fieldset)
assert exinfo.value.wrong_field == 'a'
@pytest.mark.skipif(not HAVE_LITERAL, reason="Litral not available")
def test_literal_error():
with pytest.raises(WrongLiteralError) as exinfo:
parse_input(5, Literal[6])
assert exinfo.value.value == 5
assert exinfo.value.reference == [6,]
assert "5" in str(exinfo.value)
with pytest.raises(WrongLiteralError) as exinfo:
parse_input(5, Literal[6, Literal[7], 8])
assert exinfo.value.value == 5
assert exinfo.value.reference == [6, 7, 8]
assert "7" in str(exinfo.value)