author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 26 Feb 2010 13:24:47 +0100 | |
branch | stable |
changeset 4721 | 8f63691ccb7f |
parent 4536 | 02d0803a60b8 |
child 4850 | bd640b137f50 |
child 4939 | 349af486f5ed |
permissions | -rw-r--r-- |
0 | 1 |
# coding: utf-8 |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3589
diff
changeset
|
2 |
"""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
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1640
diff
changeset
|
4 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3587
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1640
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1640
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1640
diff
changeset
|
8 |
""" |
4536
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
9 |
|
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
10 |
from urlparse import urlsplit |
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
11 |
|
02d0803a60b8
cleanup 2.5 __future__ imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4461
diff
changeset
|
12 |
from rql import parse |
0 | 13 |
|
14 |
from logilab.common.testlib import TestCase, unittest_main |
|
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
|
15 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
16 |
from cubicweb.devtools.testlib import CubicWebTC |
0 | 17 |
from cubicweb.rset import NotAnEntity, ResultSet, attr_desc_iterator |
18 |
||
1640 | 19 |
|
0 | 20 |
def pprelcachedict(d): |
21 |
res = {} |
|
22 |
for k, (rset, related) in d.items(): |
|
23 |
res[k] = sorted(v.eid for v in related) |
|
24 |
return sorted(res.items()) |
|
1640 | 25 |
|
0 | 26 |
|
27 |
class AttrDescIteratorTC(TestCase): |
|
28 |
"""TestCase for cubicweb.rset.attr_desc_iterator""" |
|
1640 | 29 |
|
0 | 30 |
def test_relations_description(self): |
31 |
"""tests relations_description() function""" |
|
32 |
queries = { |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
33 |
'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
|
34 |
'Any U,L,M where U is CWUser, L is Foo, U mail M' : [(2, 'mail', 'subject')], |
0 | 35 |
'Any C,P where C is Company, C employs P' : [(1, 'employs', 'subject')], |
36 |
'Any C,P where C is Company, P employed_by P' : [], |
|
37 |
'Any C where C is Company, C employs P' : [], |
|
38 |
} |
|
39 |
for rql, relations in queries.items(): |
|
40 |
result = list(attr_desc_iterator(parse(rql).children[0])) |
|
41 |
self.assertEquals((rql, result), (rql, relations)) |
|
1640 | 42 |
|
0 | 43 |
def test_relations_description_indexed(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 C,U,P,L,M where C is Company, C employs P, U is CWUser, U login L, U mail M' : |
0 | 47 |
{0: [(2,'employs', 'subject')], 1: [(3,'login', 'subject'), (4,'mail', 'subject')]}, |
48 |
} |
|
49 |
for rql, results in queries.items(): |
|
50 |
for var_index, relations in results.items(): |
|
51 |
result = list(attr_desc_iterator(parse(rql).children[0], var_index)) |
|
52 |
self.assertEquals(result, relations) |
|
53 |
||
54 |
||
55 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
56 |
class ResultSetTC(CubicWebTC): |
0 | 57 |
|
58 |
def setUp(self): |
|
59 |
super(ResultSetTC, self).setUp() |
|
60 |
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
|
61 |
'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
|
62 |
description=[['CWUser', 'String'], ['Bar', 'String']]) |
0 | 63 |
self.rset.vreg = self.vreg |
64 |
||
65 |
def compare_urls(self, url1, url2): |
|
66 |
info1 = urlsplit(url1) |
|
67 |
info2 = urlsplit(url2) |
|
68 |
self.assertEquals(info1[:3], info2[:3]) |
|
69 |
if info1[3] != info2[3]: |
|
70 |
params1 = dict(pair.split('=') for pair in info1[3].split('&')) |
|
71 |
params2 = dict(pair.split('=') for pair in info1[3].split('&')) |
|
72 |
self.assertDictEquals(params1, params2) |
|
73 |
||
1640 | 74 |
|
0 | 75 |
def test_build_url(self): |
76 |
req = self.request() |
|
77 |
baseurl = req.base_url() |
|
78 |
self.compare_urls(req.build_url('view', vid='foo', rql='yo'), |
|
79 |
'%sview?vid=foo&rql=yo' % baseurl) |
|
80 |
self.compare_urls(req.build_url('view', _restpath='task/title/go'), |
|
81 |
'%stask/title/go' % baseurl) |
|
82 |
#self.compare_urls(req.build_url('view', _restpath='/task/title/go'), |
|
83 |
# '%stask/title/go' % baseurl) |
|
84 |
# empty _restpath should not crash |
|
85 |
self.compare_urls(req.build_url('view', _restpath=''), baseurl) |
|
1640 | 86 |
|
87 |
||
0 | 88 |
def test_resultset_build(self): |
89 |
"""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
|
90 |
rs = ResultSet([1,2,3], 'CWGroup X', description=['CWGroup', 'CWGroup', 'CWGroup']) |
0 | 91 |
self.assertEquals(rs.rowcount, 3) |
92 |
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
|
93 |
self.assertEquals(rs.description, ['CWGroup', 'CWGroup', 'CWGroup']) |
0 | 94 |
|
95 |
||
96 |
def test_resultset_limit(self): |
|
97 |
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
|
98 |
'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
|
99 |
description=[['CWUser', 'String']] * 3) |
0 | 100 |
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
|
101 |
rs.vreg = self.vreg |
0 | 102 |
|
103 |
self.assertEquals(rs.limit(2).rows, [[12000, 'adim'], [13000, 'syt']]) |
|
104 |
rs2 = rs.limit(2, offset=1) |
|
105 |
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
|
106 |
self.assertEquals(rs2.get_entity(0, 0).cw_row, 0) |
0 | 107 |
self.assertEquals(rs.limit(2, offset=2).rows, [[14000, 'nico']]) |
108 |
self.assertEquals(rs.limit(2, offset=3).rows, []) |
|
1640 | 109 |
|
0 | 110 |
|
111 |
def test_resultset_filter(self): |
|
112 |
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
|
113 |
'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
|
114 |
description=[['CWUser', 'String']] * 3) |
0 | 115 |
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
|
116 |
rs.vreg = self.vreg |
0 | 117 |
def test_filter(entity): |
118 |
return entity.login != 'nico' |
|
1640 | 119 |
|
0 | 120 |
rs2 = rs.filtered_rset(test_filter) |
121 |
self.assertEquals(len(rs2), 2) |
|
122 |
self.assertEquals([login for _, login in rs2], ['adim', 'syt']) |
|
1640 | 123 |
|
0 | 124 |
def test_resultset_transform(self): |
125 |
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
|
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() |
129 |
def test_transform(row, desc): |
|
130 |
return row[1:], desc[1:] |
|
131 |
rs2 = rs.transformed_rset(test_transform) |
|
132 |
||
133 |
self.assertEquals(len(rs2), 3) |
|
134 |
self.assertEquals(list(rs2), [['adim'],['syt'],['nico']]) |
|
1640 | 135 |
|
0 | 136 |
def test_resultset_sort(self): |
137 |
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
|
138 |
'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
|
139 |
description=[['CWUser', 'String']] * 3) |
0 | 140 |
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
|
141 |
rs.vreg = self.vreg |
1640 | 142 |
|
0 | 143 |
rs2 = rs.sorted_rset(lambda e:e['login']) |
144 |
self.assertEquals(len(rs2), 3) |
|
145 |
self.assertEquals([login for _, login in rs2], ['adim', 'nico', 'syt']) |
|
146 |
# make sure rs is unchanged |
|
147 |
self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico']) |
|
1640 | 148 |
|
0 | 149 |
rs2 = rs.sorted_rset(lambda e:e['login'], reverse=True) |
150 |
self.assertEquals(len(rs2), 3) |
|
151 |
self.assertEquals([login for _, login in rs2], ['syt', 'nico', 'adim']) |
|
152 |
# make sure rs is unchanged |
|
153 |
self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico']) |
|
154 |
||
155 |
rs3 = rs.sorted_rset(lambda row: row[1], col=-1) |
|
156 |
self.assertEquals(len(rs3), 3) |
|
157 |
self.assertEquals([login for _, login in rs3], ['adim', 'nico', 'syt']) |
|
158 |
# make sure rs is unchanged |
|
159 |
self.assertEquals([login for _, login in rs], ['adim', 'syt', 'nico']) |
|
160 |
||
161 |
def test_resultset_split(self): |
|
162 |
rs = ResultSet([[12000, 'adim', u'Adim chez les pinguins'], |
|
163 |
[12000, 'adim', u'Jardiner facile'], |
|
164 |
[13000, 'syt', u'Le carrelage en 42 leçons'], |
|
165 |
[14000, 'nico', u'La tarte tatin en 15 minutes'], |
|
166 |
[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
|
167 |
'Any U, L, T WHERE U is CWUser, U login L,'\ |
0 | 168 |
'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
|
169 |
description=[['CWUser', 'String', 'String']] * 5) |
0 | 170 |
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
|
171 |
rs.vreg = self.vreg |
1640 | 172 |
|
0 | 173 |
rsets = rs.split_rset(lambda e:e['login']) |
174 |
self.assertEquals(len(rsets), 3) |
|
175 |
self.assertEquals([login for _, login,_ in rsets[0]], ['adim', 'adim']) |
|
176 |
self.assertEquals([login for _, login,_ in rsets[1]], ['syt']) |
|
177 |
self.assertEquals([login for _, login,_ in rsets[2]], ['nico', 'nico']) |
|
178 |
# make sure rs is unchanged |
|
179 |
self.assertEquals([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico']) |
|
1640 | 180 |
|
0 | 181 |
rsets = rs.split_rset(lambda e:e['login'], return_dict=True) |
182 |
self.assertEquals(len(rsets), 3) |
|
183 |
self.assertEquals([login for _, login,_ in rsets['nico']], ['nico', 'nico']) |
|
184 |
self.assertEquals([login for _, login,_ in rsets['adim']], ['adim', 'adim']) |
|
185 |
self.assertEquals([login for _, login,_ in rsets['syt']], ['syt']) |
|
186 |
# make sure rs is unchanged |
|
187 |
self.assertEquals([login for _, login,_ in rs], ['adim', 'adim', 'syt', 'nico', 'nico']) |
|
188 |
||
189 |
rsets = rs.split_rset(lambda s: s.count('d'), col=2) |
|
190 |
self.assertEquals(len(rsets), 2) |
|
191 |
self.assertEquals([title for _, _, title in rsets[0]], |
|
192 |
[u"Adim chez les pinguins", |
|
193 |
u"Jardiner facile", |
|
194 |
u"L'épluchage du castor commun",]) |
|
195 |
self.assertEquals([title for _, _, title in rsets[1]], |
|
196 |
[u"Le carrelage en 42 leçons", |
|
197 |
u"La tarte tatin en 15 minutes",]) |
|
198 |
# make sure rs is unchanged |
|
199 |
self.assertEquals([title for _, _, title in rs], |
|
200 |
[u'Adim chez les pinguins', |
|
201 |
u'Jardiner facile', |
|
202 |
u'Le carrelage en 42 leçons', |
|
203 |
u'La tarte tatin en 15 minutes', |
|
204 |
u"L'épluchage du castor commun"]) |
|
1640 | 205 |
|
0 | 206 |
def test_cached_syntax_tree(self): |
207 |
"""make sure syntax tree is cached""" |
|
208 |
rqlst1 = self.rset.syntax_tree() |
|
209 |
rqlst2 = self.rset.syntax_tree() |
|
210 |
self.assert_(rqlst1 is rqlst2) |
|
211 |
||
212 |
def test_get_entity_simple(self): |
|
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
213 |
self.request().create_entity('CWUser', login=u'adim', upassword='adim', |
0 | 214 |
surname=u'di mascio', firstname=u'adrien') |
215 |
e = self.entity('Any X,T WHERE X login "adim", X surname T') |
|
216 |
self.assertEquals(e['surname'], 'di mascio') |
|
217 |
self.assertRaises(KeyError, e.__getitem__, 'firstname') |
|
218 |
self.assertRaises(KeyError, e.__getitem__, 'creation_date') |
|
219 |
self.assertEquals(pprelcachedict(e._related_cache), []) |
|
220 |
e.complete() |
|
221 |
self.assertEquals(e['firstname'], 'adrien') |
|
222 |
self.assertEquals(pprelcachedict(e._related_cache), []) |
|
1640 | 223 |
|
0 | 224 |
def test_get_entity_advanced(self): |
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
225 |
self.request().create_entity('Bookmark', title=u'zou', path=u'/view') |
0 | 226 |
self.execute('SET X bookmarked_by Y WHERE X is Bookmark, Y login "anon"') |
227 |
rset = self.execute('Any X,Y,XT,YN WHERE X bookmarked_by Y, X title XT, Y login YN') |
|
1640 | 228 |
|
0 | 229 |
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
|
230 |
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
|
231 |
self.assertEquals(e.cw_col, 0) |
0 | 232 |
self.assertEquals(e['title'], 'zou') |
233 |
self.assertRaises(KeyError, e.__getitem__, 'path') |
|
1640 | 234 |
self.assertEquals(e.view('text'), 'zou') |
0 | 235 |
self.assertEquals(pprelcachedict(e._related_cache), []) |
1640 | 236 |
|
0 | 237 |
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
|
238 |
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
|
239 |
self.assertEquals(e.cw_col, 1) |
0 | 240 |
self.assertEquals(e['login'], 'anon') |
241 |
self.assertRaises(KeyError, e.__getitem__, 'firstname') |
|
242 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
243 |
[]) |
|
244 |
e.complete() |
|
245 |
self.assertEquals(e['firstname'], None) |
|
246 |
self.assertEquals(e.view('text'), 'anon') |
|
247 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
248 |
[]) |
|
1640 | 249 |
|
0 | 250 |
self.assertRaises(NotAnEntity, rset.get_entity, 0, 2) |
251 |
self.assertRaises(NotAnEntity, rset.get_entity, 0, 3) |
|
252 |
||
253 |
def test_get_entity_relation_cache_compt(self): |
|
254 |
rset = self.execute('Any X,S WHERE X in_state S, X login "anon"') |
|
255 |
e = rset.get_entity(0, 0) |
|
256 |
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
|
257 |
# for_user / in_group are prefetched in CWUser __init__, in_state should |
0 | 258 |
# be filed from our query rset |
259 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
260 |
[('in_state_subject', [seid])]) |
|
261 |
||
262 |
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
|
263 |
e = self.request().create_entity('Bookmark', title=u'zou', path=u'path') |
0 | 264 |
self.commit() |
265 |
rset = self.execute('Any X,U,S,XT,UL,SN WHERE X created_by U, U in_state S, ' |
|
266 |
'X title XT, S name SN, U login UL, X eid %s' % e.eid) |
|
267 |
e = rset.get_entity(0, 0) |
|
268 |
self.assertEquals(e['title'], 'zou') |
|
269 |
self.assertEquals(pprelcachedict(e._related_cache), |
|
270 |
[('created_by_subject', [5])]) |
|
271 |
# first level of recursion |
|
272 |
u = e.created_by[0] |
|
273 |
self.assertEquals(u['login'], 'admin') |
|
274 |
self.assertRaises(KeyError, u.__getitem__, 'firstname') |
|
275 |
# second level of recursion |
|
276 |
s = u.in_state[0] |
|
277 |
self.assertEquals(s['name'], 'activated') |
|
278 |
self.assertRaises(KeyError, s.__getitem__, 'description') |
|
279 |
||
1640 | 280 |
|
0 | 281 |
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
|
282 |
eid = self.execute('INSERT CWUser E: E login "joe", E upassword "joe", E in_group G ' |
0 | 283 |
'WHERE G name "users"')[0][0] |
284 |
rset = self.execute('Any X,E WHERE X eid %(x)s, X primary_email E?', {'x': eid}) |
|
285 |
e = rset.get_entity(0, 0) |
|
286 |
# if any of the assertion below fails with a KeyError, the relation is not cached |
|
287 |
# related entities should be an empty list |
|
3587 | 288 |
self.assertEquals(e.related_cache('primary_email', 'subject', True), ()) |
0 | 289 |
# related rset should be an empty rset |
290 |
cached = e.related_cache('primary_email', 'subject', False) |
|
291 |
self.assertIsInstance(cached, ResultSet) |
|
292 |
self.assertEquals(cached.rowcount, 0) |
|
1640 | 293 |
|
0 | 294 |
|
295 |
def test_get_entity_union(self): |
|
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
296 |
e = self.request().create_entity('Bookmark', title=u'manger', path=u'path') |
0 | 297 |
rset = self.execute('Any X,N ORDERBY N WITH X,N BEING ' |
298 |
'((Any X,N WHERE X is Bookmark, X title N)' |
|
299 |
' UNION ' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
300 |
' (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
|
301 |
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
|
302 |
('Bookmark', 'manger'), ('CWGroup', 'owners'), |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
692
diff
changeset
|
303 |
('CWGroup', 'users')) |
0 | 304 |
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
|
305 |
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
|
306 |
self.assertEquals(entity.__regid__, etype) |
0 | 307 |
attr = etype == 'Bookmark' and 'title' or 'name' |
308 |
self.assertEquals(entity[attr], n) |
|
1640 | 309 |
|
572
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
310 |
def test_related_entity_optional(self): |
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
311 |
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
|
312 |
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
|
313 |
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
|
314 |
self.assertEquals(entity, None) |
9849fed789c9
test and fix potential error with None optional relation
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
315 |
self.assertEquals(rtype, None) |
1640 | 316 |
|
0 | 317 |
def test_related_entity_union_subquery(self): |
4461
a35c76ffed92
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4427
diff
changeset
|
318 |
e = self.request().create_entity('Bookmark', title=u'aaaa', path=u'path') |
0 | 319 |
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
|
320 |
'((Any X,N WHERE X is CWGroup, X name N)' |
0 | 321 |
' UNION ' |
322 |
' (Any X,N WHERE X is Bookmark, X title N))') |
|
323 |
entity, rtype = rset.related_entity(0, 1) |
|
324 |
self.assertEquals(entity.eid, e.eid) |
|
325 |
self.assertEquals(rtype, 'title') |
|
326 |
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
|
327 |
self.assertEquals(entity.__regid__, 'CWGroup') |
0 | 328 |
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
|
329 |
# |
0 | 330 |
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
|
331 |
'((Any X,N WHERE X is CWGroup, X name N)' |
0 | 332 |
' UNION ' |
333 |
' (Any X,N WHERE X is Bookmark, X title N))') |
|
334 |
entity, rtype = rset.related_entity(0, 1) |
|
335 |
self.assertEquals(entity.eid, e.eid) |
|
336 |
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
|
337 |
# |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
338 |
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
|
339 |
'((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
|
340 |
' UNION ' |
5787d1cc8106
[rset] fix #231354 w/ rql 0.22.3 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2635
diff
changeset
|
341 |
' (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
|
342 |
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
|
343 |
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
|
344 |
self.assertEquals(rtype, 'title') |
1640 | 345 |
|
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
|
346 |
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
|
347 |
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
|
348 |
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
|
349 |
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
|
350 |
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
|
351 |
'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
|
352 |
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
|
353 |
|
0 | 354 |
def test_entities(self): |
355 |
rset = self.execute('Any U,G WHERE U in_group G') |
|
356 |
# make sure we have at least one element |
|
357 |
self.failUnless(rset) |
|
358 |
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
|
359 |
set(['CWUser',])) |
0 | 360 |
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
|
361 |
set(['CWGroup',])) |
0 | 362 |
|
1640 | 363 |
def test_printable_rql(self): |
2635
c94df21f7ab2
F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2286
diff
changeset
|
364 |
rset = self.execute(u'CWEType X WHERE X final FALSE') |
0 | 365 |
self.assertEquals(rset.printable_rql(), |
2635
c94df21f7ab2
F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2286
diff
changeset
|
366 |
'Any X WHERE X final FALSE, X is CWEType') |
0 | 367 |
|
368 |
def test_searched_text(self): |
|
369 |
rset = self.execute(u'Any X WHERE X has_text "foobar"') |
|
370 |
self.assertEquals(rset.searched_text(), 'foobar') |
|
371 |
rset = self.execute(u'Any X WHERE X has_text %(text)s', {'text' : 'foo'}) |
|
372 |
self.assertEquals(rset.searched_text(), 'foo') |
|
1640 | 373 |
|
374 |
||
0 | 375 |
if __name__ == '__main__': |
376 |
unittest_main() |