[dataimport] Turn dataimport.py into a package.

Import public methods into __init__.py. Import private methods into test/unittest_dataimport.py

authorLaura Médioni <laura.medioni@logilab.fr>
changeset92250c430b42
branchdefault
phasedraft
hiddenyes
parent revision#52a976c5d27a [connection] provide some missing documentation bits
child revision#941fbba3c721 [dataimport] Backport skos.dataimport to dataimport.importer
files modified by this revision
dataimport.py
dataimport/__init__.py
dataimport/dataimport.py
test/unittest_dataimport.py
# HG changeset patch
# User Laura Médioni <laura.medioni@logilab.fr>
# Date 1429866499 -7200
# Fri Apr 24 11:08:19 2015 +0200
# Node ID 92250c430b42f81553166f28a79adc43897b6ec5
# Parent 52a976c5d27a7c75f0146f55db357cc85f5aeef3
[dataimport] Turn dataimport.py into a package.

Import public methods into __init__.py.
Import private methods into test/unittest_dataimport.py

diff --git a/dataimport/__init__.py b/dataimport/__init__.py
@@ -0,0 +1,31 @@
1 +# -*- coding: utf-8 -*-
2 +# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3 +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4 +#
5 +# This file is part of CubicWeb.
6 +#
7 +# CubicWeb is free software: you can redistribute it and/or modify it under the
8 +# terms of the GNU Lesser General Public License as published by the Free
9 +# Software Foundation, either version 2.1 of the License, or (at your option)
10 +# any later version.
11 +#
12 +# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
13 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 +# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
15 +# details.
16 +#
17 +# You should have received a copy of the GNU Lesser General Public License along
18 +# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
19 +
20 +"""Dataimport package
21 +
22 +This package provides tools to import strutured data.
23 +"""
24 +
25 +from cubicweb.dataimport.dataimport import (count_lines, ucsvreader_pb, ucsvreader, callfunc_every,
26 +                                            lazytable, lazydbtable, mk_entity, tell, confirm,
27 +                                            catch_error, optional, required, todatetime,
28 +                                            call_transform_method, check_doubles,
29 +                                            check_doubles_not_none, ObjectStore, RQLObjectStore,
30 +                                            CWImportController, NoHookRQLObjectStore,
31 +                                            MetaGenerator, SQLGenObjectStore, SQLGenSourceWrapper)
diff --git a/dataimport.py b/dataimport/dataimport.py
diff --git a/test/unittest_dataimport.py b/test/unittest_dataimport.py
@@ -4,10 +4,17 @@
32  from StringIO import StringIO
33 
34  from logilab.common.testlib import TestCase, unittest_main
35 
36  from cubicweb import dataimport
37 +from cubicweb.dataimport.dataimport import (_create_copyfrom_buffer,
38 +                                            _copyfrom_buffer_convert_None,
39 +                                            _copyfrom_buffer_convert_number,
40 +                                            _copyfrom_buffer_convert_string,
41 +                                            _copyfrom_buffer_convert_time,
42 +                                            _copyfrom_buffer_convert_datetime,
43 +                                            _copyfrom_buffer_convert_date)
44  from cubicweb.devtools.testlib import CubicWebTC
45 
46 
47  class RQLObjectStoreTC(CubicWebTC):
48 
@@ -31,62 +38,62 @@
49  class CreateCopyFromBufferTC(TestCase):
50 
51      # test converters
52 
53      def test_convert_none(self):
54 -        cnvt = dataimport._copyfrom_buffer_convert_None
55 +        cnvt = _copyfrom_buffer_convert_None
56          self.assertEqual('NULL', cnvt(None))
57 
58      def test_convert_number(self):
59 -        cnvt = dataimport._copyfrom_buffer_convert_number
60 +        cnvt = _copyfrom_buffer_convert_number
61          self.assertEqual('42', cnvt(42))
62          self.assertEqual('42', cnvt(42L))
63          self.assertEqual('42.42', cnvt(42.42))
64 
65      def test_convert_string(self):
66 -        cnvt = dataimport._copyfrom_buffer_convert_string
67 +        cnvt = _copyfrom_buffer_convert_string
68          # simple
69          self.assertEqual('babar', cnvt('babar'))
70          # unicode
71          self.assertEqual('\xc3\xa9l\xc3\xa9phant', cnvt(u'éléphant'))
72          self.assertEqual('\xe9l\xe9phant', cnvt(u'éléphant', encoding='latin1'))
73          self.assertEqual('babar#', cnvt('babar\t', replace_sep='#'))
74          self.assertRaises(ValueError, cnvt, 'babar\t')
75 
76      def test_convert_date(self):
77 -        cnvt = dataimport._copyfrom_buffer_convert_date
78 +        cnvt = _copyfrom_buffer_convert_date
79          self.assertEqual('0666-01-13', cnvt(DT.date(666, 1, 13)))
80 
81      def test_convert_time(self):
82 -        cnvt = dataimport._copyfrom_buffer_convert_time
83 +        cnvt = _copyfrom_buffer_convert_time
84          self.assertEqual('06:06:06.000100', cnvt(DT.time(6, 6, 6, 100)))
85 
86      def test_convert_datetime(self):
87 -        cnvt = dataimport._copyfrom_buffer_convert_datetime
88 +        cnvt = _copyfrom_buffer_convert_datetime
89          self.assertEqual('0666-06-13 06:06:06.000000', cnvt(DT.datetime(666, 6, 13, 6, 6, 6)))
90 
91      # test buffer
92      def test_create_copyfrom_buffer_tuple(self):
93 -        cnvt = dataimport._create_copyfrom_buffer
94 +        cnvt = _create_copyfrom_buffer
95          data = ((42, 42L, 42.42, u'éléphant', DT.date(666, 1, 13), DT.time(6, 6, 6), DT.datetime(666, 6, 13, 6, 6, 6)),
96                  (6, 6L, 6.6, u'babar', DT.date(2014, 1, 14), DT.time(4, 2, 1), DT.datetime(2014, 1, 1, 0, 0, 0)))
97 -        results = dataimport._create_copyfrom_buffer(data)
98 +        results = _create_copyfrom_buffer(data)
99          # all columns
100          expected = '''42\t42\t42.42\téléphant\t0666-01-13\t06:06:06.000000\t0666-06-13 06:06:06.000000
101  6\t6\t6.6\tbabar\t2014-01-14\t04:02:01.000000\t2014-01-01 00:00:00.000000'''
102          self.assertMultiLineEqual(expected, results.getvalue())
103          # selected columns
104 -        results = dataimport._create_copyfrom_buffer(data, columns=(1, 3, 6))
105 +        results = _create_copyfrom_buffer(data, columns=(1, 3, 6))
106          expected = '''42\téléphant\t0666-06-13 06:06:06.000000
107  6\tbabar\t2014-01-01 00:00:00.000000'''
108          self.assertMultiLineEqual(expected, results.getvalue())
109 
110      def test_create_copyfrom_buffer_dict(self):
111 -        cnvt = dataimport._create_copyfrom_buffer
112 +        cnvt = _create_copyfrom_buffer
113          data = (dict(integer=42, double=42.42, text=u'éléphant', date=DT.datetime(666, 6, 13, 6, 6, 6)),
114                  dict(integer=6, double=6.6, text=u'babar', date=DT.datetime(2014, 1, 1, 0, 0, 0)))
115 -        results = dataimport._create_copyfrom_buffer(data, ('integer', 'text'))
116 +        results = _create_copyfrom_buffer(data, ('integer', 'text'))
117          expected = '''42\téléphant\n6\tbabar'''
118          self.assertMultiLineEqual(expected, results.getvalue())
119 
120 
121  class UcsvreaderTC(TestCase):