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