MCPcopy
hub / github.com/pydantic/pydantic / test_model_encoding

Function test_model_encoding

tests/test_json.py:123–137  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

121
122
123def test_model_encoding():
124 class ModelA(BaseModel):
125 x: int
126 y: str
127
128 class Model(BaseModel):
129 a: float
130 b: bytes
131 c: Decimal
132 d: ModelA
133
134 m = Model(a=10.2, b='foobar', c='10.2', d={'x': 123, 'y': '123'})
135 assert m.model_dump() == {'a': 10.2, 'b': b'foobar', 'c': Decimal('10.2'), 'd': {'x': 123, 'y': '123'}}
136 assert m.model_dump_json() == '{"a":10.2,"b":"foobar","c":"10.2","d":{"x":123,"y":"123"}}'
137 assert m.model_dump_json(exclude={'b'}) == '{"a":10.2,"c":"10.2","d":{"x":123,"y":"123"}}'
138
139
140def test_subclass_encoding():

Callers

nothing calls this directly

Calls 3

model_dump_jsonMethod · 0.80
ModelClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected