dataimport/test/test_pgstore.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 21 Jun 2016 15:32:51 +0200
changeset 11298 b1e7de000536
parent 10943 1079d68130e1
permissions -rw-r--r--
[pkg] Version 3.22.3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
# coding: utf-8
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
#
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
# This file is part of CubicWeb.
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
#
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
# any later version.
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
#
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
# details.
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
#
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
"""unittest for cubicweb.dataimport.pgstore"""
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
import datetime as DT
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
10591
8e46ed1a0b8a [py3k] octals and long
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10513
diff changeset
    23
from six import PY2
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
from logilab.common.testlib import TestCase, unittest_main
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
from cubicweb.dataimport import pgstore
10943
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
    27
from cubicweb.devtools import testlib
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
class CreateCopyFromBufferTC(TestCase):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
    # test converters
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
    def test_convert_none(self):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
        cnvt = pgstore._copyfrom_buffer_convert_None
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    36
        self.assertEqual(u'NULL', cnvt(None))
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
    def test_convert_number(self):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        cnvt = pgstore._copyfrom_buffer_convert_number
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    40
        self.assertEqual(u'42', cnvt(42))
10591
8e46ed1a0b8a [py3k] octals and long
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10513
diff changeset
    41
        if PY2:
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    42
            self.assertEqual(u'42', cnvt(long(42)))
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    43
        self.assertEqual(u'42.42', cnvt(42.42))
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
    def test_convert_string(self):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
        cnvt = pgstore._copyfrom_buffer_convert_string
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
        # simple
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    48
        self.assertEqual(u'babar', cnvt('babar'))
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
        # unicode
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    50
        self.assertEqual(u'éléphant', cnvt(u'éléphant'))
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
        # escaping
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    52
        self.assertEqual(u'babar\\tceleste\\n', cnvt(u'babar\tceleste\n'))
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    53
        self.assertEqual(u'C:\\\\new\\tC:\\\\test', cnvt(u'C:\\new\tC:\\test'))
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
    def test_convert_date(self):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
        cnvt = pgstore._copyfrom_buffer_convert_date
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
        self.assertEqual('0666-01-13', cnvt(DT.date(666, 1, 13)))
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
    def test_convert_time(self):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
        cnvt = pgstore._copyfrom_buffer_convert_time
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
        self.assertEqual('06:06:06.000100', cnvt(DT.time(6, 6, 6, 100)))
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
    def test_convert_datetime(self):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
        cnvt = pgstore._copyfrom_buffer_convert_datetime
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
        self.assertEqual('0666-06-13 06:06:06.000000', cnvt(DT.datetime(666, 6, 13, 6, 6, 6)))
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
    # test buffer
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
    def test_create_copyfrom_buffer_tuple(self):
10591
8e46ed1a0b8a [py3k] octals and long
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10513
diff changeset
    69
        l = long if PY2 else int
8e46ed1a0b8a [py3k] octals and long
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10513
diff changeset
    70
        data = ((42, l(42), 42.42, u'éléphant', DT.date(666, 1, 13), DT.time(6, 6, 6),
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
                 DT.datetime(666, 6, 13, 6, 6, 6)),
10591
8e46ed1a0b8a [py3k] octals and long
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10513
diff changeset
    72
                (6, l(6), 6.6, u'babar', DT.date(2014, 1, 14), DT.time(4, 2, 1),
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    73
                 DT.datetime(2014, 1, 1, 0, 0, 0)))
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
        results = pgstore._create_copyfrom_buffer(data)
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
        # all columns
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    76
        expected = u'''42\t42\t42.42\téléphant\t0666-01-13\t06:06:06.000000\t0666-06-13 06:06:06.000000
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
6\t6\t6.6\tbabar\t2014-01-14\t04:02:01.000000\t2014-01-01 00:00:00.000000'''
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
        self.assertMultiLineEqual(expected, results.getvalue())
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
        # selected columns
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
        results = pgstore._create_copyfrom_buffer(data, columns=(1, 3, 6))
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    81
        expected = u'''42\téléphant\t0666-06-13 06:06:06.000000
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
6\tbabar\t2014-01-01 00:00:00.000000'''
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
        self.assertMultiLineEqual(expected, results.getvalue())
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
    def test_create_copyfrom_buffer_dict(self):
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
        data = (dict(integer=42, double=42.42, text=u'éléphant',
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
                     date=DT.datetime(666, 6, 13, 6, 6, 6)),
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
                dict(integer=6, double=6.6, text=u'babar',
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
                     date=DT.datetime(2014, 1, 1, 0, 0, 0)))
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
        results = pgstore._create_copyfrom_buffer(data, ('integer', 'text'))
10810
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    91
        expected = u'''42\téléphant\n6\tbabar'''
0768bf2333a7 [dataimport] give unicode objects to psycopg2 copy_from
Julien Cristau <julien.cristau@logilab.fr>
parents: 10591
diff changeset
    92
        self.assertEqual(expected, results.getvalue())
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
10943
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
    94
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
    95
class SQLGenObjectStoreTC(testlib.CubicWebTC):
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
    96
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
    97
    def test_prepare_insert_entity(self):
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
    98
        with self.admin_access.repo_cnx() as cnx:
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
    99
            store = pgstore.SQLGenObjectStore(cnx)
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
   100
            eid = store.prepare_insert_entity('CWUser', login=u'toto',
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
   101
                                              upassword=u'pwd')
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
   102
            self.assertIsNotNone(eid)
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
   103
1079d68130e1 [dataimport] prepare_insert_entity should not crash with SQLGenObjectStore
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10810
diff changeset
   104
10513
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
if __name__ == '__main__':
7bec01a59f92 [dataimport] dispatch and deprecate old code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   106
    unittest_main()