[dataimport] Fix case when extid is text in use_extid_as_cwuri() 3.26
authorjulien tayon <julien.tayon@logilab.fr>
Tue, 21 May 2019 10:50:08 +0200
branch3.26
changeset 12625 ba5231e1aa45
parent 12618 3f125fdbcd70
child 12626 32ee89340e59
child 12630 1c156d9f224a
[dataimport] Fix case when extid is text in use_extid_as_cwuri() Extid can be bytes or text. This avoid AttributeError: 'str' object has no attribute 'decode'
cubicweb/dataimport/importer.py
cubicweb/dataimport/test/unittest_importer.py
--- a/cubicweb/dataimport/importer.py	Thu May 16 14:26:38 2019 +0200
+++ b/cubicweb/dataimport/importer.py	Tue May 21 10:50:08 2019 +0200
@@ -30,6 +30,8 @@
 from collections import defaultdict
 import logging
 
+import six
+
 from logilab.mtconverter import xml_escape
 
 from cubicweb import Binary
@@ -71,7 +73,10 @@
     def use_extid_as_cwuri_filter(extentities):
         for extentity in extentities:
             if extentity.extid not in extid2eid:
-                extentity.values.setdefault('cwuri', set([extentity.extid.decode('utf-8')]))
+                cwuri = extentity.extid
+                if isinstance(cwuri, six.binary_type):
+                    cwuri = cwuri.decode('utf-8')
+                extentity.values.setdefault('cwuri', set([cwuri]))
             yield extentity
     return use_extid_as_cwuri_filter
 
--- a/cubicweb/dataimport/test/unittest_importer.py	Thu May 16 14:26:38 2019 +0200
+++ b/cubicweb/dataimport/test/unittest_importer.py	Tue May 21 10:50:08 2019 +0200
@@ -196,6 +196,12 @@
         personne.values.pop('cwuri')
         list(set_cwuri((personne,)))
         self.assertNotIn('cwuri', personne.values)
+        personne = ExtEntity('Personne', u'ééé', {})
+        mapping = {}
+        set_cwuri = use_extid_as_cwuri(mapping)
+        list(set_cwuri((personne,)))
+        self.assertIn('cwuri', personne.values)
+        self.assertEqual(personne.values['cwuri'], set([u'ééé']))
 
 
 class DropExtraValuesTC(CubicWebTC):