[test] Add a "functional" test for dataimport.importer using a CSV data source

This will also be used to illustrate what will be inserted in the book.

Related to #5414753.

authorDenis Laxalde <denis.laxalde@logilab.fr>
changeset227d5bd21359
branchdefault
phasedraft
hiddenyes
parent revision#3f4997c95817 [dataimport] introduce the importer and extentity classes
child revision#83426035fbf4 [doc] Add a tutorial and extend documentation for ExtEntityImporter
files modified by this revision
dataimport/test/data/people.csv
dataimport/test/unittest_importer.py
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1435327767 -7200
# Fri Jun 26 16:09:27 2015 +0200
# Node ID 227d5bd2135989539baebf3394eff1c91d8cc5e7
# Parent 3f4997c958174816192e8a71596a85c6df73acdb
[test] Add a "functional" test for dataimport.importer using a CSV data source

This will also be used to illustrate what will be inserted in the book.

Related to #5414753.

diff --git a/dataimport/test/data/people.csv b/dataimport/test/data/people.csv
@@ -0,0 +1,3 @@
1 +# uri,name,knows
2 +http://www.example.org/alice,Alice,
3 +http://www.example.org/bob,Bob,http://www.example.org/alice
diff --git a/dataimport/test/unittest_importer.py b/dataimport/test/unittest_importer.py
@@ -20,11 +20,11 @@
4 
5  from logilab.common.testlib import unittest_main
6 
7  from cubicweb import ValidationError
8  from cubicweb.devtools.testlib import CubicWebTC
9 -from cubicweb.dataimport import RQLObjectStore
10 +from cubicweb.dataimport import RQLObjectStore, ucsvreader
11  from cubicweb.dataimport.importer import ExtEntity, ExtEntitiesImporter, SimpleImportLog, RelationMapping
12 
13 
14  class RelationMappingTC(CubicWebTC):
15 
@@ -146,7 +146,28 @@
16              self.assertEqual(len(rset), 1)
17              entity = rset.get_entity(0, 0)
18              self.assertEqual(entity.nom, u'Richelieu Cardinal')
19 
20 
21 +def extentities_from_csv(fpath):
22 +    """Yield ExtEntity read from `fpath` CSV file."""
23 +    with open(fpath) as f:
24 +        for uri, name, knows in ucsvreader(f, skipfirst=True, skip_empty=False):
25 +            yield ExtEntity('Personne', uri,
26 +                            {'nom': set([name]), 'connait': set([knows])})
27 +
28 +
29 +class DataimportFunctionalTC(CubicWebTC):
30 +
31 +    def test_csv(self):
32 +        extenties = extentities_from_csv(self.datapath('people.csv'))
33 +        with self.admin_access.repo_cnx() as cnx:
34 +            store = RQLObjectStore(cnx)
35 +            importer = ExtEntitiesImporter(self.schema, store)
36 +            importer.import_entities(extenties)
37 +            cnx.commit()
38 +            rset = cnx.execute('String N WHERE X nom N, X connait Y, Y nom "Alice"')
39 +            self.assertEqual(rset[0][0], u'Bob')
40 +
41 +
42  if __name__ == '__main__':
43      unittest_main()