add basic functionality and corresponding tests
This commit is contained in:
64
tests/test_datastructures.py
Normal file
64
tests/test_datastructures.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import pytest
|
||||
|
||||
from dopt_basics import datastructures as dst
|
||||
|
||||
|
||||
def test_flatten():
|
||||
nested_iterable = ([1, 2], [[3], [4, 5]], [6, [7, 8, 9]])
|
||||
target = tuple(i for i in range(1, 10))
|
||||
|
||||
ret_iter = dst.flatten(nested_iterable)
|
||||
ret = tuple(ret_iter)
|
||||
assert ret == target
|
||||
|
||||
|
||||
def test_DualDict():
|
||||
base_dict: dict[str, int] = {"test1": 1, "test2": 2, "test3": 3}
|
||||
inverted_dict: dict[int, str] = {1: "test1", 2: "test2", 3: "test3"}
|
||||
assert all((key == inverted_dict[value] for key, value in base_dict.items()))
|
||||
|
||||
dual_dict: dst.DualDict[str, int] = dst.DualDict(test1=1, test2=2, test3=3)
|
||||
|
||||
assert all((key in dual_dict for key in base_dict.keys()))
|
||||
assert all((base_dict[key] == dual_dict[key] for key in base_dict.keys()))
|
||||
assert all((key == dual_dict.inverted[value] for key, value in base_dict.items()))
|
||||
assert all(
|
||||
(inverted_dict[key] == dual_dict.inverted[key] for key in inverted_dict.keys())
|
||||
)
|
||||
base_dict["test_add"] = 5
|
||||
dual_dict["test_add"] = 5
|
||||
assert len(base_dict) == len(dual_dict)
|
||||
assert len(dual_dict) == len(dual_dict.inverted)
|
||||
del base_dict["test_add"]
|
||||
del dual_dict["test_add"]
|
||||
assert len(base_dict) == len(dual_dict)
|
||||
assert len(dual_dict) == len(dual_dict.inverted)
|
||||
|
||||
for key_base, key_dd in zip(base_dict, dual_dict):
|
||||
assert key_base == key_dd
|
||||
|
||||
|
||||
def test_DualDict_update_Success():
|
||||
base_dict: dict[str, int] = {"test1": 1, "test2": 2, "test3": 3}
|
||||
dual_dict: dst.DualDict[str, int] = dst.DualDict(test1=1, test2=2, test3=3)
|
||||
|
||||
update = dict(test3=4, test4=5)
|
||||
base_dict.update(**update)
|
||||
dual_dict.update(**update)
|
||||
|
||||
assert all((key in dual_dict for key in base_dict.keys()))
|
||||
assert all((base_dict[key] == dual_dict[key] for key in base_dict.keys()))
|
||||
assert all((key == dual_dict.inverted[value] for key, value in base_dict.items()))
|
||||
|
||||
|
||||
def test_DualDict_update_FailIdenticalValues():
|
||||
base_dict: dict[str, int] = {"test1": 1, "test2": 2, "test3": 3}
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
_: dst.DualDict[str, int] = dst.DualDict(test1=1, test2=3, test3=3)
|
||||
|
||||
dual_dict: dst.DualDict[str, int] = dst.DualDict(test1=1, test2=2, test3=3)
|
||||
update = dict(test3=4, test4=4)
|
||||
base_dict.update(**update)
|
||||
with pytest.raises(ValueError):
|
||||
dual_dict.update(**update)
|
||||
Reference in New Issue
Block a user