Skip to content
Snippets Groups Projects
Commit 4ff4cca5 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

ENH: completed implementation of role keyword and added several tests

parent d452e955
Branches
Tags
2 merge requests!39Release 0.4.0,!30F extend yaml model
...@@ -36,7 +36,8 @@ KEYWORDS = ["parent", # TODO: can we remove that, see: #36 ...@@ -36,7 +36,8 @@ KEYWORDS = ["parent", # TODO: can we remove that, see: #36
"inherit_from_recommended", "inherit_from_recommended",
"inherit_from_suggested", "inherit_from_suggested",
"inherit_from_obligatory", "inherit_from_obligatory",
"role", ] "role",
"value", ]
# TODO: check whether it's really ignored # TODO: check whether it's really ignored
# These KEYWORDS are not forbidden as properties, but merely ignored. # These KEYWORDS are not forbidden as properties, but merely ignored.
...@@ -278,8 +279,11 @@ class Parser(object): ...@@ -278,8 +279,11 @@ class Parser(object):
raise RuntimeError("Unknown role {} in definition of entity.".format( raise RuntimeError("Unknown role {} in definition of entity.".format(
definition["role"])) definition["role"]))
# add other definitions recursively # for setting values of properties directly:
if not isinstance(definition, dict):
return
# add other definitions recursively
for prop_type in ["recommended_properties", for prop_type in ["recommended_properties",
"suggested_properties", "obligatory_properties"]: "suggested_properties", "obligatory_properties"]:
...@@ -322,8 +326,12 @@ class Parser(object): ...@@ -322,8 +326,12 @@ class Parser(object):
name=n, name=n,
importance=importance, importance=importance,
datatype=db.LIST(_get_listdatatype(e["datatype"]))) datatype=db.LIST(_get_listdatatype(e["datatype"])))
elif e is None:
self.model[ent_name].add_property(name=n,
importance=importance)
else: else:
self.model[ent_name].add_property(name=n, self.model[ent_name].add_property(name=n,
value=e,
importance=importance) importance=importance)
def _inherit(self, name, prop, inheritance): def _inherit(self, name, prop, inheritance):
...@@ -347,6 +355,10 @@ class Parser(object): ...@@ -347,6 +355,10 @@ class Parser(object):
if definition is None: if definition is None:
return return
# for setting values of properties directly:
if not isinstance(definition, dict):
return
if ("datatype" in definition if ("datatype" in definition
and definition["datatype"].startswith("LIST")): and definition["datatype"].startswith("LIST")):
...@@ -363,6 +375,9 @@ class Parser(object): ...@@ -363,6 +375,9 @@ class Parser(object):
if prop_name == "unit": if prop_name == "unit":
self.model[name].unit = prop self.model[name].unit = prop
elif prop_name == "value":
self.model[name].value = prop
elif prop_name == "description": elif prop_name == "description":
self.model[name].description = prop self.model[name].description = prop
...@@ -391,6 +406,10 @@ class Parser(object): ...@@ -391,6 +406,10 @@ class Parser(object):
elif prop_name == "datatype": elif prop_name == "datatype":
continue continue
# role has already been used
elif prop_name == "role":
continue
elif prop_name == "inherit_from_obligatory": elif prop_name == "inherit_from_obligatory":
self._inherit(name, prop, db.OBLIGATORY) self._inherit(name, prop, db.OBLIGATORY)
elif prop_name == "inherit_from_recommended": elif prop_name == "inherit_from_recommended":
......
...@@ -15,7 +15,7 @@ def to_file(string): ...@@ -15,7 +15,7 @@ def to_file(string):
return f.name return f.name
# TODO: check purpose of this function... add documentation
def parse_str(string): def parse_str(string):
parse_model_from_yaml(to_file(string)) parse_model_from_yaml(to_file(string))
...@@ -328,3 +328,68 @@ A: ...@@ -328,3 +328,68 @@ A:
with self.assertRaises(YamlDefinitionError) as yde: with self.assertRaises(YamlDefinitionError) as yde:
parse_str(string) parse_str(string)
assert("line {}".format(line) in yde.exception.args[0]) assert("line {}".format(line) in yde.exception.args[0])
def test_define_role():
model = """
A:
role: Record
"""
entities = parse_model_from_string(model)
assert "A" in entities
assert isinstance(entities["A"], db.Record)
assert entities["A"].role == "Record"
model = """
A:
role: Record
inherit_from_obligatory:
- C
obligatory_properties:
b:
b:
datatype: INTEGER
C:
obligatory_properties:
b:
D:
role: RecordType
"""
entities = parse_model_from_string(model)
for l, ent in (("A", "Record"), ("b", "Property"),
("C", "RecordType"), ("D", "RecordType")):
assert l in entities
assert isinstance(entities[l], getattr(db, ent))
assert entities[l].role == ent
assert entities["A"].parents[0].name == "C"
assert entities["A"].name == "A"
assert entities["A"].properties[0].name == "b"
assert entities["A"].properties[0].value is None
assert entities["C"].properties[0].name == "b"
assert entities["C"].properties[0].value is None
model = """
A:
role: Record
obligatory_properties:
b: 42
b:
datatype: INTEGER
"""
entities = parse_model_from_string(model)
assert entities["A"].get_property("b").value == 42
assert entities["b"].value is None
model = """
b:
datatype: INTEGER
value: 18
"""
entities = parse_model_from_string(model)
assert entities["b"].value == 18
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment