-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstr_test.py
272 lines (173 loc) · 7.19 KB
/
str_test.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import uuid
import pytest
import zon
@pytest.fixture
def validator():
return zon.string()
def test_str_validate(validator):
assert validator.validate("1")
with pytest.raises(zon.error.ZonError):
validator.validate(1.5)
with pytest.raises(zon.error.ZonError):
validator.validate(1)
with pytest.raises(zon.error.ZonError):
validator.validate([1])
with pytest.raises(zon.error.ZonError):
validator.validate({"a": 1})
def test_str_safe_validate(validator):
assert validator.safe_validate("1") == (True, "1")
# TODO: check for the specific error?
assert validator.safe_validate(1.5)[0] is False
assert validator.safe_validate(1)[0] is False
assert validator.safe_validate([1])[0] is False
assert validator.safe_validate({"a": 1})[0] is False
def test_str_length_equal(validator):
_validator = validator.length(2)
assert _validator.validate("12")
with pytest.raises(zon.error.ZonError):
_validator.validate("1")
def test_str_length_less_than(validator):
_validator = validator.max(2)
assert _validator.validate("1")
assert _validator.validate("12")
with pytest.raises(zon.error.ZonError):
_validator.validate("123")
def test_str_length_greater_than(validator):
_validator = validator.min(2)
assert _validator.validate("123")
assert _validator.validate("12")
with pytest.raises(zon.error.ZonError):
_validator.validate("1")
def test_str_email(validator):
_validator = validator.email()
assert _validator.validate("test@host.com")
with pytest.raises(zon.error.ZonError):
_validator.validate("test@host")
with pytest.raises(zon.error.ZonError):
_validator.validate("@host.com")
with pytest.raises(zon.error.ZonError):
_validator.validate("host.com")
def test_str_url(validator):
_validator = validator.url()
assert _validator.validate("https://www.google.com")
assert _validator.validate("http://www.google.com")
with pytest.raises(zon.error.ZonError):
_validator.validate("www.google.com")
with pytest.raises(zon.error.ZonError):
_validator.validate("google.com")
with pytest.raises(zon.error.ZonError):
_validator.validate("google.com/")
with pytest.raises(zon.error.ZonError):
_validator.validate("google.com")
def test_str_emoji(validator):
with pytest.raises(NotImplementedError):
_validator = validator.emoji()
"""
_validator = validator.emoji()
assert _validator.validate("😀")
assert _validator.validate("🤩")
assert _validator.validate("🤨")
assert _validator.validate("😐")
assert _validator.validate("😑")
with pytest.raises(zon.error.ZonError):
assert not _validator.validate("1")
"""
def test_str_uuid(validator):
_validator = validator.uuid()
assert _validator.validate(uuid.uuid4().hex)
with pytest.raises(zon.error.ZonError):
_validator.validate("not_a_UUID")
# TODO: cuid, cuid2, nanoid, ulid
def test_str_regex(validator):
_validator = validator.regex(r"^[a-z ]+$")
assert _validator.validate("abc")
assert _validator.validate("def")
assert _validator.validate("abc def")
with pytest.raises(zon.error.ZonError):
_validator.validate("abc1")
with pytest.raises(zon.error.ZonError):
_validator.validate("abc def1")
def test_str_includes(validator):
_validator = validator.includes("abc")
assert _validator.validate("abc")
assert _validator.validate("abc def")
with pytest.raises(zon.error.ZonError):
_validator.validate("def")
def test_str_starts_with(validator):
_validator = validator.starts_with("abc")
assert _validator.validate("abc")
assert _validator.validate("abc def")
with pytest.raises(zon.error.ZonError):
_validator.validate("def")
def test_str_ends_with(validator):
_validator = validator.ends_with("abc")
assert _validator.validate("abc")
assert _validator.validate("def abc")
with pytest.raises(zon.error.ZonError):
_validator.validate("def")
def test_str_datetime_no_opts(validator):
_validator = (
validator.datetime()
) # this also validates the case where precision is passed in as 'None', since the behavior is the same
assert _validator.validate("2020-01-01T00:00:00Z")
assert _validator.validate("2020-01-01T00:00:00.123Z")
assert _validator.validate("2020-01-01T00:00:00.123456Z")
with pytest.raises(zon.error.ZonError):
_validator.validate("2020-01-01T00:00:00+02:00")
def test_str_datetime_precision(validator):
_validator = validator.datetime({"precision": 3})
assert _validator.validate("2020-01-01T00:00:00.123Z")
with pytest.raises(zon.error.ZonError):
_validator.validate("2020-01-01T00:00:00.12345678Z")
with pytest.raises(zon.error.ZonError):
_validator.validate("2020-01-01T00:00:00Z")
def test_str_datetime_offset(validator):
_validator = validator.datetime({"offset": True})
# test examples taken from https://zod.dev/?id=datetimes
assert _validator.validate("2020-01-01T00:00:00+02:00")
assert _validator.validate("2020-01-01T00:00:00.123+02:00")
assert _validator.validate("2020-01-01T00:00:00.123-0200")
assert _validator.validate("2020-01-01T00:00:00.123-02")
assert _validator.validate("2020-01-01T00:00:00Z")
def test_str_datetime_local(validator):
_validator = validator.datetime({"local": True})
assert _validator.validate("2020-01-01T00:00:00Z")
assert _validator.validate("2020-01-01T00:00:00")
def test_str_datetime_complex(validator):
_validator = validator.datetime({"local": True, "offset": True, "precision": 3})
# local (no 'Z'), with hour-only offset, 3 digit precision
assert _validator.validate("2020-01-01T00:00:00.123+02")
# TODO: use classes to group tests
def test_str_ip_all(validator):
_validator = validator.ip()
assert _validator.validate("127.0.0.1")
assert _validator.validate("0.0.0.0")
assert _validator.validate("::")
assert _validator.validate("::1")
assert _validator.validate("::ffff:127.0.0.1")
with pytest.raises(zon.error.ZonError):
_validator.validate("1.1.1")
with pytest.raises(zon.error.ZonError):
_validator.validate("::1.1.1")
def test_str_ip_v4(validator):
_validator = validator.ip({"version": "v4"})
assert _validator.validate("255.255.255.255")
assert _validator.validate("0.0.0.0")
with pytest.raises(zon.error.ZonError):
_validator.validate("::ffff:127.0.0.1")
def test_str_ip_v6(validator):
_validator = validator.ip({"version": "v6"})
assert _validator.validate("::")
assert _validator.validate("::1")
assert _validator.validate("::ffff:127.0.0.1")
with pytest.raises(zon.error.ZonError):
_validator.validate("255.255.255.255")
def test_str_trim(validator):
_validator = validator.trim()
assert _validator.validate(" abc ") == "abc"
def test_str_to_lower_case(validator):
_validator = validator.to_lower_case()
assert _validator.validate("AbC") == "abc"
def test_str_to_upper_case(validator):
_validator = validator.to_upper_case()
assert _validator.validate("aBc") == "ABC"