test/unittest_repoapi.py
changeset 9052 4cba5f2cd57b
child 9061 e86fdab3d296
equal deleted inserted replaced
9051:944d66870c6a 9052:4cba5f2cd57b
       
     1 # copyright 2013-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """unittest for cubicweb.dbapi"""
       
    19 
       
    20 
       
    21 from cubicweb.devtools.testlib import CubicWebTC
       
    22 
       
    23 from cubicweb import ProgrammingError
       
    24 from cubicweb.repoapi import ClientConnection
       
    25 
       
    26 
       
    27 class REPOAPITC(CubicWebTC):
       
    28 
       
    29     def test_clt_cnx_basic_usage(self):
       
    30         """Test that a client connection can be used to access the data base"""
       
    31         cltcnx = ClientConnection(self.session)
       
    32         with cltcnx:
       
    33             # (1) some RQL request
       
    34             rset = cltcnx.execute('Any X WHERE X is CWUser')
       
    35             self.assertTrue(rset)
       
    36             # (2) ORM usage
       
    37             random_user = rset.get_entity(0, 0)
       
    38             # (3) Write operation
       
    39             random_user.cw_set(surname=u'babar')
       
    40             # (4) commit
       
    41             cltcnx.commit()
       
    42             rset = cltcnx.execute('''Any X WHERE X is CWUser,
       
    43                                                  X surname "babar"
       
    44                                   ''')
       
    45             self.assertTrue(rset)
       
    46             # prepare test for implicite rollback
       
    47             random_user = rset.get_entity(0, 0)
       
    48             random_user.cw_set(surname=u'celestine')
       
    49         # implicite rollback on exit
       
    50         rset = self.session.execute('''Any X WHERE X is CWUser,
       
    51                                                  X surname "babar"
       
    52                                     ''')
       
    53         self.assertTrue(rset)
       
    54 
       
    55     def test_clt_cnx_life_cycle(self):
       
    56         """Check that ClientConnection requires explicite open and  close
       
    57         """
       
    58         cltcnx = ClientConnection(self.session)
       
    59         # connection not open yet
       
    60         with self.assertRaises(ProgrammingError):
       
    61             cltcnx.execute('Any X WHERE X is CWUser')
       
    62         # connection open and working
       
    63         with cltcnx:
       
    64             cltcnx.execute('Any X WHERE X is CWUser')
       
    65         # connection closed
       
    66         with self.assertRaises(ProgrammingError):
       
    67             cltcnx.execute('Any X WHERE X is CWUser')
       
    68 
       
    69 
       
    70