Skip to content

Commit 8882de2

Browse files
committed
Update tests
1 parent 98ed1c9 commit 8882de2

18 files changed

+189
-83
lines changed

tests/serializers/test_any.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test_any_with_date_serializer():
158158
assert s.to_python(b'bang', mode='json') == 'bang'
159159

160160
assert [w.message.args[0] for w in warning_info.list] == [
161-
'Pydantic serializer warnings:\n Expected `date` but got `bytes` - serialized value may not be as expected'
161+
"Pydantic serializer warnings:\n Expected `date` but got `bytes` with value `b'bang'` - serialized value may not be as expected"
162162
]
163163

164164

@@ -172,7 +172,7 @@ def test_any_with_timedelta_serializer():
172172
assert s.to_python(b'bang', mode='json') == 'bang'
173173

174174
assert [w.message.args[0] for w in warning_info.list] == [
175-
'Pydantic serializer warnings:\n Expected `timedelta` but got `bytes` - '
175+
"Pydantic serializer warnings:\n Expected `timedelta` but got `bytes` with value `b'bang'` - "
176176
'serialized value may not be as expected'
177177
]
178178

tests/serializers/test_bytes.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,21 @@ def test_bytes_dict_key():
4646

4747
def test_bytes_fallback():
4848
s = SchemaSerializer(core_schema.bytes_schema())
49-
with pytest.warns(UserWarning, match='Expected `bytes` but got `int` - serialized value may not be as expected'):
49+
with pytest.warns(
50+
UserWarning, match='Expected `bytes` but got `int` with value `123` - serialized value may not be as expected'
51+
):
5052
assert s.to_python(123) == 123
51-
with pytest.warns(UserWarning, match='Expected `bytes` but got `int` - serialized value may not be as expected'):
53+
with pytest.warns(
54+
UserWarning, match='Expected `bytes` but got `int` with value `123` - serialized value may not be as expected'
55+
):
5256
assert s.to_python(123, mode='json') == 123
53-
with pytest.warns(UserWarning, match='Expected `bytes` but got `int` - serialized value may not be as expected'):
57+
with pytest.warns(
58+
UserWarning, match='Expected `bytes` but got `int` with value `123` - serialized value may not be as expected'
59+
):
5460
assert s.to_json(123) == b'123'
55-
with pytest.warns(UserWarning, match='Expected `bytes` but got `str` - serialized value may not be as expected'):
61+
with pytest.warns(
62+
UserWarning, match="Expected `bytes` but got `str` with value `'foo'` - serialized value may not be as expected"
63+
):
5664
assert s.to_json('foo') == b'"foo"'
5765

5866

tests/serializers/test_datetime.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ def test_datetime():
1212
assert v.to_python(datetime(2022, 12, 2, 12, 13, 14), mode='json') == '2022-12-02T12:13:14'
1313
assert v.to_json(datetime(2022, 12, 2, 12, 13, 14)) == b'"2022-12-02T12:13:14"'
1414

15-
with pytest.warns(UserWarning, match='Expected `datetime` but got `int` - serialized value may not be as expected'):
15+
with pytest.warns(
16+
UserWarning,
17+
match='Expected `datetime` but got `int` with value `123` - serialized value may not be as expected',
18+
):
1619
assert v.to_python(123, mode='json') == 123
1720

18-
with pytest.warns(UserWarning, match='Expected `datetime` but got `int` - serialized value may not be as expected'):
21+
with pytest.warns(
22+
UserWarning,
23+
match='Expected `datetime` but got `int` with value `123` - serialized value may not be as expected',
24+
):
1925
assert v.to_json(123) == b'123'
2026

2127

tests/serializers/test_decimal.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ def test_decimal():
2020
== b'"123456789123456789123456789.123456789123456789123456789"'
2121
)
2222

23-
with pytest.warns(UserWarning, match='Expected `decimal` but got `int` - serialized value may not be as expected'):
23+
with pytest.warns(
24+
UserWarning, match='Expected `decimal` but got `int` with value `123` - serialized value may not be as expected'
25+
):
2426
assert v.to_python(123, mode='json') == 123
2527

26-
with pytest.warns(UserWarning, match='Expected `decimal` but got `int` - serialized value may not be as expected'):
28+
with pytest.warns(
29+
UserWarning, match='Expected `decimal` but got `int` with value `123` - serialized value may not be as expected'
30+
):
2731
assert v.to_json(123) == b'123'
2832

