Skip to content
Snippets Groups Projects
Commit dbe95dfd authored by leufen1's avatar leufen1
Browse files

new tests for helpers /close #241

parent a7cb6bae
No related branches found
No related tags found
3 merge requests!226Develop,!225Resolve "release v1.2.0",!215Resolve "new tests for helpers"
Pipeline #55558 passed
This commit is part of merge request !225. Comments created here will be created in the context of that merge request.
...@@ -18,7 +18,7 @@ def to_list(obj: Any) -> List: ...@@ -18,7 +18,7 @@ def to_list(obj: Any) -> List:
:return: list containing obj, or obj itself (if obj was already a list) :return: list containing obj, or obj itself (if obj was already a list)
""" """
if isinstance(obj, set): if isinstance(obj, (set, tuple)):
obj = list(obj) obj = list(obj)
elif not isinstance(obj, list): elif not isinstance(obj, list):
obj = [obj] obj = [obj]
...@@ -116,6 +116,12 @@ def select_from_dict(dict_obj: dict, sel_list: Any): ...@@ -116,6 +116,12 @@ def select_from_dict(dict_obj: dict, sel_list: Any):
def extract_value(encapsulated_value): def extract_value(encapsulated_value):
try: try:
if isinstance(encapsulated_value, str):
raise TypeError
if len(encapsulated_value) == 1:
return extract_value(encapsulated_value[0]) return extract_value(encapsulated_value[0])
else:
raise NotImplementedError("Trying to extract an encapsulated value from objects with more than a single "
"entry is not supported by this function.")
except TypeError: except TypeError:
return encapsulated_value return encapsulated_value
...@@ -10,7 +10,7 @@ import os ...@@ -10,7 +10,7 @@ import os
import mock import mock
import pytest import pytest
from mlair.helpers import to_list, dict_to_xarray, float_round, remove_items from mlair.helpers import to_list, dict_to_xarray, float_round, remove_items, extract_value, select_from_dict
from mlair.helpers import PyTestRegex from mlair.helpers import PyTestRegex
from mlair.helpers import Logger, TimeTracking from mlair.helpers import Logger, TimeTracking
...@@ -22,6 +22,9 @@ class TestToList: ...@@ -22,6 +22,9 @@ class TestToList:
assert to_list('abcd') == ['abcd'] assert to_list('abcd') == ['abcd']
assert to_list([1, 2, 3]) == [1, 2, 3] assert to_list([1, 2, 3]) == [1, 2, 3]
assert to_list([45]) == [45] assert to_list([45]) == [45]
assert to_list({34, 2, "test"}) == [34, 2, "test"]
assert to_list((34, 2, "test")) == [34, 2, "test"]
assert to_list(("test")) == ["test"]
class TestTimeTracking: class TestTimeTracking:
...@@ -164,6 +167,22 @@ class TestFloatRound: ...@@ -164,6 +167,22 @@ class TestFloatRound:
assert float_round(-34.9221, 0) == -34. assert float_round(-34.9221, 0) == -34.
class TestSelectFromDict:
@pytest.fixture
def dictionary(self):
return {"a": 1, "b": 23, "c": "last"}
def test_select(self, dictionary):
assert select_from_dict(dictionary, "c") == {"c": "last"}
assert select_from_dict(dictionary, ["a", "c"]) == {"a": 1, "c": "last"}
assert select_from_dict(dictionary, "d") == {}
def test_select_no_dict_given(self):
with pytest.raises(AssertionError):
select_from_dict(["we"], "now")
class TestRemoveItems: class TestRemoveItems:
@pytest.fixture @pytest.fixture
...@@ -229,6 +248,11 @@ class TestRemoveItems: ...@@ -229,6 +248,11 @@ class TestRemoveItems:
remove_items(custom_list) remove_items(custom_list)
assert "remove_items() missing 1 required positional argument: 'items'" in e.value.args[0] assert "remove_items() missing 1 required positional argument: 'items'" in e.value.args[0]
def test_remove_not_supported_type(self):
with pytest.raises(TypeError) as e:
remove_items(23, "test")
assert f"remove_items does not support type {type(23)}" in e.value.args[0]
class TestLogger: class TestLogger:
...@@ -272,3 +296,18 @@ class TestLogger: ...@@ -272,3 +296,18 @@ class TestLogger:
with pytest.raises(TypeError) as e: with pytest.raises(TypeError) as e:
logger.logger_console(1.5) logger.logger_console(1.5)
assert "Level not an integer or a valid string: 1.5" == e.value.args[0] assert "Level not an integer or a valid string: 1.5" == e.value.args[0]
class TestExtractValue:
def test_extract(self):
assert extract_value([1]) == 1
assert extract_value([[23]]) == 23
assert extract_value([("test")]) == "test"
assert extract_value((2,)) == 2
def test_extract_multiple_elements(self):
with pytest.raises(NotImplementedError) as e:
extract_value([1, 2, 3])
assert "Trying to extract an encapsulated value from objects with more than a single entry is not supported " \
"by this function." in e.value.args[0]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment