Skip to content

Commit 6d68041

Browse files
committed
fix tests
1 parent a774065 commit 6d68041

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

tests/test_errors.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ def f(input_value, info):
257257
('no_such_attribute', "Object has no attribute 'wrong_name'", {'attribute': 'wrong_name'}),
258258
('json_invalid', 'Invalid JSON: foobar', {'error': 'foobar'}),
259259
('json_type', 'JSON input should be string, bytes or bytearray', None),
260+
(
261+
'cannot_validate_from_json',
262+
'Cannot check `isinstance` when validating from json, use a JsonOrPython validator instead',
263+
{'method_name': 'isinstance'},
264+
),
260265
('recursion_loop', 'Recursion error - cyclic reference detected', None),
261266
('model_type', 'Input should be a valid dictionary or instance of Foobar', {'class_name': 'Foobar'}),
262267
('model_attributes_type', 'Input should be a valid dictionary or object to extract fields from', None),
@@ -506,10 +511,10 @@ def test_all_errors():
506511
'example_context': None,
507512
},
508513
{
509-
'type': 'recursion_loop',
510-
'message_template_python': 'Recursion error - cyclic reference detected',
511-
'example_message_python': 'Recursion error - cyclic reference detected',
512-
'example_context': None,
514+
'type': 'cannot_validate_from_json',
515+
'message_template_python': 'Cannot check `{method_name}` when validating from json, use a JsonOrPython validator instead',
516+
'example_message_python': 'Cannot check `` when validating from json, use a JsonOrPython validator instead',
517+
'example_context': {'method_name': ''},
513518
},
514519
]
515520

tests/validators/test_is_instance.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ class Spam:
1919

2020
def test_validate_json() -> None:
2121
v = SchemaValidator({'type': 'is-instance', 'cls': Foo})
22-
with pytest.raises(NotImplementedError, match='use a JsonOrPython validator instead'):
22+
with pytest.raises(ValidationError) as exc_info:
2323
v.validate_json('"foo"')
24+
assert exc_info.value.errors()[0]['type'] == 'cannot_validate_from_json'
2425

2526

2627
def test_is_instance():
@@ -175,11 +176,9 @@ def test_is_instance_json_type_before_validator():
175176
schema = core_schema.is_instance_schema(type)
176177
v = SchemaValidator(schema)
177178

178-
with pytest.raises(
179-
NotImplementedError,
180-
match='Cannot check isinstance when validating from json, use a JsonOrPython validator instead.',
181-
):
179+
with pytest.raises(ValidationError) as exc_info:
182180
v.validate_json('null')
181+
assert exc_info.value.errors()[0]['type'] == 'cannot_validate_from_json'
183182

184183
# now wrap in a before validator
185184
def set_type_to_int(input: None) -> type:

tests/validators/test_is_subclass.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,10 @@ def test_custom_repr():
7777
'ctx': {'class': 'Spam'},
7878
}
7979
]
80+
81+
82+
def test_is_subclass_json() -> None:
83+
v = SchemaValidator(core_schema.is_subclass_schema(Foo))
84+
with pytest.raises(ValidationError) as exc_info:
85+
v.validate_json("'Foo'")
86+
assert exc_info.value.errors()[0]['type'] == 'cannot_validate_from_json'

0 commit comments

Comments
 (0)