-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
447 lines (343 loc) · 12.8 KB
/
tests.py
File metadata and controls
447 lines (343 loc) · 12.8 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import unittest
from unittest.mock import Mock
from PyYep import Schema, InputItem, ValidationError
from PyYep.validators.bool import BooleanValidator
from PyYep.validators.string import StringValidator
from PyYep.validators.numeric import NumericValidator
from PyYep.validators.array import ArrayValidator
from PyYep.validators.dict import DictValidator
from PyYep.locale.pt_BR import DocumentsValidators as DocumentsValidator_pt_BR
class TestInputItem(unittest.TestCase):
def test_validate(self):
input_ = DummyInput("test")
def custom_validator(value):
if value == "test":
return
raise ValidationError("test", "test")
form = Schema(
[InputItem("test", input_, "get_value").validate(custom_validator)]
)
self.assertEqual(form.validate()["test"], "test")
input_.value = "a"
with self.assertRaises(ValidationError):
form.validate()
def test_hooks(self):
local_success_hook = Mock(return_value="success")
global_error_hook = Mock(return_value="global_error")
local_error_hook = Mock(return_value="local_error")
def custom_validator(_):
raise ValidationError("test", "")
form = Schema(
[
InputItem("test", DummyInput(""), "get_value").validate(
custom_validator
),
InputItem(
"test1",
DummyInput(""),
"get_value",
on_fail=local_error_hook,
).validate(custom_validator),
InputItem(
"test2",
DummyInput(""),
"get_value",
on_success=local_success_hook,
),
],
global_error_hook,
False,
)
try:
form.validate()
except ValidationError:
pass
local_success_hook.assert_called_once()
global_error_hook.assert_called_once()
local_error_hook.assert_called_once()
def test_not_aborting_early(self):
def custom_validator(value):
if value == "test":
return
raise ValidationError("test", "test")
form = Schema(
[
InputItem("a", DummyInput(""), "get_value").validate(
custom_validator
),
InputItem("b", DummyInput(""), "get_value").validate(
custom_validator
),
],
abort_early=False,
)
with self.assertRaises(ValidationError):
form.validate()
try:
form.validate()
except ValidationError as error:
self.assertTrue(error.inner)
def test_conditions(self):
input_ = DummyInput("test01")
def custom_validator(value):
raise ValidationError("test", "test")
form = Schema(
[
InputItem("test", input_, "get_value")
.validate(custom_validator)
.condition(lambda v: v == "test01")
]
)
with self.assertRaises(ValidationError):
form.validate()
input_.value = "test02"
self.assertEqual(form.validate()["test"], "test02")
def test_modifier(self):
def modifier(x: str | None) -> str | None:
if x is None:
return x
return x + "02"
form = Schema(
[
InputItem[str](
"test", DummyInput("test"), "get_value"
).modifier(modifier)
]
)
self.assertEqual(form.validate()["test"], "test02")
class TestStringValidator(unittest.TestCase):
def test_required(self):
input_ = DummyInput("")
form = Schema(
[InputItem("test", input_, "get_value").string().required()]
)
with self.assertRaises(ValidationError):
form.validate()
input_.value = None # type: ignore
with self.assertRaises(ValidationError):
form.validate()
def test_email(self):
input_ = DummyInput("test@test.com")
form = Schema(
[InputItem("email", input_, "get_value").string().email()]
)
self.assertEqual(form.validate()["email"], "test@test.com")
input_.value = 10 # type: ignore
with self.assertRaises(ValidationError):
form.validate()
input_.value = "test"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "test@test"
with self.assertRaises(ValidationError):
form.validate()
def test_min_and_max(self):
input_ = DummyInput("12345")
form = Schema(
[InputItem("test", input_, "get_value").string().min(5).max(10)]
)
self.assertEqual(form.validate()["test"], "12345")
input_.value = "1234567890"
self.assertEqual(form.validate()["test"], "1234567890")
input_.value = "1234"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "1234567890+"
with self.assertRaises(ValidationError):
form.validate()
def test_in_(self):
input_ = DummyInput("12345")
form = Schema(
[
InputItem("test", input_, "get_value")
.string()
.in_(["", "1", "12345"])
]
)
self.assertEqual(form.validate()["test"], "12345")
input_.value = "1234"
with self.assertRaises(ValidationError):
form.validate()
class TestNumberValidator(unittest.TestCase):
def test_min_and_max(self):
input_ = DummyInput(5)
form = Schema(
[InputItem("test", input_, "get_value").number().min(5).max(10)]
)
self.assertEqual(form.validate()["test"], 5)
input_.value = 10
self.assertEqual(form.validate()["test"], 10)
input_.value = 4
with self.assertRaises(ValidationError):
form.validate()
input_.value = 11
with self.assertRaises(ValidationError):
form.validate()
class TestDocumentValidator_pt_BR(unittest.TestCase):
def test_cpf(self):
input_ = DummyInput("875.920.020-00")
form = Schema(
[
InputItem("test", input_, "get_value").validate(
DocumentsValidator_pt_BR().cpf
)
]
)
self.assertEqual(form.validate()["test"], "875.920.020-00")
input_.value = "875.920.020-01"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "875.920.020"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "111.111.111-11"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "875.920.020-20"
with self.assertRaises(ValidationError):
form.validate()
def test_cnpj(self):
input_ = DummyInput("88.724.415/0001-59")
form = Schema(
[
InputItem("test", input_, "get_value").validate(
DocumentsValidator_pt_BR().cnpj
)
]
)
self.assertEqual(form.validate()["test"], "88.724.415/0001-59")
input_.value = "88.724.415/0001-58"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "88.724.415+0001-58"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "88.888.888/8888-88"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "88.724.415/0001-49"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "10.000.000/6540-67"
with self.assertRaises(ValidationError):
form.validate()
input_.value = "10.000.000/3081-96"
with self.assertRaises(ValidationError):
form.validate()
class TestArrayValidator(unittest.TestCase):
def test_type_validation(self):
input_ = DummyInput([1, 2])
form = Schema([InputItem("test", input_, "get_value").array()])
self.assertEqual(form.validate()["test"], [1, 2])
input_.value = 1 # type: ignore
with self.assertRaises(ValidationError):
form.validate()
def test_of(self):
input_ = DummyInput([1, 2])
form = Schema(
[
InputItem("test", input_, "get_value")
.array()
.of(NumericValidator().required())
]
)
self.assertEqual(form.validate()["test"], [1, 2])
input_.value = [1, "teste"]
with self.assertRaises(ValidationError):
form.validate()
def test_length(self):
input_ = DummyInput([1])
form = Schema([InputItem("test", input_, "get_value").array().len(1)])
self.assertEqual(form.validate()["test"], [1])
input_.value = []
with self.assertRaises(ValidationError):
form.validate()
input_.value = [1, 2]
with self.assertRaises(ValidationError):
form.validate()
def test_min_and_max(self):
input_ = DummyInput([1, 2, 3])
form = Schema(
[InputItem("test", input_, "get_value").array().min(3).max(5)]
)
self.assertEqual(form.validate()["test"], [1, 2, 3])
input_.value = [1, 2, 3, 4, 5]
self.assertEqual(form.validate()["test"], [1, 2, 3, 4, 5])
input_.value = [1, 2]
with self.assertRaises(ValidationError):
form.validate()
input_.value = [1, 2, 3, 4, 5, 6]
with self.assertRaises(ValidationError):
form.validate()
def test_includes(self):
input_ = DummyInput([1, 2, 3])
form = Schema(
[InputItem("test", input_, "get_value").array().includes(3)]
)
self.assertEqual(form.validate()["test"], [1, 2, 3])
input_.value = [1, 2]
with self.assertRaises(ValidationError):
form.validate()
class TestDictValidator(unittest.TestCase):
def test_type_validation(self):
input_ = DummyInput({"test": 10})
form = Schema([InputItem("test", input_, "get_value").dict()])
self.assertEqual(form.validate()["test"], {"test": 10})
input_.value = 1 # type: ignore
with self.assertRaises(ValidationError):
form.validate()
def test_shape(self):
fake_data = {"string": "test", "number": 10, "list": [1, 2, 3]}
schema = DictValidator().shape(
{
"string": StringValidator().required(),
"number": NumericValidator().max(10).required(),
"list": ArrayValidator().of(
NumericValidator().max(3).required()
),
}
)
self.assertEqual(schema.verify(fake_data), fake_data)
fake_data["number"] = 11
with self.assertRaises(ValidationError):
schema.verify(fake_data)
class TestBooleanValidator(unittest.TestCase):
def test_to_be_true(self):
form = DictValidator().shape({"test": BooleanValidator().to_be(True)})
true_values = [True, 1, [1], {"1": 1}]
false_values = [False, 0, [], {}, None]
for value in true_values:
self.assertEqual(form.verify({"test": value})["test"], True)
for value in false_values:
with self.assertRaises(ValidationError):
form.verify({"test": value})
def test_to_be_false(self):
form = DictValidator().shape({"test": BooleanValidator().to_be(False)})
true_values = [True, 1, [1], {"1": 1}]
false_values = [False, 0, [], {}, None]
for value in false_values:
self.assertEqual(form.verify({"test": value})["test"], False)
for value in true_values:
with self.assertRaises(ValidationError):
form.verify({"test": value})
def test_to_be_true_strict(self):
form = DictValidator().shape(
{"test": BooleanValidator(strict=True).to_be(True)}
)
test_values = [1, [1], {"1": 1}]
for value in test_values:
with self.assertRaises(ValidationError):
form.verify({"test": value})
def test_to_be_false_strict(self):
form = DictValidator().shape(
{"test": BooleanValidator(strict=True).to_be(False)}
)
test_values = [0, [], {}, None]
for value in test_values:
with self.assertRaises(ValidationError):
form.verify({"test": value})
class DummyInput:
def __init__(self, value):
self.value = value
def get_value(self):
return self.value