test/unittest_repoapi.py
changeset 9658 c5b47406a15d
parent 9062 fa5bc8aef7ed
child 10334 3f6702218bac
equal deleted inserted replaced
9657:3ff4c6f80c37 9658:c5b47406a15d
     1 # copyright 2013-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2013-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    25 
    25 
    26 
    26 
    27 class REPOAPITC(CubicWebTC):
    27 class REPOAPITC(CubicWebTC):
    28 
    28 
    29     def test_clt_cnx_basic_usage(self):
    29     def test_clt_cnx_basic_usage(self):
    30         """Test that a client connection can be used to access the data base"""
    30         """Test that a client connection can be used to access the database"""
    31         cltcnx = ClientConnection(self.session)
    31         with self.admin_access.client_cnx() as cltcnx:
    32         with cltcnx:
       
    33             # (1) some RQL request
    32             # (1) some RQL request
    34             rset = cltcnx.execute('Any X WHERE X is CWUser')
    33             rset = cltcnx.execute('Any X WHERE X is CWUser')
    35             self.assertTrue(rset)
    34             self.assertTrue(rset)
    36             # (2) ORM usage
    35             # (2) ORM usage
    37             random_user = rset.get_entity(0, 0)
    36             random_user = rset.get_entity(0, 0)
    41             cltcnx.commit()
    40             cltcnx.commit()
    42             rset = cltcnx.execute('''Any X WHERE X is CWUser,
    41             rset = cltcnx.execute('''Any X WHERE X is CWUser,
    43                                                  X surname "babar"
    42                                                  X surname "babar"
    44                                   ''')
    43                                   ''')
    45             self.assertTrue(rset)
    44             self.assertTrue(rset)
    46             # prepare test for implicite rollback
    45             # prepare test for implicit rollback
    47             random_user = rset.get_entity(0, 0)
    46             random_user = rset.get_entity(0, 0)
    48             random_user.cw_set(surname=u'celestine')
    47             random_user.cw_set(surname=u'celestine')
    49         # implicite rollback on exit
    48         # implicit rollback on exit
    50         rset = self.session.execute('''Any X WHERE X is CWUser,
    49         with self.admin_access.client_cnx() as cltcnx:
       
    50             rset = cltcnx.execute('''Any X WHERE X is CWUser,
    51                                                  X surname "babar"
    51                                                  X surname "babar"
    52                                     ''')
    52                                   ''')
    53         self.assertTrue(rset)
    53             self.assertTrue(rset)
    54 
    54 
    55     def test_clt_cnx_life_cycle(self):
    55     def test_clt_cnx_life_cycle(self):
    56         """Check that ClientConnection requires explicite open and  close
    56         """Check that ClientConnection requires explicit open and close
    57         """
    57         """
    58         cltcnx = ClientConnection(self.session)
    58         access = self.admin_access
       
    59         cltcnx = ClientConnection(access._session)
    59         # connection not open yet
    60         # connection not open yet
    60         with self.assertRaises(ProgrammingError):
    61         with self.assertRaises(ProgrammingError):
    61             cltcnx.execute('Any X WHERE X is CWUser')
    62             cltcnx.execute('Any X WHERE X is CWUser')
    62         # connection open and working
    63         # connection open and working
    63         with cltcnx:
    64         with cltcnx:
    65         # connection closed
    66         # connection closed
    66         with self.assertRaises(ProgrammingError):
    67         with self.assertRaises(ProgrammingError):
    67             cltcnx.execute('Any X WHERE X is CWUser')
    68             cltcnx.execute('Any X WHERE X is CWUser')
    68 
    69 
    69     def test_connect(self):
    70     def test_connect(self):
    70         """check that repoapi.connect works and return a usable connection"""
    71         """check that repoapi.connect works and returns a usable connection"""
    71         clt_cnx = connect(self.repo, login='admin', password='gingkow')
    72         clt_cnx = connect(self.repo, login='admin', password='gingkow')
    72         self.assertEqual('admin', clt_cnx.user.login)
    73         self.assertEqual('admin', clt_cnx.user.login)
    73         with clt_cnx:
    74         with clt_cnx:
    74             rset = clt_cnx.execute('Any X WHERE X is CWUser')
    75             rset = clt_cnx.execute('Any X WHERE X is CWUser')
    75             self.assertTrue(rset)
    76             self.assertTrue(rset)
    76 
    77 
    77     def test_anonymous_connect(self):
    78     def test_anonymous_connect(self):
    78         """check that you can get anonymous connection when the data exist"""
    79         """check that you can get anonymous connection when the data exist"""
    79 
       
    80         clt_cnx = anonymous_cnx(self.repo)
    80         clt_cnx = anonymous_cnx(self.repo)
    81         self.assertEqual('anon', clt_cnx.user.login)
    81         self.assertEqual('anon', clt_cnx.user.login)
    82         with clt_cnx:
    82         with clt_cnx:
    83             rset = clt_cnx.execute('Any X WHERE X is CWUser')
    83             rset = clt_cnx.execute('Any X WHERE X is CWUser')
    84             self.assertTrue(rset)
    84             self.assertTrue(rset)
    85 
    85 
    86 
    86 
    87 
    87 if __name__ == '__main__':
    88 
    88     from logilab.common.testlib import unittest_main
       
    89     unittest_main()