Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sydney-runkle committed Sep 16, 2024
1 parent ff26f78 commit d24aad5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 1 addition & 4 deletions tests/serializers/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from pydantic_core import (
PydanticSerializationError,
PydanticSerializationUnexpectedValue,
SchemaSerializer,
SchemaValidator,
core_schema,
Expand Down Expand Up @@ -1150,8 +1149,6 @@ class BModel(BasicModel): ...
)
)

with pytest.raises(
PydanticSerializationUnexpectedValue, match='Expected 2 fields but got 1 for type `.*AModel` with value `.*`.+'
):
with pytest.warns(UserWarning, match='Expected 2 fields but got 1 for type `.*AModel` with value `.*`.+'):
value = BasicModel(root=AModel(type='a'))
s.to_python(value)
16 changes: 12 additions & 4 deletions tests/serializers/test_union.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dataclasses
import json
import re
import uuid
import warnings
from decimal import Decimal
from typing import Any, ClassVar, Union

Expand Down Expand Up @@ -32,9 +32,17 @@ def test_union_bool_int(input_value, expected_value, bool_case_label, int_case_l

def test_union_error():
s = SchemaSerializer(core_schema.union_schema([core_schema.bool_schema(), core_schema.int_schema()]))
msg = "Expected `Union[bool, int]` but got `str` with value `'a string'` - serialized value may not be as expected"
with pytest.warns(UserWarning, match=re.escape(msg)):
assert s.to_python('a string') == 'a string'

messages = [
"Expected `bool` but got `str` with value `'a string'` - serialized value may not be as expected",
"Expected `int` but got `str` with value `'a string'` - serialized value may not be as expected",
]

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
s.to_python('a string')
for m in messages:
assert m in str(w[0].message)


class ModelA:
Expand Down

0 comments on commit d24aad5

Please sign in to comment.