2933

tests/serializers/test_enum.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ class MyEnum(Enum):
1717
assert v.to_python(MyEnum.a, mode='json') == 1
1818
assert v.to_json(MyEnum.a) == b'1'
1919

20-
with pytest.warns(UserWarning, match='Expected `enum` but got `int` - serialized value may not be as expected'):
20+
with pytest.warns(
21+
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
22+
):
2123
assert v.to_python(1) == 1
22-
with pytest.warns(UserWarning, match='Expected `enum` but got `int` - serialized value may not be as expected'):
24+
with pytest.warns(
25+
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
26+
):
2327
assert v.to_json(1) == b'1'
2428

2529

@@ -35,9 +39,13 @@ class MyEnum(int, Enum):
3539
assert v.to_python(MyEnum.a, mode='json') == 1
3640
assert v.to_json(MyEnum.a) == b'1'
3741

38-
with pytest.warns(UserWarning, match='Expected `enum` but got `int` - serialized value may not be as expected'):
42+
with pytest.warns(
43+
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
44+
):
3945
assert v.to_python(1) == 1
40-
with pytest.warns(UserWarning, match='Expected `enum` but got `int` - serialized value may not be as expected'):
46+
with pytest.warns(
47+
UserWarning, match='Expected `enum` but got `int` with value `1` - serialized value may not be as expected'
48+
):
4149
assert v.to_json(1) == b'1'
4250

4351

@@ -53,9 +61,13 @@ class MyEnum(str, Enum):
5361
assert v.to_python(MyEnum.a, mode='json') == 'a'
5462
assert v.to_json(MyEnum.a) == b'"a"'
5563

56-
with pytest.warns(UserWarning, match='Expected `enum` but got `str` - serialized value may not be as expected'):
64+
with pytest.warns(
65+
UserWarning, match="Expected `enum` but got `str` with value `'a'` - serialized value may not be as expected"
66+
):
5767
assert v.to_python('a') == 'a'
58-
with pytest.warns(UserWarning, match='Expected `enum` but got `str` - serialized value may not be as expected'):
68+
with pytest.warns(
69+
UserWarning, match="Expected `enum` but got `str` with value `'a'` - serialized value may not be as expected"
70+
):
5971
assert v.to_json('a') == b'"a"'
6072

6173

@@ -76,9 +88,13 @@ class MyEnum(Enum):
7688
assert v.to_python({MyEnum.a: 'x'}, mode='json') == {'1': 'x'}
7789
assert v.to_json({MyEnum.a: 'x'}) == b'{"1":"x"}'
7890

79-
with pytest.warns(UserWarning, match='Expected `enum` but got `str` - serialized value may not be as expected'):
91+
with pytest.warns(
92+
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
93+
):
8094
assert v.to_python({'x': 'x'}) == {'x': 'x'}
81-
with pytest.warns(UserWarning, match='Expected `enum` but got `str` - serialized value may not be as expected'):
95+
with pytest.warns(
96+
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
97+
):
8298
assert v.to_json({'x': 'x'}) == b'{"x":"x"}'
8399

84100

@@ -99,7 +115,11 @@ class MyEnum(int, Enum):
99115
assert v.to_python({MyEnum.a: 'x'}, mode='json') == {'1': 'x'}
100116
assert v.to_json({MyEnum.a: 'x'}) == b'{"1":"x"}'
101117

102-
with pytest.warns(UserWarning, match='Expected `enum` but got `str` - serialized value may not be as expected'):
118+
with pytest.warns(
119+
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
120+
):
103121
assert v.to_python({'x': 'x'}) == {'x': 'x'}
104-
with pytest.warns(UserWarning, match='Expected `enum` but got `str` - serialized value may not be as expected'):
122+
with pytest.warns(
123+
UserWarning, match="Expected `enum` but got `str` with value `'x'` - serialized value may not be as expected"
124+
):
105125
assert v.to_json({'x': 'x'}) == b'{"x":"x"}'

tests/serializers/test_functions.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def append_42(value, _info):
207207
assert s.to_python([1, 2, 3], mode='json') == [1, 2, 3, 42]
208208
assert s.to_json([1, 2, 3]) == b'[1,2,3,42]'
209209

