|
1 # -*- coding: utf-8 -*- |
|
2 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 """unit tests for module cubicweb.server.sqlutils |
|
20 """ |
|
21 |
|
22 import sys |
|
23 |
|
24 from logilab.common.testlib import TestCase, unittest_main |
|
25 |
|
26 from cubicweb.server.sqlutils import * |
|
27 |
|
28 from cubicweb.devtools.testlib import CubicWebTC |
|
29 |
|
30 BASE_CONFIG = { |
|
31 'db-driver' : 'Postgres', |
|
32 'db-host' : 'crater', |
|
33 'db-name' : 'cubicweb2_test', |
|
34 'db-user' : 'toto', |
|
35 'db-upassword' : 'toto', |
|
36 } |
|
37 |
|
38 class SQLAdapterMixInTC(TestCase): |
|
39 |
|
40 def test_init(self): |
|
41 o = SQLAdapterMixIn(BASE_CONFIG) |
|
42 self.assertEqual(o.dbhelper.dbencoding, 'UTF-8') |
|
43 |
|
44 def test_init_encoding(self): |
|
45 config = BASE_CONFIG.copy() |
|
46 config['db-encoding'] = 'ISO-8859-1' |
|
47 o = SQLAdapterMixIn(config) |
|
48 self.assertEqual(o.dbhelper.dbencoding, 'ISO-8859-1') |
|
49 |
|
50 |
|
51 class SQLUtilsTC(CubicWebTC): |
|
52 |
|
53 def test_group_concat(self): |
|
54 with self.admin_access.repo_cnx() as cnx: |
|
55 g = cnx.create_entity('CWGroup', name=u'héhé') |
|
56 u = cnx.create_entity('CWUser', login=u'toto', upassword=u'', |
|
57 in_group=g.eid) |
|
58 rset = cnx.execute(u'Any L,GROUP_CONCAT(G) GROUPBY L WHERE X login L,' |
|
59 u'X in_group G, G name GN, NOT G name IN ("users", "héhé")') |
|
60 self.assertEqual([[u'admin', u'3'], [u'anon', u'2']], |
|
61 rset.rows) |
|
62 rset = cnx.execute('Any L,GROUP_CONCAT(GN) GROUPBY L WHERE X login L,' |
|
63 'X in_group G, G name GN, NOT G name "users"') |
|
64 self.assertEqual([[u'admin', u'managers'], [u'anon', u'guests'], [u'toto', u'héhé']], |
|
65 rset.rows) |
|
66 |
|
67 if __name__ == '__main__': |
|
68 unittest_main() |