-
Notifications
You must be signed in to change notification settings - Fork 200
/
test_function.py
428 lines (312 loc) · 13.5 KB
/
test_function.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
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
import platform
import re
from copy import deepcopy
from typing import Type
import pytest
from pydantic_core import PydanticValueError, SchemaError, SchemaValidator, ValidationError
from ..conftest import plain_repr
def test_function_before():
def f(input_value, **kwargs):
return input_value + ' Changed'
v = SchemaValidator({'type': 'function', 'mode': 'before', 'function': f, 'schema': {'type': 'str'}})
assert v.validate_python('input value') == 'input value Changed'
def test_function_before_raise():
def f(input_value, **kwargs):
raise ValueError('foobar')
v = SchemaValidator({'type': 'function', 'mode': 'before', 'function': f, 'schema': {'type': 'str'}})
with pytest.raises(ValidationError) as exc_info:
assert v.validate_python('input value') == 'input value Changed'
# debug(str(exc_info.value))
assert exc_info.value.errors() == [
{
'kind': 'value_error',
'loc': [],
'message': 'Value error, foobar',
'input_value': 'input value',
'context': {'error': 'foobar'},
}
]
def test_function_before_error():
def f(input_value, **kwargs):
return input_value + 'x'
v = SchemaValidator(
{'type': 'function', 'mode': 'before', 'function': f, 'schema': {'type': 'str', 'max_length': 5}}
)
assert v.validate_python('1234') == '1234x'
with pytest.raises(ValidationError) as exc_info:
v.validate_python('12345')
assert exc_info.value.errors() == [
{
'kind': 'too_long',
'loc': [],
'message': 'String should have at most 5 characters',
'input_value': '12345x',
'context': {'max_length': 5},
}
]
def test_function_before_error_model():
def f(input_value, **kwargs):
if 'my_field' in input_value:
input_value['my_field'] += 'x'
return input_value
v = SchemaValidator(
{
'type': 'function',
'mode': 'before',
'function': f,
'schema': {'type': 'typed-dict', 'fields': {'my_field': {'schema': {'type': 'str', 'max_length': 5}}}},
}
)
assert v.validate_python({'my_field': '1234'}) == {'my_field': '1234x'}
with pytest.raises(ValidationError) as exc_info:
v.validate_python({'my_field': '12345'})
assert exc_info.value.errors() == [
{
'kind': 'too_long',
'loc': ['my_field'],
'message': 'String should have at most 5 characters',
'input_value': '12345x',
'context': {'max_length': 5},
}
]
def test_function_wrap():
def f(input_value, *, validator, **kwargs):
return validator(input_value) + ' Changed'
v = SchemaValidator({'type': 'function', 'mode': 'wrap', 'function': f, 'schema': 'str'})
assert v.validate_python('input value') == 'input value Changed'
def test_function_wrap_repr():
def f(input_value, *, validator, **kwargs):
assert repr(validator) == str(validator)
return plain_repr(validator)
v = SchemaValidator({'type': 'function', 'mode': 'wrap', 'function': f, 'schema': 'str'})
assert v.validate_python('input value') == 'ValidatorCallable(Str(StrValidator{strict:false}))'
def test_function_wrap_str():
def f(input_value, *, validator, **kwargs):
return plain_repr(validator)
v = SchemaValidator({'type': 'function', 'mode': 'wrap', 'function': f, 'schema': 'str'})
assert v.validate_python('input value') == 'ValidatorCallable(Str(StrValidator{strict:false}))'
def test_function_wrap_not_callable():
with pytest.raises(SchemaError, match='function -> function\n Input should be callable'):
SchemaValidator({'type': 'function', 'mode': 'wrap', 'function': [], 'schema': 'str'})
with pytest.raises(SchemaError, match='function -> function\n Field required'):
SchemaValidator({'type': 'function', 'mode': 'wrap', 'schema': 'str'})
def test_wrap_error():
def f(input_value, *, validator, **kwargs):
return validator(input_value) * 2
v = SchemaValidator({'type': 'function', 'mode': 'wrap', 'function': f, 'schema': 'int'})
assert v.validate_python('42') == 84
with pytest.raises(ValidationError) as exc_info:
v.validate_python('wrong')
assert exc_info.value.errors() == [
{
'kind': 'int_parsing',
'loc': [],
'message': 'Input should be a valid integer, unable to parse string as an integer',
'input_value': 'wrong',
}
]
def test_wrong_mode():
with pytest.raises(SchemaError, match='function -> mode\n Input should be one of'):
SchemaValidator({'type': 'function', 'mode': 'foobar', 'schema': 'str'})
def test_function_after_data():
f_kwargs = None
def f(input_value, **kwargs):
nonlocal f_kwargs
f_kwargs = deepcopy(kwargs)
return input_value + ' Changed'
v = SchemaValidator(
{
'type': 'typed-dict',
'fields': {
'field_a': {'schema': {'type': 'int'}},
'field_b': {'schema': {'type': 'function', 'mode': 'after', 'function': f, 'schema': {'type': 'str'}}},
},
}
)
assert v.validate_python({'field_a': '123', 'field_b': b'321'}) == {'field_a': 123, 'field_b': '321 Changed'}
assert f_kwargs == {'data': {'field_a': 123}, 'config': None, 'context': None}
def test_function_after_config():
f_kwargs = None
def f(input_value, **kwargs):
nonlocal f_kwargs
f_kwargs = deepcopy(kwargs)
return input_value + ' Changed'
v = SchemaValidator(
{
'type': 'typed-dict',
'fields': {
'test_field': {
'schema': {'type': 'function', 'mode': 'after', 'function': f, 'schema': {'type': 'str'}}
}
},
},
{'config_choose_priority': 2},
)
assert v.validate_python({'test_field': b'321'}) == {'test_field': '321 Changed'}
assert f_kwargs == {'data': {}, 'config': {'config_choose_priority': 2}, 'context': None}
def test_config_no_model():
f_kwargs = None
def f(input_value, **kwargs):
nonlocal f_kwargs
f_kwargs = deepcopy(kwargs)
return input_value + ' Changed'
v = SchemaValidator({'type': 'function', 'mode': 'after', 'function': f, 'schema': {'type': 'str'}})
assert v.validate_python(b'abc') == 'abc Changed'
assert f_kwargs == {'data': None, 'config': None, 'context': None}
def test_function_plain():
def f(input_value, **kwargs):
return input_value * 2
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': f})
assert v.validate_python(1) == 2
assert v.validate_python('x') == 'xx'
def test_plain_with_schema():
with pytest.raises(SchemaError, match='function-plain -> schema\n Extra inputs are not permitted'):
SchemaValidator({'type': 'function', 'mode': 'plain', 'function': lambda x: x, 'schema': 'str'})
def test_validate_assignment():
def f(input_value, **kwargs):
input_value['more'] = 'foobar'
return input_value
v = SchemaValidator(
{
'type': 'function',
'mode': 'after',
'function': f,
'schema': {'type': 'typed-dict', 'fields': {'field_a': {'schema': {'type': 'str'}}}},
}
)
m = {'field_a': 'test', 'more': 'foobar'}
assert v.validate_python({'field_a': 'test'}) == m
assert v.validate_assignment('field_a', b'abc', m) == {'field_a': 'abc', 'more': 'foobar'}
def test_function_wrong_sig():
def f(input_value):
return input_value + ' Changed'
v = SchemaValidator({'type': 'function', 'mode': 'before', 'function': f, 'schema': {'type': 'str'}})
# exception messages differ between python and pypy
if platform.python_implementation() == 'PyPy':
error_message = 'f() got 3 unexpected keyword arguments'
else:
error_message = "f() got an unexpected keyword argument 'data'"
with pytest.raises(TypeError, match=re.escape(error_message)):
v.validate_python('input value')
def test_class_with_validator():
class Foobar:
a: int
def __init__(self, a):
self.a = a
@classmethod
def __validate__(cls, input_value, **kwargs):
return Foobar(input_value * 2)
v = SchemaValidator(
{'type': 'function', 'mode': 'after', 'function': Foobar.__validate__, 'schema': {'type': 'str'}}
)
f = v.validate_python('foo')
assert isinstance(f, Foobar)
assert f.a == 'foofoo'
f = v.validate_python(b'a')
assert isinstance(f, Foobar)
assert f.a == 'aa'
with pytest.raises(ValidationError) as exc_info:
v.validate_python(True)
assert exc_info.value.errors() == [
{'kind': 'str_type', 'loc': [], 'message': 'Input should be a valid string', 'input_value': True}
]
def test_raise_assertion_error():
def f(input_value, **kwargs):
raise AssertionError('foobar')
v = SchemaValidator({'type': 'function', 'mode': 'before', 'function': f, 'schema': 'str'})
with pytest.raises(ValidationError) as exc_info:
v.validate_python('input value')
assert exc_info.value.errors() == [
{
'kind': 'assertion_error',
'loc': [],
'message': 'Assertion failed, foobar',
'input_value': 'input value',
'context': {'error': 'foobar'},
}
]
def test_raise_assertion_error_plain():
def f(input_value, **kwargs):
raise AssertionError
v = SchemaValidator({'type': 'function', 'mode': 'before', 'function': f, 'schema': 'str'})
with pytest.raises(ValidationError) as exc_info:
v.validate_python('input value')
assert exc_info.value.errors() == [
{
'kind': 'assertion_error',
'loc': [],
'message': 'Assertion failed, Unknown error',
'input_value': 'input value',
'context': {'error': 'Unknown error'},
}
]
@pytest.mark.parametrize('base_error', [ValueError, AssertionError])
def test_error_with_error(base_error: Type[Exception]):
class MyError(base_error):
def __str__(self):
raise RuntimeError('internal error')
def f(input_value, **kwargs):
raise MyError()
v = SchemaValidator({'type': 'function', 'mode': 'before', 'function': f, 'schema': 'str'})
with pytest.raises(RuntimeError, match='internal error'):
v.validate_python('input value')
def test_raise_type_error():
def f(input_value, **kwargs):
raise TypeError('foobar')
v = SchemaValidator({'type': 'function', 'mode': 'before', 'function': f, 'schema': 'str'})
with pytest.raises(TypeError, match='^foobar$'):
v.validate_python('input value')
def test_pydantic_value_error():
e = PydanticValueError(
'my_error', 'this is a custom error {missed} {foo} {bar} {spam}', {'foo': 'X', 'bar': 42, 'spam': []}
)
assert e.message() == 'this is a custom error {missed} X 42 []'
assert e.message_template == 'this is a custom error {missed} {foo} {bar} {spam}'
assert e.kind == 'my_error'
assert e.context == {'foo': 'X', 'bar': 42, 'spam': []}
assert str(e) == 'this is a custom error {missed} X 42 []'
assert repr(e) == (
"this is a custom error {missed} X 42 [] [kind=my_error, context={'foo': 'X', 'bar': 42, 'spam': []}]"
)
def test_pydantic_value_error_none():
e = PydanticValueError('my_error', 'this is a custom error {missed}')
assert e.message() == 'this is a custom error {missed}'
assert e.message_template == 'this is a custom error {missed}'
assert e.kind == 'my_error'
assert e.context is None
assert str(e) == 'this is a custom error {missed}'
assert repr(e) == 'this is a custom error {missed} [kind=my_error, context=None]'
def test_pydantic_value_error_usage():
def f(input_value, **kwargs):
raise PydanticValueError('my_error', 'this is a custom error {foo} {bar}', {'foo': 'FOOBAR', 'bar': 42})
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': f})
with pytest.raises(ValidationError) as exc_info:
v.validate_python(42)
assert exc_info.value.errors() == [
{
'kind': 'my_error',
'loc': [],
'message': 'this is a custom error FOOBAR 42',
'input_value': 42,
'context': {'foo': 'FOOBAR', 'bar': 42},
}
]
def test_pydantic_value_error_invalid_dict():
def f(input_value, **kwargs):
raise PydanticValueError('my_error', 'this is a custom error {foo}', {(): 'foobar'})
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': f})
with pytest.raises(ValidationError) as exc_info:
v.validate_python(42)
assert str(exc_info.value) == (
'1 validation error for function-plain\n'
" (error rendering message: TypeError: 'tuple' object cannot be converted to 'PyString') "
'[kind=my_error, input_value=42, input_type=int]'
)
with pytest.raises(TypeError, match="'tuple' object cannot be converted to 'PyString'"):
exc_info.value.errors()
def test_pydantic_value_error_invalid_type():
def f(input_value, **kwargs):
raise PydanticValueError('my_error', 'this is a custom error {foo}', [('foo', 123)])
v = SchemaValidator({'type': 'function', 'mode': 'plain', 'function': f})
with pytest.raises(TypeError, match="argument 'context': 'list' object cannot be converted to 'PyDict'"):
v.validate_python(42)