Extend rule_type_from_etype to support SEDASeqXXXRuleRule etype
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 12 Oct 2016 10:53:17 +0200
changeset 1735 4e36b39d78fa
parent 1734 fff22f766b10
child 1736 e6c7685a6b3b
Extend rule_type_from_etype to support SEDASeqXXXRuleRule etype we get this kind of entity type when forcing display of some inlined creation form (job done in later cset). Add an assertion to make unexpected entity type easier to debug.
entities/__init__.py
test/test_entities.py
--- a/entities/__init__.py	Fri Oct 14 11:17:50 2016 +0200
+++ b/entities/__init__.py	Wed Oct 12 10:53:17 2016 +0200
@@ -99,13 +99,20 @@
 
 
 def rule_type_from_etype(etype):
-    """Return the rule type from an etype like SEDAAccessRule or SEDAAltAccessRulePreventInheritance
+    """Return the rule type (e.g. 'access') from an etype enclosing the information
+    (e.g. 'SEDAAltAccessRulePreventInheritance', 'SEDASeqAaccessRuleRule' or 'SEDAAccessRule')
     """
     if etype.startswith('SEDAAlt'):
         rule_type = etype[len('SEDAAlt'):-len('RulePreventInheritance')]
+    elif etype.startswith('SEDASeq'):
+        rule_type = etype[len('SEDASeq'):-len('RuleRule')]
     else:
         rule_type = etype[len('SEDA'):-len('Rule')]
-    return rule_type.lower()
+    rule_type = rule_type.lower()
+    assert rule_type in ('access', 'appraisal', 'classification',
+                         'reuse', 'dissemination', 'storage'), \
+        'unhandled etype {}'.format(etype)
+    return rule_type
 
 
 class DirectLinkIContained(IContained):
--- a/test/test_entities.py	Fri Oct 14 11:17:50 2016 +0200
+++ b/test/test_entities.py	Wed Oct 12 10:53:17 2016 +0200
@@ -25,7 +25,7 @@
 from cubicweb.devtools.testlib import CubicWebTC
 
 from cubes.seda.entities import (seda_profile_container_def, simplified_profile, full_seda2_profile,
-                                 parent_and_container)
+                                 parent_and_container, rule_type_from_etype)
 
 from testutils import create_archive_unit, create_data_object
 
@@ -155,6 +155,19 @@
             self.assertParent(bdo, au.eid)
 
 
+class RuleFromETypeTC(unittest.TestCase):
+    def test_rule_from_etype(self):
+        for rule_type in ('access', 'appraisal', 'classification',
+                          'reuse', 'dissemination', 'storage'):
+            for prefix, suffix in [
+                    ('SEDAAlt', 'RulePreventInheritance'),
+                    ('SEDASeq', 'RuleRule'),
+                    ('SEDA', 'Rule'),
+            ]:
+                self.assertEqual(rule_type_from_etype(prefix + rule_type.capitalize() + suffix),
+                                 rule_type)
+
+
 class ParentAndContainerTC(CubicWebTC):
 
     def test_nodata(self):