|
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 """unittest for cubicweb.dataimport.pgstore""" |
|
20 |
|
21 import datetime as DT |
|
22 |
|
23 from logilab.common.testlib import TestCase, unittest_main |
|
24 |
|
25 from cubicweb.dataimport import pgstore |
|
26 |
|
27 |
|
28 class CreateCopyFromBufferTC(TestCase): |
|
29 |
|
30 # test converters |
|
31 |
|
32 def test_convert_none(self): |
|
33 cnvt = pgstore._copyfrom_buffer_convert_None |
|
34 self.assertEqual('NULL', cnvt(None)) |
|
35 |
|
36 def test_convert_number(self): |
|
37 cnvt = pgstore._copyfrom_buffer_convert_number |
|
38 self.assertEqual('42', cnvt(42)) |
|
39 self.assertEqual('42', cnvt(42L)) |
|
40 self.assertEqual('42.42', cnvt(42.42)) |
|
41 |
|
42 def test_convert_string(self): |
|
43 cnvt = pgstore._copyfrom_buffer_convert_string |
|
44 # simple |
|
45 self.assertEqual('babar', cnvt('babar')) |
|
46 # unicode |
|
47 self.assertEqual('\xc3\xa9l\xc3\xa9phant', cnvt(u'éléphant')) |
|
48 self.assertEqual('\xe9l\xe9phant', cnvt(u'éléphant', encoding='latin1')) |
|
49 # escaping |
|
50 self.assertEqual('babar\\tceleste\\n', cnvt('babar\tceleste\n')) |
|
51 self.assertEqual(r'C:\\new\tC:\\test', cnvt('C:\\new\tC:\\test')) |
|
52 |
|
53 def test_convert_date(self): |
|
54 cnvt = pgstore._copyfrom_buffer_convert_date |
|
55 self.assertEqual('0666-01-13', cnvt(DT.date(666, 1, 13))) |
|
56 |
|
57 def test_convert_time(self): |
|
58 cnvt = pgstore._copyfrom_buffer_convert_time |
|
59 self.assertEqual('06:06:06.000100', cnvt(DT.time(6, 6, 6, 100))) |
|
60 |
|
61 def test_convert_datetime(self): |
|
62 cnvt = pgstore._copyfrom_buffer_convert_datetime |
|
63 self.assertEqual('0666-06-13 06:06:06.000000', cnvt(DT.datetime(666, 6, 13, 6, 6, 6))) |
|
64 |
|
65 # test buffer |
|
66 def test_create_copyfrom_buffer_tuple(self): |
|
67 data = ((42, 42L, 42.42, u'éléphant', DT.date(666, 1, 13), DT.time(6, 6, 6), |
|
68 DT.datetime(666, 6, 13, 6, 6, 6)), |
|
69 (6, 6L, 6.6, u'babar', DT.date(2014, 1, 14), DT.time(4, 2, 1), |
|
70 DT.datetime(2014, 1, 1, 0, 0, 0))) |
|
71 results = pgstore._create_copyfrom_buffer(data) |
|
72 # all columns |
|
73 expected = '''42\t42\t42.42\téléphant\t0666-01-13\t06:06:06.000000\t0666-06-13 06:06:06.000000 |
|
74 6\t6\t6.6\tbabar\t2014-01-14\t04:02:01.000000\t2014-01-01 00:00:00.000000''' |
|
75 self.assertMultiLineEqual(expected, results.getvalue()) |
|
76 # selected columns |
|
77 results = pgstore._create_copyfrom_buffer(data, columns=(1, 3, 6)) |
|
78 expected = '''42\téléphant\t0666-06-13 06:06:06.000000 |
|
79 6\tbabar\t2014-01-01 00:00:00.000000''' |
|
80 self.assertMultiLineEqual(expected, results.getvalue()) |
|
81 |
|
82 def test_create_copyfrom_buffer_dict(self): |
|
83 data = (dict(integer=42, double=42.42, text=u'éléphant', |
|
84 date=DT.datetime(666, 6, 13, 6, 6, 6)), |
|
85 dict(integer=6, double=6.6, text=u'babar', |
|
86 date=DT.datetime(2014, 1, 1, 0, 0, 0))) |
|
87 results = pgstore._create_copyfrom_buffer(data, ('integer', 'text')) |
|
88 expected = '''42\téléphant\n6\tbabar''' |
|
89 self.assertMultiLineEqual(expected, results.getvalue()) |
|
90 |
|
91 if __name__ == '__main__': |
|
92 unittest_main() |