-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_string_validator.py
203 lines (151 loc) · 5.76 KB
/
test_string_validator.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import pytest
from _plotly_utils.basevalidators import StringValidator
import numpy as np
from plotly.tests.test_optional.test_utils.test_utils import np_nan
# Fixtures
# --------
@pytest.fixture()
def validator():
return StringValidator("prop", "parent")
@pytest.fixture()
def validator_values():
return StringValidator("prop", "parent", values=["foo", "BAR", ""])
@pytest.fixture()
def validator_no_blanks():
return StringValidator("prop", "parent", no_blank=True)
@pytest.fixture()
def validator_strict():
return StringValidator("prop", "parent", strict=True)
@pytest.fixture
def validator_aok():
return StringValidator("prop", "parent", array_ok=True, strict=False)
@pytest.fixture
def validator_aok_strict():
return StringValidator("prop", "parent", array_ok=True, strict=True)
@pytest.fixture
def validator_aok_values():
return StringValidator(
"prop", "parent", values=["foo", "BAR", "", "baz"], array_ok=True
)
@pytest.fixture()
def validator_no_blanks_aok():
return StringValidator("prop", "parent", no_blank=True, array_ok=True)
# Array not ok
# ------------
# Not strict
# ### Acceptance ###
@pytest.mark.parametrize(
"val", ["bar", 234, np_nan(), "HELLO!!!", "world!@#$%^&*()", "", "\u03BC"]
)
def test_acceptance(val, validator):
expected = str(val) if not isinstance(val, str) else val
assert validator.validate_coerce(val) == expected
# ### Rejection by value ###
@pytest.mark.parametrize("val", [(), [], [1, 2, 3], set()])
def test_rejection(val, validator):
with pytest.raises(ValueError) as validation_failure:
validator.validate_coerce(val)
assert "Invalid value" in str(validation_failure.value)
# Valid values
# ------------
@pytest.mark.parametrize("val", ["foo", "BAR", ""])
def test_acceptance_values(val, validator_values):
assert validator_values.validate_coerce(val) == val
@pytest.mark.parametrize("val", ["FOO", "bar", "other", "1234"])
def test_rejection_values(val, validator_values):
with pytest.raises(ValueError) as validation_failure:
validator_values.validate_coerce(val)
assert "Invalid value".format(val=val) in str(validation_failure.value)
assert "['foo', 'BAR', '']" in str(validation_failure.value)
# ### No blanks ###
@pytest.mark.parametrize("val", ["bar", "HELLO!!!", "world!@#$%^&*()", "\u03BC"])
def test_acceptance_no_blanks(val, validator_no_blanks):
assert validator_no_blanks.validate_coerce(val) == val
@pytest.mark.parametrize("val", [""])
def test_rejection_no_blanks(val, validator_no_blanks):
with pytest.raises(ValueError) as validation_failure:
validator_no_blanks.validate_coerce(val)
assert "A non-empty string" in str(validation_failure.value)
# Strict
# ------
# ### Acceptance ###
@pytest.mark.parametrize("val", ["bar", "HELLO!!!", "world!@#$%^&*()", "", "\u03BC"])
def test_acceptance_strict(val, validator_strict):
assert validator_strict.validate_coerce(val) == val
# ### Rejection by value ###
@pytest.mark.parametrize("val", [(), [], [1, 2, 3], set(), np_nan(), np.pi, 23])
def test_rejection_strict(val, validator_strict):
with pytest.raises(ValueError) as validation_failure:
validator_strict.validate_coerce(val)
assert "Invalid value" in str(validation_failure.value)
# Array ok
# --------
# ### Acceptance ###
@pytest.mark.parametrize("val", ["foo", "BAR", "", "baz", "\u03BC"])
def test_acceptance_aok_scalars(val, validator_aok):
assert validator_aok.validate_coerce(val) == val
@pytest.mark.parametrize(
"val",
[
"foo",
["foo"],
np.array(["BAR", "", "\u03BC"], dtype="object"),
["baz", "baz", "baz"],
["foo", None, "bar", "\u03BC"],
],
)
def test_acceptance_aok_list(val, validator_aok):
coerce_val = validator_aok.validate_coerce(val)
if isinstance(val, np.ndarray):
assert isinstance(coerce_val, np.ndarray)
assert np.array_equal(coerce_val, np.array(val, dtype=coerce_val.dtype))
elif isinstance(val, list):
assert validator_aok.present(val) == tuple(val)
else:
assert coerce_val == val
# ### Rejection by type ###
@pytest.mark.parametrize("val", [["foo", ()], ["foo", 3, 4], [3, 2, 1]])
def test_rejection_aok(val, validator_aok_strict):
with pytest.raises(ValueError) as validation_failure:
validator_aok_strict.validate_coerce(val)
assert "Invalid element(s)" in str(validation_failure.value)
# ### Rejection by value ###
@pytest.mark.parametrize(
"val", [["foo", "bar"], ["3", "4"], ["BAR", "BAR", "hello!"], ["foo", None]]
)
def test_rejection_aok_values(val, validator_aok_values):
with pytest.raises(ValueError) as validation_failure:
validator_aok_values.validate_coerce(val)
assert "Invalid element(s)" in str(validation_failure.value)
# ### No blanks ###
@pytest.mark.parametrize(
"val",
[
"123",
["bar", "HELLO!!!"],
np.array(["bar", "HELLO!!!"], dtype="object"),
["world!@#$%^&*()", "\u03BC"],
],
)
def test_acceptance_no_blanks_aok(val, validator_no_blanks_aok):
coerce_val = validator_no_blanks_aok.validate_coerce(val)
if isinstance(val, np.ndarray):
assert np.array_equal(coerce_val, np.array(val, dtype=coerce_val.dtype))
elif isinstance(val, list):
assert validator_no_blanks_aok.present(coerce_val) == tuple(val)
else:
assert coerce_val == val
@pytest.mark.parametrize(
"val",
[
"",
["foo", "bar", ""],
np.array(["foo", "bar", ""], dtype="object"),
[""],
np.array([""], dtype="object"),
],
)
def test_rejection_no_blanks_aok(val, validator_no_blanks_aok):
with pytest.raises(ValueError) as validation_failure:
validator_no_blanks_aok.validate_coerce(val)
assert "A non-empty string" in str(validation_failure.value)