test/unittest_dbapi.py
author Florent Cayré <florent.cayre@logilab.fr>
Wed, 11 Apr 2012 14:43:51 +0200
changeset 8366 6599f69bb846
parent 7554 fdace9d67d96
child 8367 fc59d2380c48
permissions -rw-r--r--
fix ``call_service`` API and add test service API ``call_service`` was taking a session argument. But Service is an AppoObject and already has the same session accessible as ``self._cw``.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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 __future__ import with_statement
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    21
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4768
diff changeset
    22
from copy import copy
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4768
diff changeset
    23
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    24
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
    25
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    26
from cubicweb import ConnectionError, cwconfig, NoSelectableObject
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
from cubicweb.dbapi import ProgrammingError
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    28
from cubicweb.devtools.testlib import CubicWebTC
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    30
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    31
class DBAPITC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
    def test_public_repo_api(self):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    34
        cnx = self.login('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
    35
        self.assertEqual(cnx.get_schema(), self.repo.schema)
7554
fdace9d67d96 test fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
    36
        self.assertEqual(cnx.source_defs(), {'system': {'type': 'native', 'uri': 'system',
fdace9d67d96 test fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6613
diff changeset
    37
                                                        'use-cwuri-as-url': False}})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        self.restore_connection() # proper way to close cnx
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        self.assertRaises(ProgrammingError, cnx.get_schema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        self.assertRaises(ProgrammingError, cnx.source_defs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    def test_db_api(self):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    43
        cnx = self.login('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
    44
        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
    45
        self.assertEqual(cnx.commit(), None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        self.restore_connection() # proper way to close cnx
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    47
        #self.assertEqual(cnx.close(), None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        self.assertRaises(ProgrammingError, cnx.rollback)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        self.assertRaises(ProgrammingError, cnx.commit)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
        self.assertRaises(ProgrammingError, cnx.close)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
    def test_api(self):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    53
        cnx = self.login('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
    54
        self.assertEqual(cnx.user(None).login, 'anon')
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6415
diff changeset
    55
        self.assertEqual(cnx.describe(1), (u'CWSource', u'system', None))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        self.restore_connection() # proper way to close cnx
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
    57
        self.assertRaises(ProgrammingError, cnx.user, None)
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):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    61
        cnx = self.login('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)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        self.restore_connection() # proper way to close cnx
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()
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    77
        with tempattr(self.cnx.vreg, 'config', config):
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    78
            self.cnx.use_web_compatible_requests('http://perdu.com')
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    79
            req = self.cnx.request()
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6427
diff changeset
    80
            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
    81
            self.assertEqual(req.from_controller(), 'view')
e7ff604491b2 [dbapi] web request compatibility fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6581
diff changeset
    82
            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
    83
            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
    84
            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
    85
8366
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    86
    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
    87
        req = self.request()
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]
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    89
        ret_value = req.cnx.call_service('test_service', msg='coucou')
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    90
        self.assertEqual('coucou', ServiceClass.passed_here.pop())
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    91
        self.assertEqual('babar', ret_value)
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    92
        with self.login('anon') as ctm:
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    93
            with self.assertRaises(NoSelectableObject):
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    94
                self.request().cnx.call_service('test_service', msg='toto')
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    95
            self.rollback()
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    96
            self.assertEqual([], ServiceClass.passed_here)
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    97
6599f69bb846 fix ``call_service`` API and add test service API
Florent Cayré <florent.cayre@logilab.fr>
parents: 7554
diff changeset
    98
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    from logilab.common.testlib import unittest_main
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    unittest_main()