MCPcopy
hub / github.com/pydantic/pydantic / test_on_validate_python_on_success

Function test_on_validate_python_on_success

tests/test_plugins.py:126–164  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

124
125
126def test_on_validate_python_on_success() -> None:
127 class CustomOnValidatePython(ValidatePythonHandlerProtocol):
128 def on_enter(
129 self,
130 input: Any,
131 *,
132 strict: bool | None = None,
133 extra: ExtraValues | None = None,
134 from_attributes: bool | None = None,
135 context: Any | None = None,
136 self_instance: Any | None = None,
137 by_alias: bool | None = None,
138 by_name: bool | None = None,
139 ) -> None:
140 assert input == {'a': 1}
141 assert strict is None
142 assert extra is None
143 assert context is None
144 assert self_instance is None
145
146 def on_success(self, result: Any) -> None:
147 assert isinstance(result, Model)
148
149 class Plugin:
150 def new_schema_validator(self, schema, schema_type, schema_type_path, schema_kind, config, plugin_settings):
151 assert config == {'title': 'Model'}
152 assert plugin_settings == {'observe': 'all'}
153 assert schema_type.__name__ == 'Model'
154 assert schema_kind == 'BaseModel'
155 return CustomOnValidatePython(), None, None
156
157 plugin = Plugin()
158 with install_plugin(plugin):
159
160 class Model(BaseModel, plugin_settings={'observe': 'all'}):
161 a: int
162
163 assert Model.model_validate({'a': 1}).model_dump() == {'a': 1}
164 assert Model.model_validate_json('{"a": 1}').model_dump() == {'a': 1}
165
166
167def test_on_validate_python_on_error() -> None:

Callers

nothing calls this directly

Calls 5

install_pluginFunction · 0.85
model_validateMethod · 0.80
model_validate_jsonMethod · 0.80
PluginClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected