-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
80 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2417,10 +2417,10 @@ class _(JSONWizard.Meta): | |
assert opt == Options(my_extras={}, email='[email protected]') | ||
|
||
|
||
def test_from_dict_with_nested_object_key_path(): | ||
def test_from_dict_with_nested_object_alias_path(): | ||
""" | ||
Specifying a custom mapping of "nested" JSON key to dataclass field, | ||
via the `KeyPath` and `path_field` helper functions. | ||
Specifying a custom mapping of "nested" alias to dataclass field, | ||
via the `AliasPath` helper function. | ||
""" | ||
|
||
@dataclass | ||
|
@@ -2507,12 +2507,12 @@ class _(JSONPyWizard.Meta): | |
} | ||
|
||
|
||
def test_from_dict_with_nested_object_key_path_with_skip_defaults(): | ||
def test_from_dict_with_nested_object_alias_path_with_skip_defaults(): | ||
""" | ||
Specifying a custom mapping of "nested" JSON key to dataclass field, | ||
via the `KeyPath` and `path_field` helper functions. | ||
Specifying a custom mapping of "nested" alias to dataclass field, | ||
via the `AliasPath` helper function. | ||
Test with `skip_defaults=True` and `dump=False`. | ||
Test with `skip_defaults=True`, `load_alias`, and `skip=True`. | ||
""" | ||
|
||
@dataclass | ||
|
@@ -2618,6 +2618,48 @@ class _(JSONWizard.Meta): | |
} | ||
|
||
|
||
def test_from_dict_with_nested_object_alias_path_with_dump_alias_and_skip(): | ||
""" | ||
Test nested object `AliasPath` with dump='...' and skip=True, | ||
along with `Alias` with `skip=True`, | ||
added for branch coverage. | ||
""" | ||
@dataclass | ||
class A(JSONWizard): | ||
|
||
class _(JSONWizard.Meta): | ||
v1 = True | ||
|
||
my_str: str = AliasPath(dump='a.b.c[0]') | ||
my_bool: bool = AliasPath('x.y."Z 1"', skip=True) | ||
my_int: int = Alias('my Integer', skip=True) | ||
|
||
d = {'a': {'b': {'c': [1, 2, 3]}}, | ||
'x': {'y': {'Z 1': 'f'}},} | ||
|
||
with pytest.raises(MissingFields) as exc_info: | ||
_ = A.from_dict(d) | ||
|
||
e = exc_info.value | ||
assert e.fields == ['my_bool'] | ||
assert e.missing_fields == ['my_str', 'my_int'] | ||
|
||
d = {'my_str': 'test', | ||
'my Integer': '123', | ||
'x': {'y': {'Z 1': 'f'}},} | ||
|
||
a = A.from_dict(d) | ||
|
||
assert a.my_str == 'test' | ||
assert a.my_int == 123 | ||
assert a.my_bool is False | ||
|
||
serialized = a.to_dict() | ||
assert serialized == { | ||
'a': {'b': {'c': {0: 'test'}}}, | ||
} | ||
|
||
|
||
def test_auto_assign_tags_and_raise_on_unknown_json_key(): | ||
|
||
@dataclass | ||
|