dataimport/test/unittest_importer.py
changeset 10514 b29d9904482e
parent 10460 d260722f2453
child 10807 bb0c7dbd1fe7
equal deleted inserted replaced
10513:7bec01a59f92 10514:b29d9904482e
    16 # with this program. If not, see <http://www.gnu.org/licenses/>.
    16 # with this program. If not, see <http://www.gnu.org/licenses/>.
    17 """Tests for cubicweb.dataimport.importer"""
    17 """Tests for cubicweb.dataimport.importer"""
    18 
    18 
    19 from collections import defaultdict
    19 from collections import defaultdict
    20 
    20 
    21 from logilab.common.testlib import unittest_main
    21 from logilab.common.testlib import TestCase, unittest_main
    22 
    22 
    23 from cubicweb import ValidationError
    23 from cubicweb import ValidationError
    24 from cubicweb.devtools.testlib import CubicWebTC
    24 from cubicweb.devtools.testlib import CubicWebTC
    25 from cubicweb.dataimport import RQLObjectStore, ucsvreader
    25 from cubicweb.dataimport import RQLObjectStore, ucsvreader
    26 from cubicweb.dataimport.importer import ExtEntity, ExtEntitiesImporter, SimpleImportLog, RelationMapping
    26 from cubicweb.dataimport.importer import (ExtEntity, ExtEntitiesImporter, SimpleImportLog,
       
    27                                           RelationMapping, use_extid_as_cwuri)
    27 
    28 
    28 
    29 
    29 class RelationMappingTC(CubicWebTC):
    30 class RelationMappingTC(CubicWebTC):
    30 
    31 
    31     def test_nosource(self):
    32     def test_nosource(self):
   146             self.assertEqual(len(rset), 1)
   147             self.assertEqual(len(rset), 1)
   147             entity = rset.get_entity(0, 0)
   148             entity = rset.get_entity(0, 0)
   148             self.assertEqual(entity.nom, u'Richelieu Cardinal')
   149             self.assertEqual(entity.nom, u'Richelieu Cardinal')
   149 
   150 
   150 
   151 
       
   152 class UseExtidAsCwuriTC(TestCase):
       
   153 
       
   154     def test(self):
       
   155         personne = ExtEntity('Personne', 1, {'nom': set([u'de la lune']),
       
   156                                              'prenom': set([u'Jean'])})
       
   157         mapping = {}
       
   158         set_cwuri = use_extid_as_cwuri(mapping)
       
   159         list(set_cwuri((personne,)))
       
   160         self.assertIn('cwuri', personne.values)
       
   161         self.assertEqual(personne.values['cwuri'], set(['1']))
       
   162         mapping[1] = 'whatever'
       
   163         personne.values.pop('cwuri')
       
   164         list(set_cwuri((personne,)))
       
   165         self.assertNotIn('cwuri', personne.values)
       
   166 
       
   167 
   151 def extentities_from_csv(fpath):
   168 def extentities_from_csv(fpath):
   152     """Yield ExtEntity read from `fpath` CSV file."""
   169     """Yield ExtEntity read from `fpath` CSV file."""
   153     with open(fpath) as f:
   170     with open(fpath) as f:
   154         for uri, name, knows in ucsvreader(f, skipfirst=True, skip_empty=False):
   171         for uri, name, knows in ucsvreader(f, skipfirst=True, skip_empty=False):
   155             yield ExtEntity('Personne', uri,
   172             yield ExtEntity('Personne', uri,