test/unittest_repoapi.py
author Rémi Cardona <remi.cardona@logilab.fr>
Tue, 15 Dec 2015 14:12:59 +0100
changeset 10981 45bc791275b4
parent 10356 a009a31fb1ea
permissions -rw-r--r--
merge with 3.21.4
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/>.
10334
3f6702218bac [test] fix copy-pasta in doc string
Julien Cristau <julien.cristau@logilab.fr>
parents: 9658
diff changeset
    18
"""unittest for cubicweb.repoapi"""
9052
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
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10334
diff changeset
    24
from cubicweb.repoapi import Connection, 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
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    29
    def test_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
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    55
    def test_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
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10334
diff changeset
    59
        cltcnx = Connection(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"""
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    72
        cnx = connect(self.repo, login='admin', password='gingkow')
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    73
        self.assertEqual('admin', cnx.user.login)
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    74
        with cnx:
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    75
            rset = cnx.execute('Any X WHERE X is CWUser')
9061
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"""
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    80
        cnx = anonymous_cnx(self.repo)
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    81
        self.assertEqual('anon', cnx.user.login)
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    82
        with cnx:
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    83
            rset = cnx.execute('Any X WHERE X is CWUser')
9062
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()