test/unittest_repoapi.py
author David Douard <david.douard@logilab.fr>
Thu, 28 Aug 2014 18:52:41 +0200
changeset 10022 927a795bb380
parent 9658 c5b47406a15d
child 10334 3f6702218bac
permissions -rw-r--r--
[book] several fixes in the rst files - prevent duplicate markup definitions, - use existing Workflow markup instead of redefining one (in baseschema.rst) - include dataimport - fix non-existant directive autodocfunction - remove the empty 'embedding.rst' file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
     1
# copyright 2013-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     3
#
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     5
#
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     9
# any later version.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    10
#
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    14
# details.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    15
#
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    18
"""unittest for cubicweb.dbapi"""
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    19
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    20
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    21
from cubicweb.devtools.testlib import CubicWebTC
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    22
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    23
from cubicweb import ProgrammingError
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    24
from cubicweb.repoapi import ClientConnection, connect, anonymous_cnx
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    25
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    26
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    27
class REPOAPITC(CubicWebTC):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    28
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    29
    def test_clt_cnx_basic_usage(self):
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    30
        """Test that a client connection can be used to access the database"""
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    31
        with self.admin_access.client_cnx() as cltcnx:
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    32
            # (1) some RQL request
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    33
            rset = cltcnx.execute('Any X WHERE X is CWUser')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    34
            self.assertTrue(rset)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    35
            # (2) ORM usage
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    36
            random_user = rset.get_entity(0, 0)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    37
            # (3) Write operation
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    38
            random_user.cw_set(surname=u'babar')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    39
            # (4) commit
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    40
            cltcnx.commit()
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    41
            rset = cltcnx.execute('''Any X WHERE X is CWUser,
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    42
                                                 X surname "babar"
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    43
                                  ''')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    44
            self.assertTrue(rset)
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    45
            # prepare test for implicit rollback
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    46
            random_user = rset.get_entity(0, 0)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    47
            random_user.cw_set(surname=u'celestine')
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    48
        # implicit rollback on exit
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    49
        with self.admin_access.client_cnx() as cltcnx:
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    50
            rset = cltcnx.execute('''Any X WHERE X is CWUser,
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    51
                                                 X surname "babar"
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    52
                                  ''')
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    53
            self.assertTrue(rset)
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    54
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    55
    def test_clt_cnx_life_cycle(self):
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    56
        """Check that ClientConnection requires explicit open and close
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    57
        """
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    58
        access = self.admin_access
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    59
        cltcnx = ClientConnection(access._session)
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    60
        # connection not open yet
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    61
        with self.assertRaises(ProgrammingError):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    62
            cltcnx.execute('Any X WHERE X is CWUser')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    63
        # connection open and working
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    64
        with cltcnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    65
            cltcnx.execute('Any X WHERE X is CWUser')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    66
        # connection closed
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    67
        with self.assertRaises(ProgrammingError):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    68
            cltcnx.execute('Any X WHERE X is CWUser')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    69
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    70
    def test_connect(self):
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    71
        """check that repoapi.connect works and returns a usable connection"""
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    72
        clt_cnx = connect(self.repo, login='admin', password='gingkow')
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    73
        self.assertEqual('admin', clt_cnx.user.login)
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    74
        with clt_cnx:
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    75
            rset = clt_cnx.execute('Any X WHERE X is CWUser')
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    76
            self.assertTrue(rset)
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    77
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    78
    def test_anonymous_connect(self):
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    79
        """check that you can get anonymous connection when the data exist"""
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    80
        clt_cnx = anonymous_cnx(self.repo)
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    81
        self.assertEqual('anon', clt_cnx.user.login)
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    82
        with clt_cnx:
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    83
            rset = clt_cnx.execute('Any X WHERE X is CWUser')
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    84
            self.assertTrue(rset)
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    85
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    86
9658
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    87
if __name__ == '__main__':
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    88
    from logilab.common.testlib import unittest_main
c5b47406a15d [test] update unittest_repoapi to 3.19 api (sic)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9062
diff changeset
    89
    unittest_main()