Skip to content

add tests for validation context #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@ def __init__(self, schema, validator_type: 'Literal["json", "python"] | None' =
self.validator = SchemaValidator(schema)
self.validator_type = validator_type

def validate_python(self, py_input, strict: 'bool | None' = None):
return self.validator.validate_python(py_input, strict)
def validate_python(self, py_input, strict: 'bool | None' = None, context: Any = None):
return self.validator.validate_python(py_input, strict, context)

def validate_test(self, py_input, strict: 'bool | None' = None):
def validate_test(self, py_input, strict: 'bool | None' = None, context: Any = None):
if self.validator_type == 'json':
return self.validator.validate_json(json.dumps(py_input), strict)
elif self.validator_type == 'python':
return self.validator.validate_python(py_input, strict)
return self.validator.validate_json(json.dumps(py_input), strict, context)
else:
assert self.validator_type == 'python', self.validator_type
return self.validator.validate_python(py_input, strict, context)

def isinstance_test(self, py_input, strict: 'bool | None' = None):
def isinstance_test(self, py_input, strict: 'bool | None' = None, context: Any = None):
if self.validator_type == 'json':
return self.validator.isinstance_json(json.dumps(py_input), strict)
elif self.validator_type == 'python':
return self.validator.isinstance_python(py_input, strict)
return self.validator.isinstance_json(json.dumps(py_input), strict, context)
else:
assert self.validator_type == 'python', self.validator_type
return self.validator.isinstance_python(py_input, strict, context)


PyAndJson = Type[PyAndJsonValidator]
Expand Down
91 changes: 91 additions & 0 deletions tests/test_validation_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import pytest

from pydantic_core import ValidationError

from .conftest import PyAndJson


def test_after(py_and_json: PyAndJson):
def f(input_value, *, context, **kwargs):
return input_value + f'| context: {context}'

v = py_and_json({'type': 'function', 'mode': 'after', 'function': f, 'schema': 'str'})

assert v.validate_test('foobar') == 'foobar| context: None'
assert v.validate_test('foobar', None, {1: 10}) == 'foobar| context: {1: 10}'
assert v.validate_test('foobar', None, 'frogspawn') == 'foobar| context: frogspawn'


def test_mutable_context(py_and_json: PyAndJson):
def f(input_value, *, context, **kwargs):
context['foo'] = input_value
return input_value

v = py_and_json({'type': 'function', 'mode': 'before', 'function': f, 'schema': 'str'})
mutable_context = {}
assert v.validate_test('foobar', None, mutable_context) == 'foobar'
assert mutable_context == {'foo': 'foobar'}


def test_typed_dict(py_and_json: PyAndJson):
def f1(input_value, *, context, **kwargs):
context['f1'] = input_value
return input_value + f'| context: {context}'

def f2(input_value, *, context, **kwargs):
context['f2'] = input_value
return input_value + f'| context: {context}'

v = py_and_json(
{
'type': 'typed-dict',
'fields': {
'f1': {'schema': {'type': 'function', 'mode': 'plain', 'function': f1}},
'f2': {'schema': {'type': 'function', 'mode': 'plain', 'function': f2}},
},
}
)

assert v.validate_test({'f1': '1', 'f2': '2'}, None, {'x': 'y'}) == {
'f1': "1| context: {'x': 'y', 'f1': '1'}",
'f2': "2| context: {'x': 'y', 'f1': '1', 'f2': '2'}",
}


def test_wrap(py_and_json: PyAndJson):
def f(input_value, *, validator, context, **kwargs):
return validator(input_value) + f'| context: {context}'

v = py_and_json({'type': 'function', 'mode': 'wrap', 'function': f, 'schema': 'str'})

assert v.validate_test('foobar') == 'foobar| context: None'
assert v.validate_test('foobar', None, {1: 10}) == 'foobar| context: {1: 10}'
assert v.validate_test('foobar', None, 'frogspawn') == 'foobar| context: frogspawn'


def test_isinstance(py_and_json: PyAndJson):
def f(input_value, *, validator, context, **kwargs):
if 'error' in context:
raise ValueError('wrong')
return validator(input_value)

v = py_and_json({'type': 'function', 'mode': 'wrap', 'function': f, 'schema': 'str'})

assert v.validate_python('foobar', None, {}) == 'foobar'

# internal error!, use generic bit of error message to match both cpython and pypy
with pytest.raises(TypeError, match='is not iterable'):
v.validate_test('foobar')

with pytest.raises(TypeError, match='is not iterable'):
v.isinstance_test('foobar')

with pytest.raises(ValidationError, match=r'Value error, wrong \[kind=value_error,'):
v.validate_test('foobar', None, {'error'})

assert v.isinstance_test('foobar', None, {}) is True

with pytest.raises(TypeError, match='is not iterable'):
v.isinstance_test('foobar')

assert v.isinstance_test('foobar', None, {'error'}) is False