Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andresliszt committed Aug 1, 2023
1 parent d53b4bc commit 0501342
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions tests/test_serialize.py
Expand Up @@ -1009,17 +1009,26 @@ def test_annotated_computed_field_custom_serializer():
class Model(BaseModel):
x: int

@computed_field
@property
def two_x(self) -> Annotated[int, PlainSerializer(lambda v: f'The double of x is {v}', return_type=str)]:
return self.x * 2

@computed_field(
return_type=Annotated[str, PlainSerializer(lambda v: f'The double of x is {v}', return_type=str)]
return_type=Annotated[int, PlainSerializer(lambda v: f'The quadruple of x is {v}', return_type=str)]
)
@property
def two_x(self) -> int:
return self.x * 2
def four_x(self) -> int:
return self.two_x * 2

m = Model(x=1)

assert m.model_dump() == {'two_x': 'The double of x is 2', 'x': 1}
assert json.loads(m.model_dump_json()) == {'two_x': 'The double of x is 2', 'x': 1}
assert m.model_dump() == {'x': 1, 'two_x': 'The double of x is 2', 'four_x': 'The quadruple of x is 4'}
assert json.loads(m.model_dump_json()) == {
'x': 1,
'two_x': 'The double of x is 2',
'four_x': 'The quadruple of x is 4',
}


def test_computed_field_custom_serializer_bad_signature():
Expand Down

0 comments on commit 0501342

Please sign in to comment.