Skip to content
Snippets Groups Projects
Verified Commit b9ee004d authored by Timm Fitschen's avatar Timm Fitschen
Browse files

BUG: Entity.role behavior

parent ea5763be
No related branches found
No related tags found
2 merge requests!33MAINT: change arguments of create_user,!15F entity role
Pipeline #9723 passed
...@@ -128,7 +128,24 @@ class Entity(object): ...@@ -128,7 +128,24 @@ class Entity(object):
@property @property
def role(self): 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 return self.__role
if type(self) is not Entity:
return type(self).__name__
@role.setter @role.setter
def role(self, role): def role(self, role):
...@@ -832,6 +849,9 @@ class Entity(object): ...@@ -832,6 +849,9 @@ class Entity(object):
if xml is None: if xml is None:
xml = etree.Element("Entity") xml = etree.Element("Entity")
# use role as xml tag name, fall-back to "Entity"
elem_tag = "Entity" if self.role is None else self.role
xml = etree.Element(elem_tag)
assert isinstance(xml, etree._Element) assert isinstance(xml, etree._Element)
# unwrap wrapped entity # unwrap wrapped entity
...@@ -2277,6 +2297,7 @@ class _Messages(dict): ...@@ -2277,6 +2297,7 @@ class _Messages(dict):
def _basic_sync(e_local, e_remote): def _basic_sync(e_local, e_remote):
if e_local is None or e_remote is None: if e_local is None or e_remote is None:
return None return None
e_local.role = e_remote.role
e_local.id = e_remote.id e_local.id = e_remote.id
e_local.name = e_remote.name e_local.name = e_remote.name
e_local.description = e_remote.description e_local.description = e_remote.description
......
...@@ -41,18 +41,33 @@ class TestEntity(unittest.TestCase): ...@@ -41,18 +41,33 @@ class TestEntity(unittest.TestCase):
def test_instance_variables(self): def test_instance_variables(self):
entity = Entity() entity = Entity()
self.assertTrue(hasattr(entity, "role"))
self.assertTrue(hasattr(entity, "id")) self.assertTrue(hasattr(entity, "id"))
self.assertTrue(hasattr(entity, "name")) self.assertTrue(hasattr(entity, "name"))
self.assertTrue(hasattr(entity, "description")) self.assertTrue(hasattr(entity, "description"))
self.assertTrue(hasattr(entity, "parents")) self.assertTrue(hasattr(entity, "parents"))
self.assertTrue(hasattr(entity, "properties")) self.assertTrue(hasattr(entity, "properties"))
def test_role(self): def test_entity_role_1(self):
entity = Entity(role="TestRole") entity = Entity(role="TestRole")
self.assertEqual(entity.role, "TestRole") self.assertEqual(entity.role, "TestRole")
entity.role = "TestRole2" entity.role = "TestRole2"
self.assertEqual(entity.role, "TestRole2") self.assertEqual(entity.role, "TestRole2")
def test_entity_role_2(self):
entity = Entity()
self.assertIsNone(entity.role)
self.assertEqual(entity.to_xml().tag, "Entity")
entity.role = "Record"
self.assertEqual(entity.role, "Record")
self.assertEqual(entity.to_xml().tag, "Record")
def test_recordtype_role(self):
entity = RecordType()
self.assertEqual(entity.role, "RecordType")
self.assertEqual(entity.to_xml().tag, "RecordType")
def test_instanciation(self): def test_instanciation(self):
self.assertRaises(Exception, Entity()) self.assertRaises(Exception, Entity())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment