Skip to content
Snippets Groups Projects
Commit 0f0af32f authored by Michael Thies's avatar Michael Thies
Browse files

Merge branch 'fix/idShort_onchange_collision' into 'master'

model: Check idShort on change for duplicates in parent namespace

Closes #100

See merge request !60
parents fd269bd3 86bd9191
No related branches found
No related tags found
No related merge requests found
Pipeline #348528 passed
......@@ -415,7 +415,7 @@ class Referable(metaclass=abc.ABCMeta):
def __init__(self):
super().__init__()
self.id_short: Optional[str] = ""
self._id_short: Optional[str] = ""
self.category: Optional[str] = ""
self.description: Optional[LangStringSet] = set()
# We use a Python reference to the parent Namespace instead of a Reference Object, as specified. This allows
......@@ -450,8 +450,12 @@ class Referable(metaclass=abc.ABCMeta):
Constraint AASd-002: idShort shall only feature letters, digits, underscore ('_'); starting mandatory with a
letter
Additionally check that the idShort is not already present in the same parent Namespace (if this object is
already contained in a parent Namespace).
:param id_short: Identifying string of the element within its name space
:raises: Exception if the constraint is not fulfilled
:raises ValueError: if the constraint is not fulfilled
:raises KeyError: if the new idShort causes a name collision in the parent Namespace
"""
if id_short is None and not hasattr(self, 'identification'):
......@@ -461,6 +465,13 @@ class Referable(metaclass=abc.ABCMeta):
raise ValueError("The id_short must contain only letters, digits and underscore")
if not re.match("^([a-zA-Z].*|)$", test_id_short):
raise ValueError("The id_short must start with a letter")
if self.parent is not None and id_short != self.id_short:
for set_ in self.parent.namespace_element_sets:
if id_short in set_:
raise KeyError("Referable with id_short '{}' is already present in the parent Namespace"
.format(id_short))
self._id_short = id_short
def update(self, timeout: float = 0) -> None:
......
......@@ -184,6 +184,17 @@ class ModelNamespaceTest(unittest.TestCase):
namespace.get_referable("Prop3")
self.assertEqual("'Referable with id_short Prop3 not found in this namespace'", str(cm.exception))
def test_renaming(self) -> None:
self.namespace.set1.add(self.prop1)
self.namespace.set1.add(self.prop2)
self.prop1.id_short = "Prop3"
self.assertEqual("Prop3", self.prop1.id_short)
with self.assertRaises(KeyError) as cm:
self.prop1.id_short = "Prop2"
self.assertIn("already present", str(cm.exception))
def test_Namespaceset_update_from(self) -> None:
# Prop1 is getting its value updated by namespace2.set1
# Prop2 is getting deleted since it does not exist in namespace2.set1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment