|
| 1 | +from typing import Any, Dict, List, Optional |
| 2 | +from unittest import TestCase |
| 3 | +from unittest.mock import ANY |
| 4 | + |
| 5 | +import pytest |
| 6 | +from typing_extensions import LiteralString, Self, override |
| 7 | + |
| 8 | +from pydantic_core import ErrorDetails, InitErrorDetails, PydanticCustomError, ValidationError |
| 9 | + |
| 10 | + |
| 11 | +def test_validation_error_subclassable(): |
| 12 | + """Assert subclassable and inheritance hierarchy as expected""" |
| 13 | + |
| 14 | + class CustomValidationError(ValidationError): |
| 15 | + pass |
| 16 | + |
| 17 | + with pytest.raises(ValidationError) as exception_info: |
| 18 | + raise CustomValidationError.from_exception_data( |
| 19 | + 'My CustomError', |
| 20 | + [ |
| 21 | + InitErrorDetails( |
| 22 | + type='value_error', |
| 23 | + loc=('myField',), |
| 24 | + msg='This is my custom error.', |
| 25 | + input='something invalid', |
| 26 | + ctx={ |
| 27 | + 'myField': 'something invalid', |
| 28 | + 'error': "'something invalid' is not a valid value for 'myField'", |
| 29 | + }, |
| 30 | + ) |
| 31 | + ], |
| 32 | + ) |
| 33 | + assert isinstance(exception_info.value, CustomValidationError) |
| 34 | + |
| 35 | + |
| 36 | +def test_validation_error_loc_overrides(): |
| 37 | + """Override methods in rust pyclass and assert change in behavior: ValidationError.errors""" |
| 38 | + |
| 39 | + class CustomLocOverridesError(ValidationError): |
| 40 | + """Unnests some errors""" |
| 41 | + |
| 42 | + @override |
| 43 | + def errors( |
| 44 | + self, *, include_url: bool = True, include_context: bool = True, include_input: bool = True |
| 45 | + ) -> List[ErrorDetails]: |
| 46 | + errors = super().errors( |
| 47 | + include_url=include_url, include_context=include_context, include_input=include_input |
| 48 | + ) |
| 49 | + return [{**error, 'loc': error['loc'][1:]} for error in errors] |
| 50 | + |
| 51 | + with pytest.raises(CustomLocOverridesError) as exception_info: |
| 52 | + raise CustomLocOverridesError.from_exception_data( |
| 53 | + 'My CustomError', |
| 54 | + [ |
| 55 | + InitErrorDetails( |
| 56 | + type='value_error', |
| 57 | + loc=( |
| 58 | + 'hide_this', |
| 59 | + 'myField', |
| 60 | + ), |
| 61 | + msg='This is my custom error.', |
| 62 | + input='something invalid', |
| 63 | + ctx={ |
| 64 | + 'myField': 'something invalid', |
| 65 | + 'error': "'something invalid' is not a valid value for 'myField'", |
| 66 | + }, |
| 67 | + ), |
| 68 | + InitErrorDetails( |
| 69 | + type='value_error', |
| 70 | + loc=( |
| 71 | + 'hide_this', |
| 72 | + 'myFieldToo', |
| 73 | + ), |
| 74 | + msg='This is my custom error.', |
| 75 | + input='something invalid', |
| 76 | + ctx={ |
| 77 | + 'myFieldToo': 'something invalid', |
| 78 | + 'error': "'something invalid' is not a valid value for 'myFieldToo'", |
| 79 | + }, |
| 80 | + ), |
| 81 | + ], |
| 82 | + ) |
| 83 | + |
| 84 | + TestCase().assertCountEqual( |
| 85 | + exception_info.value.errors(), |
| 86 | + [ |
| 87 | + { |
| 88 | + 'type': 'value_error', |
| 89 | + 'loc': ('myField',), |
| 90 | + 'msg': "Value error, 'something invalid' is not a valid value for 'myField'", |
| 91 | + 'input': 'something invalid', |
| 92 | + 'ctx': { |
| 93 | + 'error': "'something invalid' is not a valid value for 'myField'", |
| 94 | + 'myField': 'something invalid', |
| 95 | + }, |
| 96 | + 'url': ANY, |
| 97 | + }, |
| 98 | + { |
| 99 | + 'type': 'value_error', |
| 100 | + 'loc': ('myFieldToo',), |
| 101 | + 'msg': "Value error, 'something invalid' is not a valid value for 'myFieldToo'", |
| 102 | + 'input': 'something invalid', |
| 103 | + 'ctx': { |
| 104 | + 'error': "'something invalid' is not a valid value for 'myFieldToo'", |
| 105 | + 'myFieldToo': 'something invalid', |
| 106 | + }, |
| 107 | + 'url': ANY, |
| 108 | + }, |
| 109 | + ], |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def test_custom_pydantic_error_subclassable(): |
| 114 | + """Assert subclassable and inheritance hierarchy as expected""" |
| 115 | + |
| 116 | + class MyCustomError(PydanticCustomError): |
| 117 | + pass |
| 118 | + |
| 119 | + with pytest.raises(PydanticCustomError) as exception_info: |
| 120 | + raise MyCustomError( |
| 121 | + 'not_my_custom_thing', |
| 122 | + "value is not compatible with my custom field, got '{wrong_value}'", |
| 123 | + {'wrong_value': 'non compatible value'}, |
| 124 | + ) |
| 125 | + assert isinstance(exception_info.value, MyCustomError) |
| 126 | + |
| 127 | + |
| 128 | +def test_custom_pydantic_error_overrides(): |
| 129 | + """Override methods in rust pyclass and assert change in behavior: PydanticCustomError.__new__""" |
| 130 | + |
| 131 | + class CustomErrorWithCustomTemplate(PydanticCustomError): |
| 132 | + @override |
| 133 | + def __new__( |
| 134 | + cls, error_type: LiteralString, my_custom_setting: str, context: Optional[Dict[str, Any]] = None |
| 135 | + ) -> Self: |
| 136 | + message_template = ( |
| 137 | + "'{my_custom_value}' setting requires a specific my custom field value, got '{wrong_value}'" |
| 138 | + ) |
| 139 | + context = {**context, 'my_custom_value': my_custom_setting} |
| 140 | + return super().__new__(cls, error_type, message_template, context) |
| 141 | + |
| 142 | + with pytest.raises(CustomErrorWithCustomTemplate) as exception_info: |
| 143 | + raise CustomErrorWithCustomTemplate( |
| 144 | + 'not_my_custom_thing', 'my_setting', {'wrong_value': 'non compatible value'} |
| 145 | + ) |
| 146 | + |
| 147 | + assert ( |
| 148 | + exception_info.value.message() |
| 149 | + == "'my_setting' setting requires a specific my custom field value, got 'non compatible value'" |
| 150 | + ) |
0 commit comments