Skip to content

Commit 931759a

Browse files
Fix collecting values for enumeration
1 parent 54dae35 commit 931759a

File tree

12 files changed

+582
-15
lines changed

12 files changed

+582
-15
lines changed

.github/workflows/push_request_check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
poetry run download-samm-release
3838
poetry build
3939
40-
- name: run test*s
40+
- name: run tests
4141
run: |
4242
cd core/esmf-aspect-meta-model-python
4343
poetry run tox -e py310

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/impl/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@
4545
from .default_property import DefaultBlankProperty, DefaultProperty, DefaultPropertyWithExtends
4646
from .default_quantity_kind import DefaultQuantityKind
4747
from .default_unit import DefaultUnit
48+
from .default_value import DefaultValue

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/enumeration_instantiator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def __to_enum_node_value(self, value_node: Node) -> Union[Dict, str]:
4949
- If value_node is a URIRef it will represent a value of a ComplexType
5050
:return: the one generated value of the enumeration
5151
"""
52-
str_value: str = ""
53-
dict_value: Dict = {}
52+
str_value: Optional[str] = None
53+
dict_value: Optional[Dict] = None
5454

5555
if isinstance(value_node, rdflib.Literal) or (
5656
isinstance(value_node, rdflib.URIRef) and value_node.find("#") == -1
@@ -88,7 +88,7 @@ def __to_enum_node_value(self, value_node: Node) -> Union[Dict, str]:
8888
f"a URI reference to a ComplexType. Values of type {type(value_node).__name__} are not allowed"
8989
)
9090

91-
return str_value or dict_value
91+
return str_value if str_value is not None else dict_value if dict_value is not None else ""
9292

9393
def __is_collection_value(self, property_subject: str) -> bool:
9494
characteristic = self._aspect_graph.value( # type: ignore

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/instantiator/state_instantiator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ def __to_state_node_value(self, value_node: Optional[Node]) -> Dict:
6767
if property_urn != rdflib.RDF.type and isinstance(property_urn, str):
6868
property_name = property_urn.split("#")[1]
6969
actual_value: Optional[Any]
70-
if self.__is_collection_value(property_urn):
71-
actual_value = self.__instantiate_enum_collection(property_value)
70+
if self._EnumerationInstantiator__is_collection_value(property_urn): # type: ignore
71+
actual_value = self._EnumerationInstantiator__instantiate_enum_collection( # type: ignore
72+
property_value
73+
)
7274
else:
7375
actual_value = self.__to_state_node_value(property_value)
7476
value[property_name] = actual_value

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/loader/meta_model_base_attributes.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ def from_meta_model_element(
9494

9595
return MetaModelBaseAttributes(meta_model_version, urn, name, preferred_names, descriptions, see)
9696

97-
raise TypeError(
98-
"Unexpected type. Get MetaModelBaseAttributes.from_meta_model_element \
99-
can't handle this type."
100-
)
97+
raise TypeError("Unexpected type. Get MetaModelBaseAttributes.from_meta_model_element can't handle this type.")
10198

10299
@staticmethod
103100
def __create_default_name(meta_model_element: rdflib.BNode, aspect_graph, samm) -> str:
@@ -129,12 +126,14 @@ def __get_name_from_urn(urn: str) -> str:
129126
urn:samm:org.eclipse.esmf.examples:TestAspect:1.0.0 -> TestAspect
130127
"""
131128
split_urn = urn.split("#")
129+
132130
if len(split_urn) == 2:
133-
# urn:samm:org.eclipse.esmf.examples#testProperty
134-
return split_urn[1]
135-
# urn:samm:org.eclipse.esmf.examples:TestAspect:1.0.0
136-
split_urn = urn.split(":")
137-
return split_urn[-2]
131+
name = split_urn[1]
132+
else:
133+
split_urn = urn.split(":")
134+
name = split_urn[-2]
135+
136+
return name
138137

139138
@staticmethod
140139
def __get_language_strings(
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""Aspect interface test suite."""
2+
3+
from esmf_aspect_meta_model_python.base.aspect import Aspect
4+
5+
6+
class AspectInterface(Aspect):
7+
"""Aspect interface class for testing."""
8+
9+
def __init__(self, name):
10+
self._name = name
11+
12+
def urn(self):
13+
pass
14+
15+
def meta_model_version(self):
16+
pass
17+
18+
@property
19+
def name(self):
20+
return self._name
21+
22+
def preferred_names(self):
23+
pass
24+
25+
def descriptions(self):
26+
pass
27+
28+
def see(self):
29+
pass
30+
31+
def events(self):
32+
pass
33+
34+
def operations(self):
35+
pass
36+
37+
def properties(self):
38+
pass
39+
40+
@property
41+
def parent_elements(self):
42+
return None
43+
44+
@parent_elements.setter
45+
def parent_elements(self, elements):
46+
pass
47+
48+
def append_parent_element(self, element):
49+
pass
50+
51+
52+
class TestAspect:
53+
"""Aspect interface test suite."""
54+
55+
def test_is_collection_aspect(self):
56+
aspect = AspectInterface("name")
57+
result = aspect.is_collection_aspect
58+
59+
assert result is False
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""Property interface test suite."""
2+
3+
from unittest import mock
4+
5+
from esmf_aspect_meta_model_python.base.characteristics.trait import Trait
6+
from esmf_aspect_meta_model_python.base.property import Property
7+
8+
9+
class PropertyInterface(Property):
10+
"""Property interface class for testing."""
11+
12+
def __init__(self, name, characteristic=None):
13+
self.name = name
14+
self.characteristic = characteristic
15+
16+
def characteristic(self):
17+
pass
18+
19+
def example_value(self):
20+
pass
21+
22+
def is_abstract(self):
23+
pass
24+
25+
def extends(self):
26+
pass
27+
28+
def is_optional(self):
29+
pass
30+
31+
def is_not_in_payload(self):
32+
pass
33+
34+
def payload_name(self):
35+
pass
36+
37+
@property
38+
def parent_elements(self):
39+
return None
40+
41+
@parent_elements.setter
42+
def parent_elements(self, elements):
43+
pass
44+
45+
def append_parent_element(self, element):
46+
pass
47+
48+
def name(self):
49+
return self.name
50+
51+
def preferred_names(self):
52+
pass
53+
54+
def descriptions(self):
55+
pass
56+
57+
def see(self):
58+
pass
59+
60+
def urn(self):
61+
pass
62+
63+
def meta_model_version(self):
64+
pass
65+
66+
67+
class TestProperty:
68+
"""Property interface test suite."""
69+
70+
def test_effective_characteristic_empty(self):
71+
interface = PropertyInterface("name")
72+
result = interface.effective_characteristic
73+
74+
assert result is None
75+
76+
@mock.patch("esmf_aspect_meta_model_python.base.property.isinstance")
77+
def test_effective_characteristic(self, isinstance_mock):
78+
characteristic_mock = mock.MagicMock(name="characteristic")
79+
characteristic_mock.base_characteristic = "base_characteristic"
80+
isinstance_mock.side_effect = (True, False)
81+
interface = PropertyInterface("name", characteristic_mock)
82+
result = interface.effective_characteristic
83+
84+
assert result == "base_characteristic"
85+
isinstance_mock.assert_has_calls(
86+
[
87+
mock.call(characteristic_mock, Trait),
88+
mock.call("base_characteristic", Trait),
89+
]
90+
)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Property Func interface test suite."""
2+
3+
from unittest import mock
4+
5+
import pytest
6+
7+
from esmf_aspect_meta_model_python.base.property_func import PropertyFunc
8+
9+
10+
class TestPropertyFunc:
11+
"""Property Func interface test suite."""
12+
13+
def test_fget_name(self):
14+
callable_property_mock = mock.MagicMock(name="callable_property")
15+
callable_property_mock.fget.__name__ = "test_name"
16+
result = PropertyFunc.fget_name(callable_property_mock)
17+
18+
assert result == "test_name"
19+
20+
def test_fget_name_raises_attribute_error(self):
21+
with pytest.raises(AttributeError) as error:
22+
PropertyFunc.fget_name(None)
23+
24+
assert str(error.value) == "Unable to execute fget.__name__ for this argument."
25+
26+
def test_has_properties(self):
27+
class TestClass:
28+
def __init__(self, a, b):
29+
self._a = a
30+
self._b = b
31+
32+
@property
33+
def a(self):
34+
return self._a
35+
36+
@property
37+
def b(self):
38+
return self._b
39+
40+
obj = TestClass("value_1", "value_2")
41+
result = PropertyFunc.has_properties(obj, TestClass.a, TestClass.b)
42+
43+
assert result is True
44+
45+
def test_has_properties_missing_property(self):
46+
class BaseTestClass:
47+
def __init__(self, a, b):
48+
self._a = a
49+
self._b = b
50+
51+
@property
52+
def a(self):
53+
return self._a
54+
55+
@property
56+
def b(self):
57+
return self._b
58+
59+
class TestClass:
60+
def __init__(self, a, b):
61+
self._a = a
62+
self._b = b
63+
64+
@property
65+
def a(self):
66+
return self._a
67+
68+
obj = TestClass("value_1", "value_2")
69+
result = PropertyFunc.has_properties(obj, BaseTestClass.a, BaseTestClass.b)
70+
71+
assert result is False

core/esmf-aspect-meta-model-python/tests/unit/impl/characteristics/test_characteristic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from unittest import mock
44

5+
import pytest
6+
57
from esmf_aspect_meta_model_python.impl import DefaultCharacteristic
68

79

@@ -21,6 +23,12 @@ def test_init(self, super_mock, isinstance_mock):
2123
assert result._data_type == self.data_type_mock
2224
self.data_type_mock.append_parent_element.assert_called_once_with(result)
2325

26+
def test_init_raise_exception(self):
27+
with pytest.raises(ValueError) as error:
28+
DefaultCharacteristic(self.meta_model_mock, None)
29+
30+
assert str(error.value) == "Attribute 'data_type' is required for Characteristic class."
31+
2432
@mock.patch("esmf_aspect_meta_model_python.impl.characteristics.default_characteristic.BaseImpl.__init__")
2533
def test_data_type(self, _):
2634
characteristic = DefaultCharacteristic(self.meta_model_mock, self.data_type_mock)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""DefaultValue class unit tests suit."""
2+
3+
from unittest import mock
4+
5+
from esmf_aspect_meta_model_python.impl import DefaultValue
6+
7+
8+
class TestDefaultValue:
9+
"""DefaultValue unit tests class."""
10+
11+
meta_model_mock = mock.MagicMock(name="meta_model_base_attributes")
12+
13+
@mock.patch("esmf_aspect_meta_model_python.impl.default_unit.BaseImpl.__init__")
14+
def test_init(self, super_mock):
15+
result = DefaultValue(self.meta_model_mock, "value")
16+
17+
super_mock.assert_called_once_with(self.meta_model_mock)
18+
assert result.value == "value"

0 commit comments

Comments
 (0)