18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 """unittest for cubicweb.dataimport.pgstore""" |
19 """unittest for cubicweb.dataimport.pgstore""" |
20 |
20 |
21 import datetime as DT |
21 import datetime as DT |
22 |
22 |
23 from six import PY3 |
|
24 from logilab.common.testlib import TestCase, unittest_main |
23 from logilab.common.testlib import TestCase, unittest_main |
25 |
24 |
26 from cubicweb.dataimport import pgstore |
25 from cubicweb.dataimport import pgstore |
27 |
|
28 |
|
29 if PY3: |
|
30 long = int |
|
31 |
26 |
32 |
27 |
33 class CreateCopyFromBufferTC(TestCase): |
28 class CreateCopyFromBufferTC(TestCase): |
34 |
29 |
35 # test converters |
30 # test converters |
39 self.assertEqual(u'NULL', cnvt(None)) |
34 self.assertEqual(u'NULL', cnvt(None)) |
40 |
35 |
41 def test_convert_number(self): |
36 def test_convert_number(self): |
42 cnvt = pgstore._copyfrom_buffer_convert_number |
37 cnvt = pgstore._copyfrom_buffer_convert_number |
43 self.assertEqual(u'42', cnvt(42)) |
38 self.assertEqual(u'42', cnvt(42)) |
44 self.assertEqual(u'42', cnvt(long(42))) |
39 self.assertEqual(u'42', cnvt(int(42))) |
45 self.assertEqual(u'42.42', cnvt(42.42)) |
40 self.assertEqual(u'42.42', cnvt(42.42)) |
46 |
41 |
47 def test_convert_string(self): |
42 def test_convert_string(self): |
48 cnvt = pgstore._copyfrom_buffer_convert_string |
43 cnvt = pgstore._copyfrom_buffer_convert_string |
49 # simple |
44 # simple |
66 cnvt = pgstore._copyfrom_buffer_convert_datetime |
61 cnvt = pgstore._copyfrom_buffer_convert_datetime |
67 self.assertEqual('0666-06-13 06:06:06.000000', cnvt(DT.datetime(666, 6, 13, 6, 6, 6))) |
62 self.assertEqual('0666-06-13 06:06:06.000000', cnvt(DT.datetime(666, 6, 13, 6, 6, 6))) |
68 |
63 |
69 # test buffer |
64 # test buffer |
70 def test_create_copyfrom_buffer_tuple(self): |
65 def test_create_copyfrom_buffer_tuple(self): |
71 data = ((42, long(42), 42.42, u'éléphant', DT.date(666, 1, 13), DT.time(6, 6, 6), |
66 data = ((42, int(42), 42.42, u'éléphant', DT.date(666, 1, 13), DT.time(6, 6, 6), |
72 DT.datetime(666, 6, 13, 6, 6, 6)), |
67 DT.datetime(666, 6, 13, 6, 6, 6)), |
73 (6, long(6), 6.6, u'babar', DT.date(2014, 1, 14), DT.time(4, 2, 1), |
68 (6, int(6), 6.6, u'babar', DT.date(2014, 1, 14), DT.time(4, 2, 1), |
74 DT.datetime(2014, 1, 1, 0, 0, 0))) |
69 DT.datetime(2014, 1, 1, 0, 0, 0))) |
75 results = pgstore._create_copyfrom_buffer(data) |
70 results = pgstore._create_copyfrom_buffer(data) |
76 # all columns |
71 # all columns |
77 expected = u'''42\t42\t42.42\téléphant\t0666-01-13\t06:06:06.000000\t0666-06-13 06:06:06.000000 |
72 expected = u'''42\t42\t42.42\téléphant\t0666-01-13\t06:06:06.000000\t0666-06-13 06:06:06.000000 |
78 6\t6\t6.6\tbabar\t2014-01-14\t04:02:01.000000\t2014-01-01 00:00:00.000000''' |
73 6\t6\t6.6\tbabar\t2014-01-14\t04:02:01.000000\t2014-01-01 00:00:00.000000''' |