author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 17 May 2010 17:29:54 +0200 | |
branch | stable |
changeset 5528 | 5d1b2ae7d459 |
parent 5426 | 0d4853a6e5ee |
child 5557 | 1a534c596bff |
child 5667 | 04cbd80fd5dc |
permissions | -rw-r--r-- |
0 | 1 |
# coding: utf-8 |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
2 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
3 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
4 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
5 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
6 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
7 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
8 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
9 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
10 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
11 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
12 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
14 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
15 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
16 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
17 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
18 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3589
diff
changeset
|
19 |
"""unit tests for module cubicweb.utils |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1640
diff
changeset
|
20 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1640
diff
changeset
|
21 |
""" |
4536
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
22 |
|
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
23 |
from urlparse import urlsplit |
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
24 |
|
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
25 |
from rql import parse |
0 | 26 |
|
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
27 |
from logilab.common.testlib import TestCase, unittest_main, mock_object |
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
572
diff
changeset
|
28 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
29 |
from cubicweb.devtools.testlib import CubicWebTC |
0 | 30 |
from cubicweb.rset import NotAnEntity, ResultSet, attr_desc_iterator |
31 |
||
1640 | 32 |
|
0 | 33 |
def pprelcachedict(d): |
34 |
res = {} |
|
35 |
for k, (rset, related) in d.items(): |
|
36 |
res[k] = sorted(v.eid for v in related) |
|
37 |
return sorted(res.items()) |
|
1640 | 38 |
|
0 | 39 |
|
40 |
class AttrDescIteratorTC(TestCase): |
|
41 |
"""TestCase for cubicweb.rset.attr_desc_iterator""" |
|
1640 | 42 |
|
0 | 43 |
def test_relations_description(self): |
44 |
"""tests relations_description() function""" |
|
45 |
queries = { |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
46 |
'Any U,L,M where U is CWUser, U login L, U mail M' : [(1, 'login', 'subject'), (2, 'mail', 'subject')], |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
47 |
'Any U,L,M where U is CWUser, L is Foo, U mail M' : [(2, 'mail', 'subject')], |
0 | 48 |
'Any C,P where C is Company, C employs P' : [(1, 'employs', 'subject')], |
49 |
'Any C,P where C is Company, P employed_by P' : [], |
|
50 |
'Any C where C is Company, C employs P' : [], |
|
51 |
} |
|
52 |
for rql, relations in queries.items(): |
|
53 |
result = list(attr_desc_iterator(parse(rql).children[0])) |
|
54 |
self.assertEquals((rql, result), (rql, relations)) |
|
1640 | 55 |
|
0 | 56 |
def test_relations_description_indexed(self): |
57 |
"""tests relations_description() function""" |
|
58 |
queries = { |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
59 |
'Any C,U,P,L,M where C is Company, C employs P, U is CWUser, U login L, U mail M' : |
0 | 60 |
{0: [(2,'employs', 'subject')], 1: [(3,'login', 'subject'), (4,'mail', 'subject')]}, |
61 |
} |
|
62 |
for rql, results in queries.items(): |
|
63 |
for var_index, relations in results.items(): |
|
64 |
result = list(attr_desc_iterator(parse(rql).children[0], var_index)) |
|
65 |
self.assertEquals(result, relations) |
|
66 |
||
67 |
||
68 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
69 |
class ResultSetTC(CubicWebTC): |
0 | 70 |
|
71 |
def setUp(self): |
|
72 |
super(ResultSetTC, self).setUp() |
|
73 |
self.rset = ResultSet([[12, 'adim'], [13, 'syt']], |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
74 |
'Any U,L where U is CWUser, U login L', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
75 |
description=[['CWUser', 'String'], ['Bar', 'String']]) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
76 |
self.rset.req = mock_object(vreg=self.vreg) |
0 | 77 |
|
78 |
def compare_urls(self, url1, url2): |
|
79 |
info1 = urlsplit(url1) |
|
80 |
info2 = urlsplit(url2) |
|
81 |
self.assertEquals(info1[:3], info2[:3]) |
|
82 |
if info1[3] != info2[3]: |
|
83 |
params1 = dict(pair.split('=') for pair in info1[3].split('&')) |
|
84 |
params2 = dict(pair.split('=') for pair in info1[3].split('&')) |
|
85 |
self.assertDictEquals(params1, params2) |
|
86 |
||
1640 | 87 |
|
0 | 88 |
def test_build_url(self): |
89 |
req = self.request() |
|
90 |
baseurl = req.base_url() |
|
91 |
self.compare_urls(req.build_url('view', vid='foo', rql='yo'), |
|
92 |
'%sview?vid=foo&rql=yo' % baseurl) |
|
93 |
self.compare_urls(req.build_url('view', _restpath='task/title/go'), |
|
94 |
'%stask/title/go' % baseurl) |
|
95 |
#self.compare_urls(req.build_url('view', _restpath='/task/title/go'), |
|
96 |
# '%stask/title/go' % baseurl) |
|
97 |
# empty _restpath should not crash |
|
98 |
self.compare_urls(req.build_url('view', _restpath=''), baseurl) |
|
1640 | 99 |
|
100 |
||
0 | 101 |
def test_resultset_build(self): |
102 |
"""test basic build of a ResultSet""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
103 |
rs = ResultSet([1,2,3], 'CWGroup X', description=['CWGroup', 'CWGroup', 'CWGroup']) |
0 | 104 |
self.assertEquals(rs.rowcount, 3) |
105 |
self.assertEquals(rs.rows, [1,2,3]) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
106 |
self.assertEquals(rs.description, ['CWGroup', 'CWGroup', 'CWGroup']) |
0 | 107 |
|
108 |
||
109 |
def test_resultset_limit(self): |
|
110 |
rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']], |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
111 |
'Any U,L where U is CWUser, U login L', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
112 |
description=[['CWUser', 'String']] * 3) |
0 | 113 |
rs.req = self.request() |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
114 |
rs.vreg = self.vreg |
0 | 115 |
|
116 |
self.assertEquals(rs.limit(2).rows, [[12000, 'adim'], [13000, 'syt']]) |
|
117 |
rs2 = rs.limit(2, offset=1) |
|
118 |
self.assertEquals(rs2.rows, [[13000, 'syt'], [14000, 'nico']]) |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
119 |
self.assertEquals(rs2.get_entity(0, 0).cw_row, 0) |
0 | 120 |
self.assertEquals(rs.limit(2, offset=2).rows, [[14000, 'nico']]) |
121 |
self.assertEquals(rs.limit(2, offset=3).rows, []) |
|
1640 | 122 |
|
0 | 123 |
|
124 |
def test_resultset_filter(self): |
|
125 |
rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']], |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
126 |
'Any U,L where U is CWUser, U login L', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
127 |
description=[['CWUser', 'String']] * 3) |
0 | 128 |
rs.req = self.request() |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
129 |
rs.vreg = self.vreg |
0 | 130 |
def test_filter(entity): |
131 |
return entity.login != 'nico' |
|
1640 | 132 |
|
0 | 133 |
rs2 = rs.filtered_rset(test_filter) |
134 |
self.assertEquals(len(rs2), 2) |
|
135 |
self.assertEquals([login for _, login in rs2], ['adim', 'syt']) |
|
1640 | 136 |
|
0 | 137 |
def test_resultset_transform(self): |
138 |
rs = ResultSet([[12, 'adim'], [13, 'syt'], [14, 'nico']], |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
139 |
'Any U,L where U is CWUser, U login L', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
140 |
description=[['CWUser', 'String']] * 3) |
0 | 141 |
rs.req = self.request() |
142 |
def test_transform(row, desc): |
|
143 |
return row[1:], desc[1:] |
|
144 |
rs2 = rs.transformed_rset(test_transform) |
|
145 |
||
146 |
self.assertEquals(len(rs2), 3) |
|
147 |
self.assertEquals(list(rs2), [['adim'],['syt'],['nico']]) |
|
1640 | 148 |
|
0 | 149 |
def test_resultset_sort(self): |
150 |
rs = ResultSet([[12000, 'adim'], [13000, 'syt'], [14000, 'nico']], |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
151 |
'Any U,L where U is CWUser, U login L', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
152 |
description=[['CWUser', 'String']] * 3) |
0 | 153 |
rs.req = self.request() |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
154 |
rs.vreg = self.vreg |
1640 | 155 |
|
0 | 156 |
rs2 = rs.sorted_rset(lambda e:e['login']) |
157 |
self.assertEquals(len(rs2), 3) |
|
158 |
self.assertEquals([login for _, login in rs2], ['adim', 'nico', 'syt']) |
|
159 |
# make sure rs is unchanged |
|
160 |
self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico']) |
|
1640 | 161 |
|
0 | 162 |
rs2 = rs.sorted_rset(lambda e:e['login'], reverse=True) |
163 |
self.assertEquals(len(rs2), 3) |
|
164 |
self.assertEquals([login for _, login in rs2], ['syt', 'nico', 'adim']) |
|
165 |
# make sure rs is unchanged |
|
166 |
self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico']) |
|
167 |
||
168 |
rs3 = rs.sorted_rset(lambda row: row[1], col=-1) |
|
169 |
self.assertEquals(len(rs3), 3) |
|
170 |
self.assertEquals([login for _, login in rs3], ['adim', 'nico', 'syt']) |
|
171 |
# make sure rs is unchanged |
|
172 |
self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico']) |
|
173 |
||
174 |
def test_resultset_split(self): |
|
175 |
rs = ResultSet([[12000, 'adim', u'Adim chez les pinguins'], |
|
176 |
[12000, 'adim', u'Jardiner facile'], |
|
177 |
[13000, 'syt', u'Le carrelage en 42 leçons'], |
|
178 |
[14000, 'nico', u'La tarte tatin en 15 minutes'], |
|
179 |
[14000, 'nico', u"L'épluchage du castor commun"]], |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
180 |
'Any U, L, T WHERE U is CWUser, U login L,'\ |
0 | 181 |
'D created_by U, D title T', |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
182 |
description=[['CWUser', 'String', 'String']] * 5) |
0 | 183 |
rs.req = self.request() |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
184 |
rs.vreg = self.vreg |
1640 | 185 |
|
0 | 186 |
rsets = rs.split_rset(lambda e:e['login']) |
187 |
self.assertEquals(len(rsets), 3) |
|
188 |
self.assertEquals([login for _, login,_ in rsets[0]], ['adim', 'adim']) |
|
189 |
self.assertEquals([login for _, login,_ in rsets[1]], ['syt']) |
|
190 |
self.assertEquals([login for _, login,_ in rsets[2]], ['nico', 'nico']) |
|
191 |
# make sure rs is unchanged |
|
192 |
self.assertEquals([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico']) |
|
1640 | 193 |
|
0 | 194 |
rsets = rs.split_rset(lambda e:e['login'], return_dict=True) |
195 |
self.assertEquals(len(rsets), 3) |
|
196 |
self.assertEquals([login for _, login,_ in rsets['nico']], ['nico', 'nico']) |
|
197 |
self.assertEquals([login for _, login,_ in rsets['adim']], ['adim', 'adim']) |
|
198 |
self.assertEquals([login for _, login,_ in rsets['syt']], ['syt']) |
|
199 |
# make sure rs is unchanged |
|
200 |
self.assertEquals([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico']) |
|
201 |
||
202 |
rsets = rs.split_rset(lambda s: s.count('d'), col=2) |
|
203 |
self.assertEquals(len(rsets), 2) |
|
204 |
self.assertEquals([title for _, _, title in rsets[0]], |
|
205 |
[u"Adim chez les pinguins", |
|
206 |
u"Jardiner facile", |
|
207 |
u"L'épluchage du castor commun",]) |
|
208 |
self.assertEquals([title for _, _, title in rsets[1]], |
|
209 |
[u"Le carrelage en 42 leçons", |
|
210 |
u"La tarte tatin en 15 minutes",]) |
|
211 |
# make sure rs is unchanged |
|
212 |
self.assertEquals([title for _, _, title in rs], |
|
213 |
[u'Adim chez les pinguins', |
|
214 |
u'Jardiner facile', |
|
215 |
u'Le carrelage en 42 leçons', |
|
216 |
u'La tarte tatin en 15 minutes', |
|
217 |
u"L'épluchage du castor commun"]) |
|
1640 | 218 |
|
0 | 219 |
def test_cached_syntax_tree(self): |
220 |
"""make sure syntax tree is cached""" |
|
221 |
rqlst1 = self.rset.syntax_tree() |
|
222 |
rqlst2 = self.rset.syntax_tree() |
|
223 |
self.assert_(rqlst1 is rqlst2) |
|
224 |
||
225 |
def test_get_entity_simple(self): |
|
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
226 |
self.request().create_entity('CWUser', login=u'adim', upassword='adim', |
0 | 227 |
surname=u'di mascio', firstname=u'adrien') |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
228 |
e = self.execute('Any X,T WHERE X login "adim", X surname T').get_entity(0, 0) |
0 | 229 |
self.assertEquals(e['surname'], 'di mascio') |
230 |
self.assertRaises(KeyError, e.__getitem__, 'firstname') |
|
231 |
self.assertRaises(KeyError, e.__getitem__, 'creation_date') |
|
232 |
self.assertEquals(pprelcachedict(e._related_cache), []) |
|
233 |
e.complete() |
|
234 |
self.assertEquals(e['firstname'], 'adrien') |
|
235 |
self.assertEquals(pprelcachedict(e._related_cache), []) |
|
1640 | 236 |
|
0 | 237 |
def test_get_entity_advanced(self): |
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
238 |
self.request().create_entity('Bookmark', title=u'zou', path=u'/view') |
0 | 239 |
self.execute('SET X bookmarked_by Y WHERE X is Bookmark, Y login "anon"') |
240 |
rset = self.execute('Any X,Y,XT,YN WHERE X bookmarked_by Y, X title XT, Y login YN') |
|
1640 | 241 |
|
0 | 242 |
e = rset.get_entity(0, 0) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
243 |
self.assertEquals(e.cw_row, 0) |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
244 |
self.assertEquals(e.cw_col, 0) |
0 | 245 |
self.assertEquals(e['title'], 'zou') |
246 |
self.assertRaises(KeyError, e.__getitem__, 'path') |
|
1640 | 247 |
self.assertEquals(e.view('text'), 'zou') |
0 | 248 |
self.assertEquals(pprelcachedict(e._related_cache), []) |
1640 | 249 |
|
0 | 250 |
e = rset.get_entity(0, 1) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
251 |
self.assertEquals(e.cw_row, 0) |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
252 |
self.assertEquals(e.cw_col, 1) |
0 | 253 |
self.assertEquals(e['login'], 'anon') |
254 |
self.assertRaises(KeyError, e.__getitem__, 'firstname') |
|
255 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
256 |
[]) |
|
257 |
e.complete() |
|
258 |
self.assertEquals(e['firstname'], None) |
|
259 |
self.assertEquals(e.view('text'), 'anon') |
|
260 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
261 |
[]) |
|
1640 | 262 |
|
0 | 263 |
self.assertRaises(NotAnEntity, rset.get_entity, 0, 2) |
264 |
self.assertRaises(NotAnEntity, rset.get_entity, 0, 3) |
|
265 |
||
266 |
def test_get_entity_relation_cache_compt(self): |
|
267 |
rset = self.execute('Any X,S WHERE X in_state S, X login "anon"') |
|
268 |
e = rset.get_entity(0, 0) |
|
269 |
seid = self.execute('State X WHERE X name "activated"')[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
270 |
# for_user / in_group are prefetched in CWUser __init__, in_state should |
0 | 271 |
# be filed from our query rset |
272 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
273 |
[('in_state_subject', [seid])]) |
|
274 |
||
275 |
def test_get_entity_advanced_prefilled_cache(self): |
|
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
276 |
e = self.request().create_entity('Bookmark', title=u'zou', path=u'path') |
0 | 277 |
self.commit() |
278 |
rset = self.execute('Any X,U,S,XT,UL,SN WHERE X created_by U, U in_state S, ' |
|
279 |
'X title XT, S name SN, U login UL, X eid %s' % e.eid) |
|
280 |
e = rset.get_entity(0, 0) |
|
281 |
self.assertEquals(e['title'], 'zou') |
|
282 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
283 |
[('created_by_subject', [5])]) |
|
284 |
# first level of recursion |
|
285 |
u = e.created_by[0] |
|
286 |
self.assertEquals(u['login'], 'admin') |
|
287 |
self.assertRaises(KeyError, u.__getitem__, 'firstname') |
|
288 |
# second level of recursion |
|
289 |
s = u.in_state[0] |
|
290 |
self.assertEquals(s['name'], 'activated') |
|
291 |
self.assertRaises(KeyError, s.__getitem__, 'description') |
|
292 |
||
1640 | 293 |
|
0 | 294 |
def test_get_entity_cache_with_left_outer_join(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
295 |
eid = self.execute('INSERT CWUser E: E login "joe", E upassword "joe", E in_group G ' |
0 | 296 |
'WHERE G name "users"')[0][0] |
297 |
rset = self.execute('Any X,E WHERE X eid %(x)s, X primary_email E?', {'x': eid}) |
|
298 |
e = rset.get_entity(0, 0) |
|
299 |
# if any of the assertion below fails with a KeyError, the relation is not cached |
|
300 |
# related entities should be an empty list |
|
3587 | 301 |
self.assertEquals(e.related_cache('primary_email', 'subject', True), ()) |
0 | 302 |
# related rset should be an empty rset |
303 |
cached = e.related_cache('primary_email', 'subject', False) |
|
304 |
self.assertIsInstance(cached, ResultSet) |
|
305 |
self.assertEquals(cached.rowcount, 0) |
|
1640 | 306 |
|
0 | 307 |
|
308 |
def test_get_entity_union(self): |
|
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
309 |
e = self.request().create_entity('Bookmark', title=u'manger', path=u'path') |
0 | 310 |
rset = self.execute('Any X,N ORDERBY N WITH X,N BEING ' |
311 |
'((Any X,N WHERE X is Bookmark, X title N)' |
|
312 |
' UNION ' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
313 |
' (Any X,N WHERE X is CWGroup, X name N))') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
314 |
expected = (('CWGroup', 'guests'), ('CWGroup', 'managers'), |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
315 |
('Bookmark', 'manger'), ('CWGroup', 'owners'), |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
316 |
('CWGroup', 'users')) |
0 | 317 |
for entity in rset.entities(): # test get_entity for each row actually |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
318 |
etype, n = expected[entity.cw_row] |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
319 |
self.assertEquals(entity.__regid__, etype) |
0 | 320 |
attr = etype == 'Bookmark' and 'title' or 'name' |
321 |
self.assertEquals(entity[attr], n) |
|
1640 | 322 |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
323 |
def test_related_entity_optional(self): |
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
324 |
e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path') |
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
325 |
rset = self.execute('Any B,U,L WHERE B bookmarked_by U?, U login L') |
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
326 |
entity, rtype = rset.related_entity(0, 2) |
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
327 |
self.assertEquals(entity, None) |
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
328 |
self.assertEquals(rtype, None) |
1640 | 329 |
|
0 | 330 |
def test_related_entity_union_subquery(self): |
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
331 |
e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path') |
0 | 332 |
rset = self.execute('Any X,N ORDERBY N WITH X,N BEING ' |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
333 |
'((Any X,N WHERE X is CWGroup, X name N)' |
0 | 334 |
' UNION ' |
335 |
' (Any X,N WHERE X is Bookmark, X title N))') |
|
336 |
entity, rtype = rset.related_entity(0, 1) |
|
337 |
self.assertEquals(entity.eid, e.eid) |
|
338 |
self.assertEquals(rtype, 'title') |
|
339 |
entity, rtype = rset.related_entity(1, 1) |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
340 |
self.assertEquals(entity.__regid__, 'CWGroup') |
0 | 341 |
self.assertEquals(rtype, 'name') |
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
342 |
# |
0 | 343 |
rset = self.execute('Any X,N ORDERBY N WHERE X is Bookmark WITH X,N BEING ' |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
344 |
'((Any X,N WHERE X is CWGroup, X name N)' |
0 | 345 |
' UNION ' |
346 |
' (Any X,N WHERE X is Bookmark, X title N))') |
|
347 |
entity, rtype = rset.related_entity(0, 1) |
|
348 |
self.assertEquals(entity.eid, e.eid) |
|
349 |
self.assertEquals(rtype, 'title') |
|
3016
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
350 |
# |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
351 |
rset = self.execute('Any X,N ORDERBY N WITH N,X BEING ' |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
352 |
'((Any N,X WHERE X is CWGroup, X name N)' |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
353 |
' UNION ' |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
354 |
' (Any N,X WHERE X is Bookmark, X title N))') |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
355 |
entity, rtype = rset.related_entity(0, 1) |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
356 |
self.assertEquals(entity.eid, e.eid) |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
357 |
self.assertEquals(rtype, 'title') |
1640 | 358 |
|
4427
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
359 |
def test_related_entity_trap_subquery(self): |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
360 |
req = self.request() |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
361 |
req.create_entity('Bookmark', title=u'test bookmark', path=u'') |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
362 |
self.execute('SET B bookmarked_by U WHERE U login "admin"') |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
363 |
rset = self.execute('Any B,T,L WHERE B bookmarked_by U, U login L ' |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
364 |
'WITH B,T BEING (Any B,T WHERE B is Bookmark, B title T)') |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
365 |
rset.related_entity(0, 2) |
410c99a917fa
fix rset.related_entity with variables coming from subquery while some others not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
366 |
|
0 | 367 |
def test_entities(self): |
368 |
rset = self.execute('Any U,G WHERE U in_group G') |
|
369 |
# make sure we have at least one element |
|
370 |
self.failUnless(rset) |
|
371 |
self.assertEquals(set(e.e_schema.type for e in rset.entities(0)), |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
372 |
set(['CWUser',])) |
0 | 373 |
self.assertEquals(set(e.e_schema.type for e in rset.entities(1)), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
374 |
set(['CWGroup',])) |
0 | 375 |
|
1640 | 376 |
def test_printable_rql(self): |
2635
c94df21f7ab2
F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2286
diff
changeset
|
377 |
rset = self.execute(u'CWEType X WHERE X final FALSE') |
0 | 378 |
self.assertEquals(rset.printable_rql(), |
2635
c94df21f7ab2
F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2286
diff
changeset
|
379 |
'Any X WHERE X final FALSE, X is CWEType') |
0 | 380 |
|
381 |
def test_searched_text(self): |
|
382 |
rset = self.execute(u'Any X WHERE X has_text "foobar"') |
|
383 |
self.assertEquals(rset.searched_text(), 'foobar') |
|
384 |
rset = self.execute(u'Any X WHERE X has_text %(text)s', {'text' : 'foo'}) |
|
385 |
self.assertEquals(rset.searched_text(), 'foo') |
|
1640 | 386 |
|
4939
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
387 |
def test_union_limited_rql(self): |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
388 |
rset = self.execute('(Any X,N WHERE X is Bookmark, X title N)' |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
389 |
' UNION ' |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
390 |
'(Any X,N WHERE X is CWGroup, X name N)') |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
391 |
rset.limit(2, 10, inplace=True) |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
392 |
self.assertEquals(rset.limited_rql(), |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
393 |
'Any A,B LIMIT 2 OFFSET 10 ' |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
394 |
'WITH A,B BEING (' |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
395 |
'(Any X,N WHERE X is Bookmark, X title N) ' |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
396 |
'UNION ' |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
397 |
'(Any X,N WHERE X is CWGroup, X name N)' |
349af486f5ed
fix limited_rql w/ UNION query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4536
diff
changeset
|
398 |
')') |
1640 | 399 |
|
0 | 400 |
if __name__ == '__main__': |
401 |
unittest_main() |