-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
83 additions
and
232 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
python_sources() | ||
|
||
python_tests( | ||
name="tests", | ||
) |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import copy | ||
import json | ||
import string | ||
from dataclasses import dataclass | ||
from typing import Dict, List | ||
|
||
import pytest | ||
|
||
import empyc.dataclasses | ||
|
||
|
||
def compare_no_ws(a, b): | ||
remove = {ord(c): None for c in string.punctuation + string.whitespace} | ||
|
||
assert a.translate(remove) == b.translate(remove) | ||
|
||
|
||
@dataclass | ||
class Inner: | ||
list_data: List[int] | ||
|
||
|
||
@dataclass | ||
class Outer: | ||
inner_data: Inner | ||
something_else: Dict[str, str] | ||
|
||
|
||
@pytest.fixture | ||
def outer() -> Outer: | ||
return Outer(inner_data=Inner(list_data=[1, 2, 3]), something_else={"hello": "world"}) | ||
|
||
|
||
@pytest.fixture | ||
def outer_dict() -> Outer: | ||
return { | ||
"inner_data": {"list_data": [1, 2, 3]}, | ||
"something_else": {"hello": "world"}, | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def outer_json() -> Outer: | ||
return """{ | ||
"inner_data": {"list_data": [1,2,3]}, | ||
"something_else": {"hello": "world"} | ||
}""" | ||
|
||
|
||
def test_outer_to_json(outer, outer_json): | ||
compare_no_ws(empyc.dataclasses.to_json(outer), outer_json) | ||
|
||
|
||
def test_from_dict_no_modify(outer_dict): | ||
indata = copy.deepcopy(outer_dict) | ||
_ = empyc.dataclasses.recursive_from_dict(Outer, indata) | ||
|
||
assert indata == outer_dict | ||
|
||
|
||
def test_outer_from_dict(outer, outer_dict): | ||
other = empyc.dataclasses.recursive_from_dict(Outer, outer_dict) | ||
assert other == outer | ||
|
||
|
||
def test_outer_from_json(outer, outer_json): | ||
other = empyc.dataclasses.recursive_from_dict(Outer, json.loads(outer_json)) | ||
assert other == outer | ||
|
||
|
||
def test_outer_roundtrip(outer): | ||
other = empyc.dataclasses.recursive_from_dict( | ||
Outer, json.loads(empyc.dataclasses.to_json(outer)) | ||
) | ||
|
||
assert other == outer |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.