Skip to content

Commit 772f706

Browse files
committed
serialize_by_alias tests
1 parent 2c5c0f8 commit 772f706

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

tests/serializers/test_dataclasses.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,44 @@ class Bar:
235235
s = SchemaSerializer(schema)
236236
assert s.to_python(Foo(x=1), warnings='error') == {'x': 1}
237237
assert s.to_python(Foo(x=1, init_var=2), warnings='error') == {'x': 1}
238+
239+
240+
@pytest.mark.parametrize(
241+
'config,runtime,expected',
242+
[
243+
(True, True, {'A': 'hello'}),
244+
(True, False, {'a': 'hello'}),
245+
(True, None, {'A': 'hello'}),
246+
(False, True, {'A': 'hello'}),
247+
(False, False, {'a': 'hello'}),
248+
(False, None, {'a': 'hello'}),
249+
(None, True, {'A': 'hello'}),
250+
(None, False, {'a': 'hello'}),
251+
(None, None, {'a': 'hello'}),
252+
],
253+
)
254+
def test_alias_by_config_via_runtime_setting(config, runtime, expected) -> None:
255+
"""This test reflects the priority that applies for config vs runtime serialization alias configuration.
256+
257+
If the runtime value (by_alias) is set, that value is used.
258+
If the runtime value is unset, the config value (serialize_by_alias) is used.
259+
If neither are set, the default, False, is used.
260+
"""
261+
262+
@dataclasses.dataclass
263+
class Foo:
264+
a: str
265+
266+
schema = core_schema.dataclass_schema(
267+
Foo,
268+
core_schema.dataclass_args_schema(
269+
'Foo',
270+
[
271+
core_schema.dataclass_field(name='a', schema=core_schema.str_schema(), serialization_alias='A'),
272+
],
273+
),
274+
['a'],
275+
config=core_schema.CoreConfig(serialize_by_alias=config) if config is not None else {},
276+
)
277+
s = SchemaSerializer(schema)
278+
assert s.to_python(Foo(a='hello'), by_alias=runtime) == expected

tests/serializers/test_model.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,3 +1174,42 @@ class BModel(BasicModel): ...
11741174
with pytest.warns(UserWarning, match='Expected 2 fields but got 1 for type `.*AModel` with value `.*`.+'):
11751175
value = BasicModel(root=AModel(type='a'))
11761176
s.to_python(value)
1177+
1178+
1179+
@pytest.mark.parametrize(
1180+
'config,runtime,expected',
1181+
[
1182+
(True, True, {'A': 1}),
1183+
(True, False, {'a': 1}),
1184+
(True, None, {'A': 1}),
1185+
(False, True, {'A': 1}),
1186+
(False, False, {'a': 1}),
1187+
(False, None, {'a': 1}),
1188+
(None, True, {'A': 1}),
1189+
(None, False, {'a': 1}),
1190+
(None, None, {'a': 1}),
1191+
],
1192+
)
1193+
def test_alias_by_config_via_runtime_setting(config, runtime, expected) -> None:
1194+
"""This test reflects the priority that applies for config vs runtime serialization alias configuration.
1195+
1196+
If the runtime value (by_alias) is set, that value is used.
1197+
If the runtime value is unset, the config value (serialize_by_alias) is used.
1198+
If neither are set, the default, False, is used.
1199+
"""
1200+
1201+
class Model:
1202+
def __init__(self, a: int) -> None:
1203+
self.a = a
1204+
1205+
schema = core_schema.model_schema(
1206+
Model,
1207+
core_schema.model_fields_schema(
1208+
{
1209+
'a': core_schema.model_field(core_schema.int_schema(), serialization_alias='A'),
1210+
}
1211+
),
1212+
config=core_schema.CoreConfig(serialize_by_alias=config) if config is not None else {},
1213+
)
1214+
s = SchemaSerializer(schema)
1215+
assert s.to_python(Model(1), by_alias=runtime) == expected

tests/serializers/test_typed_dict.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,37 @@ def test_extra_custom_serializer():
333333
m = {'extra': 'extra'}
334334

335335
assert s.to_python(m) == {'extra': 'extra bam!'}
336+
337+
338+
@pytest.mark.parametrize(
339+
'config,runtime,expected',
340+
[
341+
(True, True, {'A': 1}),
342+
(True, False, {'a': 1}),
343+
(True, None, {'A': 1}),
344+
(False, True, {'A': 1}),
345+
(False, False, {'a': 1}),
346+
(False, None, {'a': 1}),
347+
(None, True, {'A': 1}),
348+
(None, False, {'a': 1}),
349+
(None, None, {'a': 1}),
350+
],
351+
)
352+
def test_alias_by_config_via_runtime_setting(config, runtime, expected) -> None:
353+
"""This test reflects the priority that applies for config vs runtime serialization alias configuration.
354+
355+
If the runtime value (by_alias) is set, that value is used.
356+
If the runtime value is unset, the config value (serialize_by_alias) is used.
357+
If neither are set, the default, False, is used.
358+
"""
359+
360+
class Model(TypedDict):
361+
a: int
362+
363+
schema = core_schema.typed_dict_schema(
364+
{
365+
'a': core_schema.typed_dict_field(core_schema.int_schema(), serialization_alias='A'),
366+
},
367+
)
368+
s = SchemaSerializer(schema, config=core_schema.CoreConfig(serialize_by_alias=config) if config is not None else {})
369+
assert s.to_python(Model(a=1), by_alias=runtime) == expected

0 commit comments

Comments
 (0)