diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index ed4815ffa3e9c1e26bbf4ed0b197f38acb5b0e80..3966ac69ff3ca9b2baa4956746b809b2d68ce510 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -128,24 +128,7 @@ class Entity(object): @property def role(self): - """Return the role of this entity. - - Particularly, this means - 1. return the explicitly set role, if present. - 2. or, if this is a direct instance of the Entity class (and not of - any subclass of Entity) which hence has no "natural" role, return - None. - 3. Otherwise, return the class name of this entity, because this the - natural role of this object. - Returns - ------- - str - The entities role or `None` - """ - if self.__role: - return self.__role - if type(self) is not Entity: - return type(self).__name__ + return self.__role @role.setter def role(self, role): @@ -969,6 +952,8 @@ class Entity(object): @param elem: the xml element """ + if type(entity) is Entity: + entity.role = elem.tag entity._cuid = elem.get("cuid") entity.id = elem.get("id") # @ReservedAssignment entity.name = elem.get("name") @@ -2297,7 +2282,8 @@ class _Messages(dict): def _basic_sync(e_local, e_remote): if e_local is None or e_remote is None: return None - e_local.role = e_remote.role + if type(e_local) is Entity: + e_local.role = e_remote.role e_local.id = e_remote.id e_local.name = e_remote.name e_local.description = e_remote.description diff --git a/unittests/test_entity.py b/unittests/test_entity.py index 24ebb9196b90e589f8d5d0799c6b7cd74d26caeb..0195f24b93717dd5a8a55c2a590179dc287abda8 100644 --- a/unittests/test_entity.py +++ b/unittests/test_entity.py @@ -24,6 +24,7 @@ """Tests for the Entity class.""" # pylint: disable=missing-docstring import unittest +from lxml import etree from caosdb import (INTEGER, Entity, Property, Record, RecordType, configure_connection) @@ -71,3 +72,15 @@ class TestEntity(unittest.TestCase): def test_instanciation(self): self.assertRaises(Exception, Entity()) + + def test_parse_role(self): + """During parsing, the role of an entity is set explicitely. All other + classes use the class name as a "natural" value for the role property. + """ + parser = etree.XMLParser(remove_comments=True) + entity = Entity._from_xml(Entity(), + etree.parse("unittests/test_record.xml", + parser).getroot()) + + self.assertEqual(entity.role, "Record") + self.assertEqual(getattr(entity, "_Entity__role"), "Record")