Skip to content

Commit

Permalink
serialization test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Apr 13, 2023
1 parent 633227f commit 493bbb6
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion tests/serializers/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

import pytest

from pydantic_core import PydanticOmit, PydanticSerializationError, SchemaError, SchemaSerializer, core_schema
from pydantic_core import (
PydanticOmit,
PydanticSerializationError,
PydanticSerializationUnexpectedValue,
SchemaError,
SchemaSerializer,
core_schema,
)


def repr_function(value, _info):
Expand Down Expand Up @@ -566,3 +573,42 @@ def to_path(value, handler, _info):
assert s.to_python('foobar') == Path('foobar.new')
assert s.to_python('foobar', mode='json') == 'foobar.new'
assert s.to_json('foobar') == b'"foobar.new"'


def test_unexpected_value():
def return_xxx(_value):
return 'xxx'

s = SchemaSerializer(
core_schema.any_schema(
serialization=core_schema.plain_serializer_function_ser_schema(return_xxx, json_return_type='int')
)
)
assert s.to_python('foo') == 'xxx'

m = "Error serializing to JSON: TypeError: 'str' object cannot be interpreted as an integer"
with pytest.raises(PydanticSerializationError, match=m):
s.to_json('foo')


def test_raise_unexpected():
def raise_unexpected(_value):
raise PydanticSerializationUnexpectedValue('unexpected')

s = SchemaSerializer(
core_schema.any_schema(serialization=core_schema.plain_serializer_function_ser_schema(raise_unexpected))
)
with pytest.warns(UserWarning, match=r'PydanticSerializationUnexpectedValue\(unexpected\)'):
assert s.to_python('foo') == 'foo'

with pytest.warns(UserWarning, match=r'PydanticSerializationUnexpectedValue\(unexpected\)'):
assert s.to_json('foo') == b'"foo"'


def test_pydantic_serialization_unexpected_value():
v = PydanticSerializationUnexpectedValue('abc')
assert str(v) == 'abc'
assert repr(v) == 'PydanticSerializationUnexpectedValue(abc)'
v = PydanticSerializationUnexpectedValue()
assert str(v) == 'Unexpected Value'
assert repr(v) == 'PydanticSerializationUnexpectedValue(Unexpected Value)'

0 comments on commit 493bbb6

Please sign in to comment.