author | sylvain.thenault@logilab.fr |
Wed, 29 Apr 2009 09:03:31 +0200 | |
branch | tls-sprint |
changeset 1530 | 849fd3d64f11 |
parent 1398 | 5fe84a5f7035 |
child 1802 | d628defebc17 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: iso-8859-1 -*- |
2 |
"""unit tests for modules cubicweb.server.rqlannotation |
|
3 |
""" |
|
4 |
||
5 |
from cubicweb.devtools import init_test_database |
|
6 |
from cubicweb.devtools.repotest import BaseQuerierTC |
|
7 |
||
8 |
repo, cnx = init_test_database('sqlite') |
|
9 |
||
10 |
class SQLGenAnnotatorTC(BaseQuerierTC): |
|
11 |
repo = repo |
|
12 |
||
13 |
def get_max_eid(self): |
|
14 |
# no need for cleanup here |
|
15 |
return None |
|
16 |
def cleanup(self): |
|
17 |
# no need for cleanup here |
|
18 |
pass |
|
19 |
||
20 |
def test_0_1(self): |
|
21 |
rqlst = self._prepare('Any SEN,RN,OEN WHERE X from_entity SE, SE eid 44, X relation_type R, R eid 139, X to_entity OE, OE eid 42, R name RN, SE name SEN, OE name OEN') |
|
22 |
self.assertEquals(rqlst.defined_vars['SE']._q_invariant, False) |
|
23 |
self.assertEquals(rqlst.defined_vars['OE']._q_invariant, False) |
|
24 |
self.assertEquals(rqlst.defined_vars['R']._q_invariant, False) |
|
25 |
self.assertEquals(rqlst.defined_vars['SE'].stinfo['attrvar'], None) |
|
26 |
self.assertEquals(rqlst.defined_vars['OE'].stinfo['attrvar'], None) |
|
27 |
self.assertEquals(rqlst.defined_vars['R'].stinfo['attrvar'], None) |
|
28 |
||
29 |
def test_0_2(self): |
|
30 |
rqlst = self._prepare('Any O WHERE NOT S ecrit_par O, S eid 1, S inline1 P, O inline2 P') |
|
31 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, True) |
|
32 |
self.assertEquals(rqlst.defined_vars['O'].stinfo['attrvar'], None) |
|
33 |
||
34 |
def test_0_4(self): |
|
35 |
rqlst = self._prepare('Any A,B,C WHERE A eid 12,A comment B, A ?wf_info_for C') |
|
36 |
self.assertEquals(rqlst.defined_vars['A']._q_invariant, False) |
|
37 |
self.assert_(rqlst.defined_vars['B'].stinfo['attrvar']) |
|
38 |
self.assertEquals(rqlst.defined_vars['C']._q_invariant, False) |
|
39 |
self.assertEquals(rqlst.solutions, [{'A': 'TrInfo', 'B': 'String', 'C': 'Affaire'}, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
40 |
{'A': 'TrInfo', 'B': 'String', 'C': 'CWUser'}, |
0 | 41 |
{'A': 'TrInfo', 'B': 'String', 'C': 'Note'}]) |
42 |
||
43 |
def test_0_5(self): |
|
44 |
rqlst = self._prepare('Any P WHERE N ecrit_par P, N eid 0') |
|
45 |
self.assertEquals(rqlst.defined_vars['N']._q_invariant, False) |
|
46 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, True) |
|
47 |
||
48 |
def test_0_6(self): |
|
49 |
rqlst = self._prepare('Any P WHERE NOT N ecrit_par P, N eid 512') |
|
50 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, False) |
|
51 |
||
52 |
def test_0_7(self): |
|
53 |
rqlst = self._prepare('Personne X,Y where X nom NX, Y nom NX, X eid XE, not Y eid XE') |
|
54 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
55 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
56 |
self.assert_(rqlst.defined_vars['XE'].stinfo['attrvar']) |
|
57 |
||
58 |
def test_0_8(self): |
|
59 |
rqlst = self._prepare('Any P WHERE X eid 0, NOT X connait P') |
|
60 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, False) |
|
61 |
#self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
62 |
self.assertEquals(len(rqlst.solutions), 1, rqlst.solutions) |
|
63 |
||
64 |
def test_0_10(self): |
|
65 |
rqlst = self._prepare('Any X WHERE X concerne Y, Y is Note') |
|
66 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
67 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
68 |
||
69 |
def test_0_11(self): |
|
70 |
rqlst = self._prepare('Any X WHERE X todo_by Y, X is Affaire') |
|
71 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
72 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, True) |
|
73 |
||
74 |
def test_0_12(self): |
|
75 |
rqlst = self._prepare('Personne P WHERE P concerne A, A concerne S, S nom "Logilab"') |
|
76 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, True) |
|
77 |
self.assertEquals(rqlst.defined_vars['A']._q_invariant, True) |
|
78 |
self.assertEquals(rqlst.defined_vars['S']._q_invariant, False) |
|
79 |
||
80 |
def test_1_0(self): |
|
81 |
rqlst = self._prepare('Any X,Y WHERE X created_by Y, X eid 5, NOT Y eid 6') |
|
82 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, True) |
|
83 |
||
84 |
def test_1_1(self): |
|
85 |
rqlst = self._prepare('Any X,Y WHERE X created_by Y, X eid 5, NOT Y eid IN (6,7)') |
|
86 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, True) |
|
87 |
||
88 |
def test_2(self): |
|
89 |
rqlst = self._prepare('Any X WHERE X identity Y, Y eid 1') |
|
90 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
91 |
||
92 |
def test_7(self): |
|
93 |
rqlst = self._prepare('Personne X,Y where X nom NX, Y nom NX, X eid XE, not Y eid XE') |
|
94 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
95 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
96 |
||
97 |
def test_optional_inlined(self): |
|
98 |
rqlst = self._prepare('Any X,S where X from_state S?') |
|
99 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
100 |
self.assertEquals(rqlst.defined_vars['S']._q_invariant, True) |
|
101 |
||
102 |
def test_optional_inlined_2(self): |
|
103 |
rqlst = self._prepare('Any N,A WHERE N? inline1 A') |
|
104 |
self.assertEquals(rqlst.defined_vars['N']._q_invariant, False) |
|
105 |
self.assertEquals(rqlst.defined_vars['A']._q_invariant, False) |
|
106 |
||
107 |
def test_optional_1(self): |
|
108 |
rqlst = self._prepare('Any X,S WHERE X travaille S?') |
|
109 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
110 |
self.assertEquals(rqlst.defined_vars['S']._q_invariant, True) |
|
111 |
||
112 |
def test_greater_eid(self): |
|
113 |
rqlst = self._prepare('Any X WHERE X eid > 5') |
|
114 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
115 |
||
116 |
def test_greater_eid_typed(self): |
|
117 |
rqlst = self._prepare('Any X WHERE X eid > 5, X is Note') |
|
118 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
119 |
||
120 |
def test_max_eid(self): |
|
121 |
rqlst = self._prepare('Any MAX(X)') |
|
122 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
123 |
||
124 |
def test_max_eid_typed(self): |
|
125 |
rqlst = self._prepare('Any MAX(X) WHERE X is Note') |
|
126 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
127 |
||
128 |
def test_all_entities(self): |
|
129 |
rqlst = self._prepare('Any X') |
|
130 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
131 |
||
132 |
def test_all_typed_entity(self): |
|
133 |
rqlst = self._prepare('Any X WHERE X is Note') |
|
134 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
135 |
||
136 |
def test_has_text_1(self): |
|
137 |
rqlst = self._prepare('Any X WHERE X has_text "toto tata"') |
|
138 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
139 |
self.assertEquals(rqlst.defined_vars['X'].stinfo['principal'].r_type, 'has_text') |
|
140 |
||
141 |
def test_has_text_2(self): |
|
142 |
rqlst = self._prepare('Any X WHERE X is Personne, X has_text "coucou"') |
|
143 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
144 |
self.assertEquals(rqlst.defined_vars['X'].stinfo['principal'].r_type, 'has_text') |
|
145 |
||
146 |
def test_not_relation_1(self): |
|
147 |
# P can't be invariant since deambiguification caused by "NOT X require_permission P" |
|
148 |
# is not considered by generated sql (NOT EXISTS(...)) |
|
149 |
rqlst = self._prepare('Any P,G WHERE P require_group G, NOT X require_permission P') |
|
150 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, False) |
|
151 |
self.assertEquals(rqlst.defined_vars['G']._q_invariant, True) |
|
152 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
153 |
||
154 |
def test_not_relation_2(self): |
|
155 |
rqlst = self._prepare('TrInfo X WHERE X eid 2, NOT X from_state Y, Y is State') |
|
156 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, True) |
|
157 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
158 |
||
159 |
def test_not_relation_3(self): |
|
160 |
rqlst = self._prepare('Any X, Y WHERE X eid 1, Y eid in (2, 3)') |
|
161 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
162 |
||
163 |
def test_not_relation_4_1(self): |
|
164 |
rqlst = self._prepare('Note X WHERE NOT Y evaluee X') |
|
165 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
166 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, True) |
|
167 |
||
168 |
def test_not_relation_4_2(self): |
|
169 |
rqlst = self._prepare('Any X WHERE NOT Y evaluee X') |
|
170 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
171 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, True) |
|
172 |
||
173 |
def test_not_relation_4_3(self): |
|
174 |
rqlst = self._prepare('Any Y WHERE NOT Y evaluee X') |
|
175 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
176 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
177 |
||
178 |
def test_not_relation_4_4(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
179 |
rqlst = self._prepare('Any X WHERE NOT Y evaluee X, Y is CWUser') |
0 | 180 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
181 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
182 |
||
183 |
def test_not_relation_4_5(self): |
|
184 |
rqlst = self._prepare('Any X WHERE NOT Y evaluee X, Y eid %s, X is Note' % self.ueid) |
|
185 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
186 |
self.assertEquals(rqlst.solutions, [{'X': 'Note'}]) |
|
187 |
||
188 |
def test_not_relation_5_1(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
189 |
rqlst = self._prepare('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y') |
0 | 190 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
191 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
192 |
||
193 |
def test_not_relation_5_2(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
194 |
rqlst = self._prepare('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT X read_permission Y') |
0 | 195 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
196 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
197 |
||
198 |
def test_not_relation_6(self): |
|
199 |
rqlst = self._prepare('Personne P where NOT P concerne A') |
|
200 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, False) |
|
201 |
self.assertEquals(rqlst.defined_vars['A']._q_invariant, True) |
|
202 |
||
203 |
def test_not_relation_7(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
204 |
rqlst = self._prepare('Any K,V WHERE P is CWProperty, P pkey K, P value V, NOT P for_user U') |
0 | 205 |
self.assertEquals(rqlst.defined_vars['P']._q_invariant, False) |
206 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, True) |
|
207 |
||
208 |
def test_exists_1(self): |
|
209 |
rqlst = self._prepare('Any U WHERE U eid IN (1,2), EXISTS(X owned_by U)') |
|
210 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, False) |
|
211 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
212 |
||
213 |
def test_exists_2(self): |
|
214 |
rqlst = self._prepare('Any U WHERE EXISTS(U eid IN (1,2), X owned_by U)') |
|
215 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, False) |
|
216 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
217 |
||
218 |
def test_exists_3(self): |
|
219 |
rqlst = self._prepare('Any U WHERE EXISTS(X owned_by U, X bookmarked_by U)') |
|
220 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, False) |
|
221 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
222 |
||
223 |
def test_exists_4(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
224 |
rqlst = self._prepare('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)') |
0 | 225 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
226 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
|
227 |
||
228 |
def test_exists_5(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
229 |
rqlst = self._prepare('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), EXISTS(X read_permission Y)') |
0 | 230 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
231 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, True) |
|
232 |
||
233 |
def test_not_exists_1(self): |
|
234 |
rqlst = self._prepare('Any U WHERE NOT EXISTS(X owned_by U, X bookmarked_by U)') |
|
235 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, False) |
|
236 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
237 |
||
238 |
def test_not_exists_2(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
239 |
rqlst = self._prepare('Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT EXISTS(X read_permission Y)') |
0 | 240 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
241 |
||
242 |
def test_not_exists_distinct_1(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
967
diff
changeset
|
243 |
rqlst = self._prepare('DISTINCT Any X,Y WHERE X name "CWGroup", Y eid IN(1, 2, 3), NOT EXISTS(X read_permission Y)') |
0 | 244 |
self.assertEquals(rqlst.defined_vars['Y']._q_invariant, False) |
245 |
||
246 |
def test_or_1(self): |
|
247 |
rqlst = self._prepare('Any X WHERE X concerne B OR C concerne X, B eid 12, C eid 13') |
|
248 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, False) |
|
249 |
||
250 |
def test_or_2(self): |
|
251 |
rqlst = self._prepare('Any X WHERE X created_by U, X concerne B OR C concerne X, B eid 12, C eid 13') |
|
252 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
253 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, True) |
|
254 |
self.assertEquals(rqlst.defined_vars['X'].stinfo['principal'].r_type, 'created_by') |
|
255 |
||
256 |
def test_or_3(self): |
|
257 |
rqlst = self._prepare('Any N WHERE A evaluee N or EXISTS(N todo_by U)') |
|
258 |
self.assertEquals(rqlst.defined_vars['N']._q_invariant, False) |
|
259 |
self.assertEquals(rqlst.defined_vars['A']._q_invariant, True) |
|
260 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, True) |
|
261 |
||
262 |
def test_or_exists_1(self): |
|
263 |
# query generated by security rewriting |
|
264 |
rqlst = self._prepare('DISTINCT Any A,S WHERE A is Affaire, S nom "chouette", S is IN(Division, Societe, SubDivision),' |
|
265 |
'(EXISTS(A owned_by %(D)s)) ' |
|
266 |
'OR ((((EXISTS(E concerne C?, C owned_by %(D)s, A identity E, C is Note, E is Affaire)) ' |
|
267 |
'OR (EXISTS(I concerne H?, H owned_by %(D)s, H is Societe, A identity I, I is Affaire))) ' |
|
268 |
'OR (EXISTS(J concerne G?, G owned_by %(D)s, G is SubDivision, A identity J, J is Affaire))) ' |
|
269 |
'OR (EXISTS(K concerne F?, F owned_by %(D)s, F is Division, A identity K, K is Affaire)))') |
|
270 |
self.assertEquals(rqlst.defined_vars['A']._q_invariant, False) |
|
271 |
self.assertEquals(rqlst.defined_vars['S']._q_invariant, False) |
|
272 |
||
273 |
def test_or_exists_2(self): |
|
274 |
rqlst = self._prepare('Any U WHERE EXISTS(U in_group G, G name "managers") OR EXISTS(X owned_by U, X bookmarked_by U)') |
|
275 |
self.assertEquals(rqlst.defined_vars['U']._q_invariant, False) |
|
276 |
self.assertEquals(rqlst.defined_vars['G']._q_invariant, False) |
|
277 |
self.assertEquals(rqlst.defined_vars['X']._q_invariant, True) |
|
278 |
||
279 |
def test_or_exists_3(self): |
|
280 |
rqlst = self._prepare('Any COUNT(S),CS GROUPBY CS ORDERBY 1 DESC LIMIT 10 ' |
|
281 |
'WHERE C is Societe, S concerne C, C nom CS, ' |
|
282 |
'(EXISTS(S owned_by 1)) OR (EXISTS(S documented_by N, N title "published"))') |
|
283 |
self.assertEquals(rqlst.defined_vars['S']._q_invariant, True) |
|
284 |
rqlst = self._prepare('Any COUNT(S),CS GROUPBY CS ORDERBY 1 DESC LIMIT 10 ' |
|
285 |
'WHERE S is Affaire, C is Societe, S concerne C, C nom CS, ' |
|
286 |
'(EXISTS(S owned_by 1)) OR (EXISTS(S documented_by N, N title "published"))') |
|
287 |
self.assertEquals(rqlst.defined_vars['S']._q_invariant, True) |
|
288 |
||
967 | 289 |
def test_nonregr_ambiguity(self): |
290 |
rqlst = self._prepare('Note N WHERE N attachment F') |
|
291 |
# N may be an image as well, not invariant |
|
292 |
self.assertEquals(rqlst.defined_vars['N']._q_invariant, False) |
|
293 |
self.assertEquals(rqlst.defined_vars['F']._q_invariant, True) |
|
294 |
||
0 | 295 |
if __name__ == '__main__': |
296 |
from logilab.common.testlib import unittest_main |
|
297 |
unittest_main() |