|
4 | 4 | import logging
|
5 | 5 | import pathlib
|
6 | 6 | import uuid
|
7 |
| -from typing import ClassVar, DefaultDict, Dict, FrozenSet, List, Optional, Set, Tuple, Union |
| 7 | +from typing import ( |
| 8 | + ClassVar, |
| 9 | + DefaultDict, |
| 10 | + Dict, |
| 11 | + FrozenSet, |
| 12 | + List, |
| 13 | + Optional, |
| 14 | + Set, |
| 15 | + Tuple, |
| 16 | + Union, |
| 17 | +) |
8 | 18 |
|
9 | 19 | import pytest
|
10 | 20 |
|
@@ -536,6 +546,45 @@ class Foo:
|
536 | 546 | assert ff.a == 10
|
537 | 547 |
|
538 | 548 |
|
| 549 | +def test_default_and_alias(): |
| 550 | + @serde.serde |
| 551 | + class Foo: |
| 552 | + a: int = serde.field(default=2, alias=["b", "c", "d"]) |
| 553 | + |
| 554 | + f = Foo(a=1) |
| 555 | + assert '{"a":1}' == serde.json.to_json(f) |
| 556 | + ff = serde.json.from_json(Foo, '{"b":10}') |
| 557 | + assert ff.a == 10 |
| 558 | + ff = serde.json.from_json(Foo, '{"e":10}') |
| 559 | + assert ff.a == 2 |
| 560 | + |
| 561 | + |
| 562 | +def test_default_and_rename(): |
| 563 | + @serde.serde |
| 564 | + class Foo: |
| 565 | + a: int = serde.field(default=2, rename="z") |
| 566 | + |
| 567 | + f = Foo(a=1) |
| 568 | + assert '{"z":1}' == serde.json.to_json(f) |
| 569 | + ff = serde.json.from_json(Foo, '{"z":10}') |
| 570 | + assert ff.a == 10 |
| 571 | + fff = serde.json.from_json(Foo, '{"a":10}') |
| 572 | + assert fff.a == 2 |
| 573 | + |
| 574 | + |
| 575 | +def test_default_rename_and_alias(): |
| 576 | + @serde.serde |
| 577 | + class Foo: |
| 578 | + a: int = serde.field(default=2, rename="z", alias=["b", "c", "d"]) |
| 579 | + |
| 580 | + f = Foo(a=1) |
| 581 | + assert '{"z":1}' == serde.json.to_json(f) |
| 582 | + ff = serde.json.from_json(Foo, '{"b":10}') |
| 583 | + assert ff.a == 10 |
| 584 | + ff = serde.json.from_json(Foo, '{"e":10}') |
| 585 | + assert ff.a == 2 |
| 586 | + |
| 587 | + |
539 | 588 | @pytest.mark.parametrize(
|
540 | 589 | 'se,de', (format_dict + format_json + format_msgpack + format_yaml + format_toml + format_pickle)
|
541 | 590 | )
|
|
0 commit comments