210-
msg = r'Expected `list\[int\]` but got `str` - serialized value may not be as expected'
210+
msg = r"Expected `list\[int\]` but got `str` with value `'abc'` - serialized value may not be as expected"
211211
with pytest.warns(UserWarning, match=msg):
212212
assert s.to_python('abc') == 'abc'
213213

@@ -322,11 +322,17 @@ def test_wrong_return_type():
322322
)
323323
)
324324
)
325-
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
325+
with pytest.warns(
326+
UserWarning, match="Expected `int` but got `str` with value `'123'` - serialized value may not be as expected"
327+
):
326328
assert s.to_python(123) == '123'
327-
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
329+
with pytest.warns(
330+
UserWarning, match="Expected `int` but got `str` with value `'123'` - serialized value may not be as expected"
331+
):
328332
assert s.to_python(123, mode='json') == '123'
329-
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
333+
with pytest.warns(
334+
UserWarning, match="Expected `int` but got `str` with value `'123'` - serialized value may not be as expected"
335+
):
330336
assert s.to_json(123) == b'"123"'
331337

332338

@@ -356,11 +362,17 @@ def f(value, serializer):
356362
assert s.to_python(3) == 'result=3'
357363
assert s.to_python(3, mode='json') == 'result=3'
358364
assert s.to_json(3) == b'"result=3"'
359-
with pytest.warns(UserWarning, match='Expected `str` but got `int` - serialized value may not be as expected'):
365+
with pytest.warns(
366+
UserWarning, match='Expected `str` but got `int` with value `42` - serialized value may not be as expected'
367+
):
360368
assert s.to_python(42) == 42
361-
with pytest.warns(UserWarning, match='Expected `str` but got `int` - serialized value may not be as expected'):
369+
with pytest.warns(
370+
UserWarning, match='Expected `str` but got `int` with value `42` - serialized value may not be as expected'
371+
):
362372
assert s.to_python(42, mode='json') == 42
363-
with pytest.warns(UserWarning, match='Expected `str` but got `int` - serialized value may not be as expected'):
373+
with pytest.warns(
374+
UserWarning, match='Expected `str` but got `int` with value `42` - serialized value may not be as expected'
375+
):
364376
assert s.to_json(42) == b'42'
365377

366378

@@ -611,7 +623,9 @@ def f(value, _info):
611623
return value
612624

613625
s = SchemaSerializer(core_schema.with_info_after_validator_function(f, core_schema.int_schema()))
614-
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
626+
with pytest.warns(
627+
UserWarning, match="Expected `int` but got `str` with value `'abc'` - serialized value may not be as expected"
628+
):
615629
assert s.to_python('abc') == 'abc'
616630

617631

@@ -620,7 +634,9 @@ def f(value, handler, _info):
620634
return handler(value)
621635

622636
s = SchemaSerializer(core_schema.with_info_wrap_validator_function(f, core_schema.int_schema()))
623-
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
637+
with pytest.warns(
638+
UserWarning, match="Expected `int` but got `str` with value `'abc'` - serialized value may not be as expected"
639+
):
624640
assert s.to_python('abc') == 'abc'
625641

626642

tests/serializers/test_generator.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ def test_generator_any():
5454
assert s.to_json(iter(['a', b'b', 3])) == b'["a","b",3]'
5555
assert s.to_json(gen_ok('a', b'b', 3)) == b'["a","b",3]'
5656

57-
msg = 'Expected `generator` but got `int` - serialized value may not be as expected'
57+
msg = 'Expected `generator` but got `int` with value `4` - serialized value may not be as expected'
5858
with pytest.warns(UserWarning, match=msg):
5959
assert s.to_python(4) == 4
60-
with pytest.warns(UserWarning, match='Expected `generator` but got `tuple`'):
60+
with pytest.warns(UserWarning, match="Expected `generator` but got `tuple` with value `\\('a', b'b', 3\\)`"):
6161
assert s.to_python(('a', b'b', 3)) == ('a', b'b', 3)
62-
with pytest.warns(UserWarning, match='Expected `generator` but got `str`'):
62+
with pytest.warns(UserWarning, match="Expected `generator` but got `str` with value `'abc'`"):
6363
assert s.to_python('abc') == 'abc'
6464

