1 # copyright 2013-2014 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.repoapi""" |
|
19 |
|
20 |
|
21 from cubicweb.devtools.testlib import CubicWebTC |
|
22 |
|
23 from cubicweb import ProgrammingError |
|
24 from cubicweb.repoapi import Connection, connect, anonymous_cnx |
|
25 |
|
26 |
|
27 class REPOAPITC(CubicWebTC): |
|
28 |
|
29 def test_cnx_basic_usage(self): |
|
30 """Test that a client connection can be used to access the database""" |
|
31 with self.admin_access.client_cnx() as cltcnx: |
|
32 # (1) some RQL request |
|
33 rset = cltcnx.execute('Any X WHERE X is CWUser') |
|
34 self.assertTrue(rset) |
|
35 # (2) ORM usage |
|
36 random_user = rset.get_entity(0, 0) |
|
37 # (3) Write operation |
|
38 random_user.cw_set(surname=u'babar') |
|
39 # (4) commit |
|
40 cltcnx.commit() |
|
41 rset = cltcnx.execute('''Any X WHERE X is CWUser, |
|
42 X surname "babar" |
|
43 ''') |
|
44 self.assertTrue(rset) |
|
45 # prepare test for implicit rollback |
|
46 random_user = rset.get_entity(0, 0) |
|
47 random_user.cw_set(surname=u'celestine') |
|
48 # implicit rollback on exit |
|
49 with self.admin_access.client_cnx() as cltcnx: |
|
50 rset = cltcnx.execute('''Any X WHERE X is CWUser, |
|
51 X surname "babar" |
|
52 ''') |
|
53 self.assertTrue(rset) |
|
54 |
|
55 def test_cnx_life_cycle(self): |
|
56 """Check that ClientConnection requires explicit open and close |
|
57 """ |
|
58 access = self.admin_access |
|
59 cltcnx = Connection(access._session) |
|
60 # connection not open yet |
|
61 with self.assertRaises(ProgrammingError): |
|
62 cltcnx.execute('Any X WHERE X is CWUser') |
|
63 # connection open and working |
|
64 with cltcnx: |
|
65 cltcnx.execute('Any X WHERE X is CWUser') |
|
66 # connection closed |
|
67 with self.assertRaises(ProgrammingError): |
|
68 cltcnx.execute('Any X WHERE X is CWUser') |
|
69 |
|
70 def test_connect(self): |
|
71 """check that repoapi.connect works and returns a usable connection""" |
|
72 cnx = connect(self.repo, login='admin', password='gingkow') |
|
73 self.assertEqual('admin', cnx.user.login) |
|
74 with cnx: |
|
75 rset = cnx.execute('Any X WHERE X is CWUser') |
|
76 self.assertTrue(rset) |
|
77 |
|
78 def test_anonymous_connect(self): |
|
79 """check that you can get anonymous connection when the data exist""" |
|
80 cnx = anonymous_cnx(self.repo) |
|
81 self.assertEqual('anon', cnx.user.login) |
|
82 with cnx: |
|
83 rset = cnx.execute('Any X WHERE X is CWUser') |
|
84 self.assertTrue(rset) |
|
85 |
|
86 |
|
87 if __name__ == '__main__': |
|
88 from logilab.common.testlib import unittest_main |
|
89 unittest_main() |
|