|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | | -from pydantic_core import PydanticCustomError, PydanticErrorKind, SchemaError, SchemaValidator, ValidationError |
| 8 | +from pydantic_core import ( |
| 9 | + PydanticCustomError, |
| 10 | + PydanticErrorKind, |
| 11 | + PydanticOmit, |
| 12 | + SchemaError, |
| 13 | + SchemaValidator, |
| 14 | + ValidationError, |
| 15 | +) |
9 | 16 |
|
10 | | -from ..conftest import plain_repr |
| 17 | +from ..conftest import PyAndJson, plain_repr |
11 | 18 |
|
12 | 19 |
|
13 | 20 | def test_function_before(): |
@@ -635,3 +642,33 @@ def test_error_kind(kind, message, context): |
635 | 642 | assert e.message() == message |
636 | 643 | assert e.kind == kind |
637 | 644 | assert e.context == context |
| 645 | + |
| 646 | + |
| 647 | +def test_pydantic_value_error_plain(py_and_json: PyAndJson): |
| 648 | + def f(input_value, **kwargs): |
| 649 | + raise PydanticCustomError |
| 650 | + |
| 651 | + v = py_and_json({'type': 'function', 'mode': 'plain', 'function': f}) |
| 652 | + with pytest.raises(TypeError, match='missing 2 required positional arguments'): |
| 653 | + v.validate_test('4') |
| 654 | + |
| 655 | + |
| 656 | +@pytest.mark.parametrize('exception', [PydanticOmit(), PydanticOmit]) |
| 657 | +def test_list_omit_exception(py_and_json: PyAndJson, exception): |
| 658 | + def f(input_value, **kwargs): |
| 659 | + if input_value % 2 == 0: |
| 660 | + raise exception |
| 661 | + return input_value |
| 662 | + |
| 663 | + v = py_and_json( |
| 664 | + { |
| 665 | + 'type': 'list', |
| 666 | + 'items_schema': {'type': 'function', 'schema': {'type': 'int'}, 'mode': 'after', 'function': f}, |
| 667 | + } |
| 668 | + ) |
| 669 | + assert v.validate_test([1, 2, '3', '4']) == [1, 3] |
| 670 | + |
| 671 | + |
| 672 | +def test_omit_exc_repr(): |
| 673 | + assert repr(PydanticOmit()) == 'PydanticOmit()' |
| 674 | + assert str(PydanticOmit()) == 'PydanticOmit()' |
0 commit comments