-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/test_mapping.py: add tests, bug fix nested dataclasses
- Loading branch information
Showing
2 changed files
with
57 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,59 @@ | ||
"""Test all asset dataclasses.""" | ||
|
||
from dataclasses import dataclass | ||
from esdl4tulipa.mapping import AssetData | ||
from esdl4tulipa.mapping import asset_types | ||
from esdl4tulipa.mapping import ESDL2TULIPA | ||
from esdl4tulipa.mapping import _producer_t | ||
import pytest | ||
import re | ||
|
||
|
||
def test_asset_types(): | ||
"""Test attribute names and types match.""" | ||
for kind, fields in ESDL2TULIPA.items(): | ||
assert asset_types[kind].__annotations__ == dict(fields.values()) | ||
@dataclass(unsafe_hash=True) | ||
class Leaf(AssetData): | ||
random: int = 42 | ||
|
||
|
||
@dataclass(unsafe_hash=True) | ||
class Nested(AssetData): | ||
flag: bool = True | ||
seed: Leaf = Leaf() | ||
|
||
|
||
def test_nesting_asset_types(): | ||
nest = Nested() | ||
assert isinstance(nest.seed, Leaf) | ||
assert nest.seed.random == 42 | ||
|
||
data = {"flag": False, "seed": {"random": 5}} | ||
nest = Nested(**data) | ||
assert not nest.flag | ||
assert isinstance(nest.seed, Leaf) | ||
assert nest.seed.random == 5 | ||
|
||
|
||
@pytest.mark.parametrize("kind", list(asset_types)) | ||
def test_asset_types(kind: str): | ||
"""Test asset types are default constructable, and hashable.""" | ||
assert hash(asset_types[kind]()) | ||
|
||
|
||
@pytest.mark.parametrize("kind", list(asset_types)) | ||
def test_esdl_key(kind): | ||
asset = asset_types[kind]() | ||
|
||
# test examples | ||
key_re = re.compile(r"cost|lifetime") | ||
esdl_re = re.compile(r"costInformation\..+\.value|technicalLifetime") | ||
|
||
for fld in asset.fields(): | ||
try: | ||
assert asset.esdl_key(fld) | ||
except AssertionError: | ||
if fld in ("from_asset", "to_asset"): | ||
# from & to nodes have no ESDL equivalent | ||
pass | ||
else: | ||
raise | ||
|
||
if isinstance(asset, _producer_t) and key_re.match(fld): | ||
assert esdl_re.match(asset.esdl_key(fld)) |