[profile gen] Avoid duplicated format ids / mime types in enum
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 14 Dec 2017 14:34:12 +0100
changeset 2928 c3345b8d9664
parent 2927 a94fa8b3611a
child 2929 352167325f8f
[profile gen] Avoid duplicated format ids / mime types in enum There may be cases where several concepts use the same label, mostly in the case of format identifier but do the same thing for mime types as a safety belt.
cubicweb_seda/entities/profile_generation.py
test/test_profile_generation.py
--- a/cubicweb_seda/entities/profile_generation.py	Thu Dec 14 14:32:29 2017 +0100
+++ b/cubicweb_seda/entities/profile_generation.py	Thu Dec 14 14:34:12 2017 +0100
@@ -891,14 +891,14 @@
 
         format_id = data_object.format_id
         if format_id is not None:
-            format_ids = [_concept_value(concept, self.concepts_language)
-                          for concept in format_id.concepts]
+            format_ids = sorted(set(_concept_value(concept, self.concepts_language)
+                                    for concept in format_id.concepts))
         else:
             format_ids = []
         mime_type = data_object.mime_type
         if mime_type is not None:
-            mime_types = [_concept_value(concept, self.concepts_language)
-                          for concept in mime_type.concepts]
+            mime_types = sorted(set(_concept_value(concept, self.concepts_language)
+                                    for concept in mime_type.concepts))
         else:
             mime_types = []
         encoding = data_object.encoding
--- a/test/test_profile_generation.py	Thu Dec 14 14:32:29 2017 +0100
+++ b/test/test_profile_generation.py	Thu Dec 14 14:34:12 2017 +0100
@@ -1018,6 +1018,24 @@
             self.assertEqual([node.attrib['name'] for node in root],
                              ['ArchiveObject', 'Document'])
 
+    def test_duplicated_format_id(self):
+        with self.admin_access.cnx() as cnx:
+            scheme = testutils.scheme_for_type(cnx, 'file_category', None,
+                                               u'fmt/123', u'fmt/123')
+            concepts = scheme.reverse_in_scheme
+
+            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'my profile',
+                                         simplified_profile=True)
+            bdo = testutils.create_data_object(transfer)
+            bdo.format_id.cw_set(seda_format_id_to=concepts)
+
+            adapter = transfer.cw_adapt_to('SEDA-1.0.rng')
+            root = etree.Element('test-root')
+            adapter.xsd_attachment(root, bdo)
+
+            self.assertEqual([node.text for node in self.xpath(root, '//rng:value')],
+                             ['fmt/123'])
+
 
 class SEDAExportUnitTest(unittest.TestCase):