test/unittest_dbapi.py
author Julien Cristau <julien.cristau@logilab.fr>
Tue, 23 Jun 2015 15:17:31 +0200
changeset 10449 279b3662e8f1
parent 9818 e3d2012adcd0
permissions -rw-r--r--
[doc/book] fix rql syntax examples "Any", not "ANY". "EXISTS" is a function.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8544
3d049071957e massive copyright update to avoid clutering later patches
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8367
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4913
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6415
b0b0f097a72d cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    18
"""unittest for cubicweb.dbapi"""
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1398
diff changeset
    19
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4768
diff changeset
    20
from copy import copy
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4768
diff changeset
    21
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    22
from logilab.common import tempattr
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    23
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    24
from cubicweb import ConnectionError, cwconfig, NoSelectableObject
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    25
from cubicweb.dbapi import ProgrammingError, _repo_connect
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    26
from cubicweb.devtools.testlib import CubicWebTC
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    28
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    29
class DBAPITC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    def test_public_repo_api(self):
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    32
        cnx = _repo_connect(self.repo, login='anon', password='anon')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    33
        self.assertEqual(cnx.get_schema(), self.repo.schema)
7554
fdace9d67d96 test fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
    34
        self.assertEqual(cnx.source_defs(), {'system': {'type': 'native', 'uri': 'system',
fdace9d67d96 test fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
    35
                                                        'use-cwuri-as-url': False}})
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    36
        cnx.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        self.assertRaises(ProgrammingError, cnx.get_schema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        self.assertRaises(ProgrammingError, cnx.source_defs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    def test_db_api(self):
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    41
        cnx = _repo_connect(self.repo, login='anon', password='anon')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    42
        self.assertEqual(cnx.rollback(), None)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    43
        self.assertEqual(cnx.commit(), None)
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    44
        cnx.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        self.assertRaises(ProgrammingError, cnx.rollback)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        self.assertRaises(ProgrammingError, cnx.commit)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        self.assertRaises(ProgrammingError, cnx.close)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    def test_api(self):
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    50
        cnx = _repo_connect(self.repo, login='anon', password='anon')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    51
        self.assertEqual(cnx.user(None).login, 'anon')
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9068
diff changeset
    52
        self.assertEqual({'type': u'CWSource', 'source': u'system', 'extid': None},
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9068
diff changeset
    53
                         cnx.entity_metas(1))
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6415
diff changeset
    54
        self.assertEqual(cnx.describe(1), (u'CWSource', u'system', None))
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    55
        cnx.close()
4768
430b89aed996 delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    56
        self.assertRaises(ProgrammingError, cnx.user, None)
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9068
diff changeset
    57
        self.assertRaises(ProgrammingError, cnx.entity_metas, 1)
4768
430b89aed996 delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    58
        self.assertRaises(ProgrammingError, cnx.describe, 1)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    def test_shared_data_api(self):
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    61
        cnx = _repo_connect(self.repo, login='anon', password='anon')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    62
        self.assertEqual(cnx.get_shared_data('data'), None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        cnx.set_shared_data('data', 4)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    64
        self.assertEqual(cnx.get_shared_data('data'), 4)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        cnx.get_shared_data('data', pop=True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        cnx.get_shared_data('whatever', pop=True)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    67
        self.assertEqual(cnx.get_shared_data('data'), None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        cnx.set_shared_data('data', 4)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    69
        self.assertEqual(cnx.get_shared_data('data'), 4)
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    70
        cnx.close()
4768
430b89aed996 delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    71
        self.assertRaises(ProgrammingError, cnx.check)
430b89aed996 delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    72
        self.assertRaises(ProgrammingError, cnx.set_shared_data, 'data', 0)
430b89aed996 delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    73
        self.assertRaises(ProgrammingError, cnx.get_shared_data, 'data')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    75
    def test_web_compatible_request(self):
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    76
        config = cwconfig.CubicWebNoAppConfiguration()
9068
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    77
        cnx = _repo_connect(self.repo, login='admin', password='gingkow')
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    78
        with tempattr(cnx.vreg, 'config', config):
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    79
            cnx.use_web_compatible_requests('http://perdu.com')
86dcc29740e0 [test/dbapi] do not rely on the Testcase provided cnx
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    80
            req = cnx.request()
9818
e3d2012adcd0 [request] Ensure base_url() always has a trailing '/' (closes #3955093)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9469
diff changeset
    81
            self.assertEqual(req.base_url(), 'http://perdu.com/')
6613
e7ff604491b2 [dbapi] web request compatibility fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6581
diff changeset
    82
            self.assertEqual(req.from_controller(), 'view')
e7ff604491b2 [dbapi] web request compatibility fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6581
diff changeset
    83
            self.assertEqual(req.relative_path(), '')
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    84
            req.ajax_replace_url('domid') # don't crash
6613
e7ff604491b2 [dbapi] web request compatibility fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6581
diff changeset
    85
            req.user.cw_adapt_to('IBreadCrumbs') # don't crash
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4768
diff changeset
    86
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    87
    def test_call_service(self):
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    88
        ServiceClass = self.vreg['services']['test_service'][0]
8367
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    89
        for _cw in (self.request(), self.session):
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    90
            ret_value = _cw.call_service('test_service', msg='coucou')
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    91
            self.assertEqual('coucou', ServiceClass.passed_here.pop())
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    92
            self.assertEqual('babar', ret_value)
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    93
        with self.login('anon') as ctm:
8367
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    94
            for _cw in (self.request(), self.session):
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    95
                with self.assertRaises(NoSelectableObject):
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    96
                    _cw.call_service('test_service', msg='toto')
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    97
                self.rollback()
fc59d2380c48 [service-api] Add unified service API on `_cw` attribute
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8366
diff changeset
    98
                self.assertEqual([], ServiceClass.passed_here)
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    99
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
   100
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    from logilab.common.testlib import unittest_main
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    unittest_main()