[profile gen] Add a special case for path through data_object_reference_id
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 11 Jan 2017 09:06:18 +0100
changeset 2296 5f607c9702cb
parent 2295 8433dcfc7419
child 2297 ff04117be4a2
[profile gen] Add a special case for path through data_object_reference_id This relation is explicitly marked as composite in shema/__init__.py to ease the case of simplified profiles, where we want data objects to appear as children of archive unit instead of references as expected by SEDA 2. Doing this, we introduced a bug in SEDA 2 profile generation where the target value (hence the referenced data object) was also considered as the 'cardinality entity' (the one defining the cardinality / annotation) for the associated XML element. This led to using the data object's cardinality / annotation for the *reference* element.
entities/profile_generation.py
test/test_profile_generation.py
--- a/entities/profile_generation.py	Fri Jan 13 11:09:39 2017 +0100
+++ b/entities/profile_generation.py	Wed Jan 11 09:06:18 2017 +0100
@@ -1520,7 +1520,9 @@
     targets = entity.related(rtype, role, entities=True)
     rschema = entity._cw.vreg.schema.rschema
     rdefschema = next(iter(rschema(rtype).rdefs.values()))
-    if rdefschema.composite:
+    # data_object_reference_id is artificially composite to ease the case of simplified profile
+    # (set explicitly in schema/__init__.py)
+    if rtype != 'seda_data_object_reference_id' and rdefschema.composite:
         return [(target, target) for target in targets]
     elif targets:
         return [(None, target) for target in targets]
--- a/test/test_profile_generation.py	Fri Jan 13 11:09:39 2017 +0100
+++ b/test/test_profile_generation.py	Wed Jan 11 09:06:18 2017 +0100
@@ -335,6 +335,36 @@
             self.assertEqual(target_value[0].eid, restr_end_date.eid)
             self.assertEqual(target_value[1], None)
 
+    def test_internal_reference(self):
+        element_defs = iter(XSDMMapping('DataObjectReference'))
+        with self.admin_access.client_cnx() as cnx:
+            create = cnx.create_entity
+            transfer = create('SEDAArchiveTransfer', title=u'test profile')
+            unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(
+                transfer)
+            bdo = cnx.create_entity('SEDABinaryDataObject',
+                                    user_annotation=u'I am mandatory',
+                                    seda_binary_data_object=transfer)
+            ref = create('SEDADataObjectReference', seda_data_object_reference=unit_alt_seq,
+                         seda_data_object_reference_id=bdo)
+
+            edef = next(element_defs)
+            # readable_edef(edef)
+            # ('DataObjectReference',
+            #  'SEDADataObjectReference',
+            #  [('id', [('seda_id', 'object', 'SEDAid'), ('id', 'subject', 'String')]),
+            #   ('DataObjectReferenceId',
+            #    [('seda_data_object_reference_id',
+            #      'subject',
+            #      ('SEDABinaryDataObject', 'SEDAPhysicalDataObject'))])])
+
+            path = edef[-1][1][1]
+            target_values = _path_target_values(ref, path)
+            self.assertEqual(len(target_values), 1)
+            target_value = target_values[0]
+            self.assertEqual(target_value[0], None)
+            self.assertEqual(target_value[1].eid, bdo.eid)
+
 
 class SEDA2ExportTCMixIn(object):