Skip to content

Commit

Permalink
Add test to compute on TypedDict model
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhly committed Oct 25, 2023
1 parent d293215 commit 147e917
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/serializers/test_typed_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,23 @@ def ser_columns(v: dict, serializer: core_schema.SerializerFunctionWrapHandler,
value = {'one': 1, 'two': 2, 'three': 3}
assert s.to_python(value) == {'one': 1, 'two': 2, 'three': 3, 'columns': ['ONE', 'TWO', 'THREE']}
assert s.to_json(value) == b'{"one":1,"two":2,"three":3,"columns":["ONE","TWO","THREE"]}'


def test_computed_fields_with_typed_dict_model():
class Model(TypedDict):
x: int

def ser_y(v: Any) -> str:
return f'{v["x"]}.00'

s = SchemaSerializer(
core_schema.typed_dict_schema(
{'x': core_schema.typed_dict_field(core_schema.int_schema())},
computed_fields=[
core_schema.computed_field(
'y', core_schema.str_schema(serialization=core_schema.plain_serializer_function_ser_schema(ser_y))
)
],
)
)
assert s.to_python(Model(x=1000)) == {'x': 1000, 'y': '1000.00'}

0 comments on commit 147e917

Please sign in to comment.