[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'
--- 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):