[entities] Make _climb_rule_holders working on SEDASeqAltArchiveUnitArchiveUnitRefIdManagement entities
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 17 Mar 2017 09:55:52 +0100
changeset 2514 c18e4cdffd64
parent 2513 660611c16bb1
child 2515 bfe6130e62b5
[entities] Make _climb_rule_holders working on SEDASeqAltArchiveUnitArchiveUnitRefIdManagement entities This will be useful in later cset to add inherited_rule method on this entity type.
cubicweb_seda/entities/custom.py
test/test_entities.py
--- a/cubicweb_seda/entities/custom.py	Fri Mar 17 08:58:37 2017 +0100
+++ b/cubicweb_seda/entities/custom.py	Fri Mar 17 09:55:52 2017 +0100
@@ -15,7 +15,7 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
 from ..xsd2yams import RULE_TYPES
-from . import generated
+from . import generated, itree
 
 
 def _extract_title(annotation):
@@ -30,7 +30,10 @@
     management rule until the root (transfer) is reached.
     """
     while transfer_or_archive_unit is not None:
-        if transfer_or_archive_unit.cw_etype == 'SEDAArchiveTransfer':
+        if transfer_or_archive_unit.cw_etype == 'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement':
+            yield transfer_or_archive_unit
+            transfer_or_archive_unit = itree.parent_archive_unit(transfer_or_archive_unit)
+        elif transfer_or_archive_unit.cw_etype == 'SEDAArchiveTransfer':
             yield transfer_or_archive_unit
         else:
             yield transfer_or_archive_unit.first_level_choice.content_sequence
--- a/test/test_entities.py	Fri Mar 17 08:58:37 2017 +0100
+++ b/test/test_entities.py	Fri Mar 17 09:55:52 2017 +0100
@@ -26,7 +26,7 @@
 
 from cubicweb_seda.entities import (seda_profile_container_def, simplified_profile,
                                     full_seda2_profile, parent_and_container,
-                                    rule_type_from_etype)
+                                    rule_type_from_etype, custom)
 
 from testutils import create_archive_unit, create_data_object
 
@@ -298,6 +298,24 @@
                 ent = cnx.create_entity(etype, user_annotation=u'bla bla\nbli bli blo\n')
                 self.assertEqual(ent.dc_title(), u'bla bla')
 
+    def test_climb_rule_holders(self):
+        with self.admin_access.client_cnx() as cnx:
+            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            unit, _, unit_alt_seq = create_archive_unit(transfer)
+            subunit, _, subunit_alt_seq = create_archive_unit(unit_alt_seq)
+            cnx.commit()
+
+            self.assertEqual(list(custom._climb_rule_holders(subunit_alt_seq)),
+                             [subunit_alt_seq, unit_alt_seq, transfer])
+            self.assertEqual(list(custom._climb_rule_holders(subunit)),
+                             [subunit_alt_seq, unit_alt_seq, transfer])
+            self.assertEqual(list(custom._climb_rule_holders(unit_alt_seq)),
+                             [unit_alt_seq, transfer])
+            self.assertEqual(list(custom._climb_rule_holders(unit)),
+                             [unit_alt_seq, transfer])
+            self.assertEqual(list(custom._climb_rule_holders(transfer)),
+                             [transfer])
+
 
 if __name__ == '__main__':
     unittest.main()