6565
with pytest.raises(ValueError, match='oops'):
@@ -88,14 +88,21 @@ def test_generator_int():
8888
with pytest.raises(ValueError, match='oops'):
8989
s.to_json(gen_error(1, 2))
9090

91-
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
91+
with pytest.warns(
92+
UserWarning, match="Expected `int` but got `str` with value `'a'` - serialized value may not be as expected"
93+
):
9294
s.to_json(gen_ok(1, 'a'))
9395

9496
gen = s.to_python(gen_ok(1, 'a'))
9597
assert next(gen) == 1
96-
with pytest.warns(UserWarning, match='Expected `int` but got `str` - serialized value may not be as expected'):
98+
with pytest.warns(
99+
UserWarning, match="Expected `int` but got `str` with value `'a'` - serialized value may not be as expected"
100+
):
97101
assert next(gen) == 'a'
98-
with pytest.warns(UserWarning, match='Expected `generator` but got `tuple` - serialized value may not.+'):
102+
with pytest.warns(
103+
UserWarning,
104+
match='Expected `generator` but got `tuple` with value `\\(1, 2, 3\\)` - serialized value may not.+',
105+
):
99106
s.to_python((1, 2, 3))
100107

101108

tests/serializers/test_list_tuple.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ def test_list_any():
1818

1919
def test_list_fallback():
2020
v = SchemaSerializer(core_schema.list_schema(core_schema.any_schema()))
21-
msg = 'Expected `list[any]` but got `str` - serialized value may not be as expected'
21+
msg = "Expected `list[any]` but got `str` with value `'apple'` - serialized value may not be as expected"
2222
with pytest.warns(UserWarning, match=re.escape(msg)):
2323
assert v.to_python('apple') == 'apple'
2424

2525
with pytest.warns(UserWarning) as warning_info:
2626
assert v.to_json('apple') == b'"apple"'
2727
assert [w.message.args[0] for w in warning_info.list] == [
28-
'Pydantic serializer warnings:\n Expected `list[any]` but got `str` - serialized value may not be as expected'
28+
"Pydantic serializer warnings:\n Expected `list[any]` but got `str` with value `'apple'` - serialized value may not be as expected"
2929
]
3030

31-
msg = 'Expected `list[any]` but got `bytes` - serialized value may not be as expected'
31+
msg = "Expected `list[any]` but got `bytes` with value `b'apple'` - serialized value may not be as expected"
3232
with pytest.warns(UserWarning, match=re.escape(msg)):
3333
assert v.to_json(b'apple') == b'"apple"'
3434

35-
msg = 'Expected `list[any]` but got `tuple` - serialized value may not be as expected'
35+
msg = 'Expected `list[any]` but got `tuple` with value `(1, 2, 3)` - serialized value may not be as expected'
3636
with pytest.warns(UserWarning, match=re.escape(msg)):
3737
assert v.to_python((1, 2, 3)) == (1, 2, 3)
3838

3939
# # even though we're in the fallback state, non JSON types should still be converted to JSON here
40-
msg = 'Expected `list[any]` but got `tuple` - serialized value may not be as expected'
40+
msg = 'Expected `list[any]` but got `tuple` with value `(1, 2, 3)` - serialized value may not be as expected'
4141
with pytest.warns(UserWarning, match=re.escape(msg)):
4242
assert v.to_python((1, 2, 3), mode='json') == [1, 2, 3]
4343

@@ -48,18 +48,18 @@ def test_list_str_fallback():
4848
assert v.to_json([1, 2, 3]) == b'[1,2,3]'
4949
assert [w.message.args[0] for w in warning_info.list] == [
5050
'Pydantic serializer warnings:\n'
51-
' Expected `str` but got `int` - serialized value may not be as expected\n'
52-
' Expected `str` but got `int` - serialized value may not be as expected\n'
53-
' Expected `str` but got `int` - serialized value may not be as expected'
51+
' Expected `str` but got `int` with value `1` - serialized value may not be as expected\n'
52+
' Expected `str` but got `int` with value `2` - serialized value may not be as expected\n'
53+
' Expected `str` but got `int` with value `3` - serialized value may not be as expected'
5454
]
5555
with pytest.raises(PydanticSerializationError) as warning_ex:
5656
v.to_json([1, 2, 3], warnings='error')
5757
assert str(warning_ex.value) == ''.join(
5858
[
5959
'Pydantic serializer warnings:\n'
60-
' Expected `str` but got `int` - serialized value may not be as expected\n'
61-
' Expected `str` but got `int` - serialized value may not be as expected\n'
62-
' Expected `str` but got `int` - serialized value may not be as expected'
60+
' Expected `str` but got `int` with value `1` - serialized value may not be as expected\n'
61+
' Expected `str` but got `int` with value `2` - serialized value may not be as expected\n'
62+
' Expected `str` but got `int` with value `3` - serialized value may not be as expected'
6363
]
6464
)
6565

@@ -243,25 +243,25 @@ def test_include_error_call_time(schema_func, seq_f, include, exclude):
243243

244244
def test_tuple_fallback():
245245
v = SchemaSerializer(core_schema.tuple_variable_schema(core_schema.any_schema()))
246-
msg = 'Expected `tuple[any, ...]` but got `str` - serialized value may not be as expected'
246+
msg = "Expected `tuple[any, ...]` but got `str` with value `'apple'` - serialized value may not be as expected"
247247
with pytest.warns(UserWarning, match=re.escape(msg)):
248248
assert v.to_python('apple') == 'apple'
249249

250250
with pytest.warns(UserWarning) as warning_info:
251251
assert v.to_json([1, 2, 3]) == b'[1,2,3]'
252252
assert [w.message.args[0] for w in warning_info.list] == [
253-
'Pydantic serializer warnings:\n Expected `tuple[any, ...]` but got `list` - '
253+
'Pydantic serializer warnings:\n Expected `tuple[any, ...]` but got `list` with value `[1, 2, 3]` - '
254254
'serialized value may not be as expected'
255255
]
256256

257-
msg = 'Expected `tuple[any, ...]` but got `bytes` - serialized value may not be as expected'
257+
msg = "Expected `tuple[any, ...]` but got `bytes` with value `b'apple'` - serialized value may not be as expected"
258258
with pytest.warns(UserWarning, match=re.escape(msg)):
259259
assert v.to_json(b'apple') == b'"apple"'
260260

261261
assert v.to_python((1, 2, 3)) == (1, 2, 3)
262262

263263
# even though we're in the fallback state, non JSON types should still be converted to JSON here
264-
msg = 'Expected `tuple[any, ...]` but got `list` - serialized value may not be as expected'
264+
msg = 'Expected `tuple[any, ...]` but got `list` with value `[1, 2, 3]` - serialized value may not be as expected'
265265
with pytest.warns(UserWarning, match=re.escape(msg)):
266266
assert v.to_python([1, 2, 3], mode='json') == [1, 2, 3]
267267

tests/serializers/test_model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,17 @@ def test_model_wrong_warn():
230230
assert s.to_python(None, mode='json') is None
231231
assert s.to_json(None) == b'null'
232232

233-
with pytest.warns(UserWarning, match='Expected `MyModel` but got `int` - serialized value may.+'):
233+
with pytest.warns(UserWarning, match='Expected `MyModel` but got `int` with value `123` - serialized value may.+'):
234234
assert s.to_python(123) == 123
235-
with pytest.warns(UserWarning, match='Expected `MyModel` but got `int` - serialized value may.+'):
235+
with pytest.warns(UserWarning, match='Expected `MyModel` but got `int` with value `123` - serialized value may.+'):
236236
assert s.to_python(123, mode='json') == 123
237-
with pytest.warns(UserWarning, match='Expected `MyModel` but got `int` - serialized value may.+'):
237+
with pytest.warns(UserWarning, match='Expected `MyModel` but got `int` with value `123` - serialized value may.+'):
238238
assert s.to_json(123) == b'123'
239239

240-
with pytest.warns(UserWarning, match='Expected `MyModel` but got `dict` - serialized value may.+'):
240+
with pytest.warns(
241+
UserWarning,
242+
match="Expected `MyModel` but got `dict` with value `{'foo': 1, 'bar': b'more'}` - serialized value may.+",
243+
):
241244
assert s.to_python({'foo': 1, 'bar': b'more'}) == {'foo': 1, 'bar': b'more'}
242245

243246

0 commit comments

Comments
 (0)