|
1 | 1 | import re |
2 | 2 | from copy import deepcopy |
3 | | -from typing import Any, List |
| 3 | +from typing import Any, Callable, Dict, List, Set, Tuple |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
@@ -88,33 +88,121 @@ def __setattr__(self, key, value): |
88 | 88 | assert setattr_calls == [] |
89 | 89 |
|
90 | 90 |
|
91 | | -def test_model_class_root_validator(): |
| 91 | +def test_model_class_root_validator_wrap(): |
92 | 92 | class MyModel: |
93 | | - pass |
| 93 | + def __init__(self, **kwargs: Any) -> None: |
| 94 | + self.__dict__.update(kwargs) |
94 | 95 |
|
95 | | - def f(input_value, validator, info): |
| 96 | + def f( |
| 97 | + input_value: Dict[str, Any], |
| 98 | + validator: Callable[[Dict[str, Any]], Dict[str, Any]], |
| 99 | + info: core_schema.ValidationInfo, |
| 100 | + ): |
| 101 | + assert input_value['field_a'] == 123 |
96 | 102 | output = validator(input_value) |
97 | | - return str(output) |
| 103 | + return output |
98 | 104 |
|
99 | | - v = SchemaValidator( |
| 105 | + schema = core_schema.model_schema( |
| 106 | + MyModel, |
| 107 | + core_schema.general_wrap_validator_function( |
| 108 | + f, |
| 109 | + core_schema.typed_dict_schema( |
| 110 | + {'field_a': core_schema.typed_dict_field(core_schema.int_schema())}, return_fields_set=True |
| 111 | + ), |
| 112 | + ), |
| 113 | + ) |
| 114 | + |
| 115 | + v = SchemaValidator(schema) |
| 116 | + m = v.validate_python({'field_a': 123}) |
| 117 | + assert m.field_a == 123 |
| 118 | + |
| 119 | + with pytest.raises(ValidationError) as e: |
| 120 | + v.validate_python({'field_a': 456}) |
| 121 | + |
| 122 | + assert e.value.errors() == [ |
100 | 123 | { |
101 | | - 'type': 'function-wrap', |
102 | | - 'function': {'type': 'general', 'function': f}, |
103 | | - 'schema': { |
104 | | - 'type': 'model', |
105 | | - 'cls': MyModel, |
106 | | - 'schema': { |
107 | | - 'type': 'typed-dict', |
108 | | - 'return_fields_set': True, |
109 | | - 'fields': {'field_a': {'type': 'typed-dict-field', 'schema': {'type': 'str'}}}, |
110 | | - }, |
111 | | - }, |
| 124 | + 'type': 'assertion_error', |
| 125 | + 'loc': (), |
| 126 | + 'msg': 'Assertion failed, assert 456 == 123', |
| 127 | + 'input': {'field_a': 456}, |
| 128 | + 'ctx': {'error': 'assert 456 == 123'}, |
| 129 | + } |
| 130 | + ] |
| 131 | + |
| 132 | + |
| 133 | +def test_model_class_root_validator_before(): |
| 134 | + class MyModel: |
| 135 | + def __init__(self, **kwargs: Any) -> None: |
| 136 | + self.__dict__.update(kwargs) |
| 137 | + |
| 138 | + def f(input_value: Dict[str, Any], info: core_schema.ValidationInfo): |
| 139 | + assert input_value['field_a'] == 123 |
| 140 | + return input_value |
| 141 | + |
| 142 | + schema = core_schema.model_schema( |
| 143 | + MyModel, |
| 144 | + core_schema.general_before_validator_function( |
| 145 | + f, |
| 146 | + core_schema.typed_dict_schema( |
| 147 | + {'field_a': core_schema.typed_dict_field(core_schema.int_schema())}, return_fields_set=True |
| 148 | + ), |
| 149 | + ), |
| 150 | + ) |
| 151 | + |
| 152 | + v = SchemaValidator(schema) |
| 153 | + m = v.validate_python({'field_a': 123}) |
| 154 | + assert m.field_a == 123 |
| 155 | + |
| 156 | + with pytest.raises(ValidationError) as e: |
| 157 | + v.validate_python({'field_a': 456}) |
| 158 | + |
| 159 | + assert e.value.errors() == [ |
| 160 | + { |
| 161 | + 'type': 'assertion_error', |
| 162 | + 'loc': (), |
| 163 | + 'msg': 'Assertion failed, assert 456 == 123', |
| 164 | + 'input': {'field_a': 456}, |
| 165 | + 'ctx': {'error': 'assert 456 == 123'}, |
112 | 166 | } |
| 167 | + ] |
| 168 | + |
| 169 | + |
| 170 | +def test_model_class_root_validator_after(): |
| 171 | + class MyModel: |
| 172 | + def __init__(self, **kwargs: Any) -> None: |
| 173 | + self.__dict__.update(kwargs) |
| 174 | + |
| 175 | + def f(input_value_and_fields_set: Tuple[Dict[str, Any], Set[str]], info: core_schema.ValidationInfo): |
| 176 | + input_value, _ = input_value_and_fields_set |
| 177 | + assert input_value['field_a'] == 123 |
| 178 | + return input_value_and_fields_set |
| 179 | + |
| 180 | + schema = core_schema.model_schema( |
| 181 | + MyModel, |
| 182 | + core_schema.general_after_validator_function( |
| 183 | + f, |
| 184 | + core_schema.typed_dict_schema( |
| 185 | + {'field_a': core_schema.typed_dict_field(core_schema.int_schema())}, return_fields_set=True |
| 186 | + ), |
| 187 | + ), |
113 | 188 | ) |
114 | | - assert 'expect_fields_set:true' in plain_repr(v) |
115 | | - m = v.validate_python({'field_a': 'test'}) |
116 | | - assert isinstance(m, str) |
117 | | - assert 'test_model_class_root_validator.<locals>.MyModel' in m |
| 189 | + |
| 190 | + v = SchemaValidator(schema) |
| 191 | + m = v.validate_python({'field_a': 123}) |
| 192 | + assert m.field_a == 123 |
| 193 | + |
| 194 | + with pytest.raises(ValidationError) as e: |
| 195 | + v.validate_python({'field_a': 456}) |
| 196 | + |
| 197 | + assert e.value.errors() == [ |
| 198 | + { |
| 199 | + 'type': 'assertion_error', |
| 200 | + 'loc': (), |
| 201 | + 'msg': 'Assertion failed, assert 456 == 123', |
| 202 | + 'input': {'field_a': 456}, |
| 203 | + 'ctx': {'error': 'assert 456 == 123'}, |
| 204 | + } |
| 205 | + ] |
118 | 206 |
|
119 | 207 |
|
120 | 208 | @pytest.mark.parametrize('mode', ['before', 'after', 'wrap']) |
|
0 commit comments