Skip to content

Commit 1ab9182

Browse files
authored
tests to confirm "loc" on errors with an alias (#310)
* tests to confirm "loc" on errors with an alias * fix conflicting test
1 parent 9693919 commit 1ab9182

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/validators/test_typed_dict.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,36 @@ def test_alias_build_error(alias_schema, error):
660660
SchemaValidator({'type': 'typed-dict', 'fields': {'field_a': {'schema': {'type': 'int'}, **alias_schema}}})
661661

662662

663+
def test_alias_error_loc():
664+
v = SchemaValidator(
665+
{'type': 'typed-dict', 'fields': {'field_a': {'schema': {'type': 'int'}, 'alias': [['bar'], ['foo']]}}}
666+
)
667+
assert v.validate_python({'foo': 42}) == {'field_a': 42}
668+
assert v.validate_python({'bar': 42}) == {'field_a': 42}
669+
with pytest.raises(ValidationError) as exc_info:
670+
v.validate_python({'foo': 'not_int'})
671+
# insert_assert(exc_info.value.errors())
672+
assert exc_info.value.errors() == [
673+
{
674+
'type': 'int_parsing',
675+
'loc': ('field_a',),
676+
'msg': 'Input should be a valid integer, unable to parse string as an integer',
677+
'input': 'not_int',
678+
}
679+
]
680+
with pytest.raises(ValidationError) as exc_info:
681+
v.validate_python({'bar': 'not_int'})
682+
# insert_assert(exc_info.value.errors())
683+
assert exc_info.value.errors() == [
684+
{
685+
'type': 'int_parsing',
686+
'loc': ('field_a',),
687+
'msg': 'Input should be a valid integer, unable to parse string as an integer',
688+
'input': 'not_int',
689+
}
690+
]
691+
692+
663693
def test_empty_model():
664694
v = SchemaValidator({'type': 'typed-dict', 'fields': {}, 'return_fields_set': True})
665695
assert v.validate_python({}) == ({}, set())

0 commit comments

Comments
 (0)