test/unittest_repoapi.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 27 Jun 2013 19:14:22 +0200
changeset 9124 d1d4b3669e41
parent 9062 fa5bc8aef7ed
child 9658 c5b47406a15d
permissions -rw-r--r--
[testlib] deprecated the older api to access the repo.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
     1
# copyright 2013-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    30
        """Test that a client connection can be used to access the data base"""
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    31
        cltcnx = ClientConnection(self.session)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    32
        with cltcnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    33
            # (1) some RQL request
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    34
            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
    35
            self.assertTrue(rset)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    36
            # (2) ORM usage
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    37
            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
    38
            # (3) Write operation
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    39
            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
    40
            # (4) commit
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    41
            cltcnx.commit()
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    42
            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
    43
                                                 X surname "babar"
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    44
                                  ''')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    45
            self.assertTrue(rset)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    46
            # prepare test for implicite rollback
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    47
            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
    48
            random_user.cw_set(surname=u'celestine')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    49
        # implicite rollback on exit
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    50
        rset = self.session.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
    51
                                                 X surname "babar"
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    52
                                    ''')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    53
        self.assertTrue(rset)
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):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    56
        """Check that ClientConnection requires explicite open and  close
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    57
        """
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    58
        cltcnx = ClientConnection(self.session)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    59
        # connection not open yet
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    60
        with self.assertRaises(ProgrammingError):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    61
            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
    62
        # connection open and working
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    63
        with cltcnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    64
            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
    65
        # connection closed
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    66
        with self.assertRaises(ProgrammingError):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    67
            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
    68
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    69
    def test_connect(self):
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    70
        """check that repoapi.connect works and return a usable connection"""
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    71
        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
    72
        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
    73
        with clt_cnx:
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    74
            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
    75
            self.assertTrue(rset)
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    76
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    77
    def test_anonymous_connect(self):
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    78
        """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
    79
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
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
    87
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    88