test/unittest_repoapi.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 10 Jun 2014 16:01:49 +0200
changeset 10354 635cfac73d28
parent 10334 3f6702218bac
child 10356 a009a31fb1ea
permissions -rw-r--r--
[repoapi] fold ClientConnection into Connection Connection replaces ClientConnection everywhere. Some notes: * testlib: .client_cnx and .repo_cnx become aliases of .cnx (we might not want to tell people to update their tests again for just no real benefit, so we'll live with these aliases for a while) * entity.as_rset must not be cached because we risk caching result sets having a Connection object as .req (helps unittest_breadcrumbs) * entity._cw_dont_cache_attributes loses its repo/request special paths and only keeps its storage/bfss user (this helps unittest_wfobjs) * moreover, entity.cw_instantiate and .cw_set stop overriding the attributes cache *after* the before_*_entity hooks have run, because there is no need to (it is now actually harmful to do it and unittest_hooks.test_html_tidy* tests remain green because of this) * rset._build_entity sticks its .req onto the entity just fetched from the cache, because otherwise it might carry a _cw that is a Connection object where a Request is expected (helps unittest_views_actions) * we get overall better cache usages (entity caches were split over Request + ClientConnection and Connection), hence the changes unittest_entity and unittest_wfobjs * void the ecache when providing the cnx to a request object Having the entity cache pre-filled when we bind it to the request object hurts because these entities are bound to Connection objects, that lack e.g. `.form` or `.add_js` and crash the views subsystem. Thus, the unittest_testlib.test_error_raised test will are kept green. Closes #3837233
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
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
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"""
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()