author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 09 Jun 2009 14:07:57 +0200 | |
branch | stable |
changeset 2069 | 7c0a1b90b1c6 |
parent 1977 | 606923dff11b |
child 2074 | 9e268cb6202e |
permissions | -rw-r--r-- |
0 | 1 |
"""some utilities to ease repository testing |
2 |
||
3 |
This module contains functions to initialize a new repository. |
|
4 |
||
5 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 7 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
8 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 9 |
""" |
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
12 |
from pprint import pprint |
|
13 |
||
14 |
def tuplify(list): |
|
15 |
for i in range(len(list)): |
|
16 |
if type(list[i]) is not type(()): |
|
17 |
list[i] = tuple(list[i]) |
|
18 |
return list |
|
19 |
||
20 |
def snippet_cmp(a, b): |
|
21 |
a = (a[0], [e.expression for e in a[1]]) |
|
22 |
b = (b[0], [e.expression for e in b[1]]) |
|
23 |
return cmp(a, b) |
|
24 |
||
25 |
def test_plan(self, rql, expected, kwargs=None): |
|
26 |
plan = self._prepare_plan(rql, kwargs) |
|
27 |
self.planner.build_plan(plan) |
|
28 |
try: |
|
29 |
self.assertEquals(len(plan.steps), len(expected), |
|
30 |
'expected %s steps, got %s' % (len(expected), len(plan.steps))) |
|
31 |
# step order is important |
|
32 |
for i, step in enumerate(plan.steps): |
|
33 |
compare_steps(self, step.test_repr(), expected[i]) |
|
34 |
except AssertionError: |
|
35 |
pprint([step.test_repr() for step in plan.steps]) |
|
36 |
raise |
|
37 |
||
38 |
def compare_steps(self, step, expected): |
|
39 |
try: |
|
40 |
self.assertEquals(step[0], expected[0], 'expected step type %s, got %s' % (expected[0], step[0])) |
|
41 |
if len(step) > 2 and isinstance(step[1], list) and isinstance(expected[1], list): |
|
42 |
queries, equeries = step[1], expected[1] |
|
43 |
self.assertEquals(len(queries), len(equeries), |
|
44 |
'expected %s queries, got %s' % (len(equeries), len(queries))) |
|
45 |
for i, (rql, sol) in enumerate(queries): |
|
46 |
self.assertEquals(rql, equeries[i][0]) |
|
1789 | 47 |
self.assertEquals(sorted(sol), sorted(equeries[i][1])) |
0 | 48 |
idx = 2 |
49 |
else: |
|
50 |
idx = 1 |
|
51 |
self.assertEquals(step[idx:-1], expected[idx:-1], |
|
52 |
'expected step characteristic \n%s\n, got\n%s' % (expected[1:-1], step[1:-1])) |
|
53 |
self.assertEquals(len(step[-1]), len(expected[-1]), |
|
54 |
'got %s child steps, expected %s' % (len(step[-1]), len(expected[-1]))) |
|
55 |
except AssertionError: |
|
56 |
print 'error on step ', |
|
57 |
pprint(step[:-1]) |
|
58 |
raise |
|
59 |
children = step[-1] |
|
60 |
if step[0] in ('UnionFetchStep', 'UnionStep'): |
|
61 |
# sort children |
|
62 |
children = sorted(children) |
|
63 |
expectedchildren = sorted(expected[-1]) |
|
64 |
else: |
|
65 |
expectedchildren = expected[-1] |
|
66 |
for i, substep in enumerate(children): |
|
67 |
compare_steps(self, substep, expectedchildren[i]) |
|
68 |
||
69 |
||
70 |
class DumbOrderedDict(list): |
|
71 |
def __iter__(self): |
|
72 |
return self.iterkeys() |
|
73 |
def __contains__(self, key): |
|
74 |
return key in self.iterkeys() |
|
75 |
def __getitem__(self, key): |
|
1236
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
76 |
for key_, value in list.__iter__(self): |
0 | 77 |
if key == key_: |
78 |
return value |
|
79 |
raise KeyError(key) |
|
80 |
def iterkeys(self): |
|
81 |
return (x for x, y in list.__iter__(self)) |
|
82 |
def iteritems(self): |
|
83 |
return (x for x in list.__iter__(self)) |
|
1236
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
84 |
def items(self): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
85 |
return [x for x in list.__iter__(self)] |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
86 |
|
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
87 |
class DumbOrderedDict2(object): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
88 |
def __init__(self, origdict, sortkey): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
89 |
self.origdict = origdict |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
90 |
self.sortkey = sortkey |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
91 |
def __getattr__(self, attr): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
92 |
return getattr(self.origdict, attr) |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
93 |
def __iter__(self): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
94 |
return iter(sorted(self.origdict, key=self.sortkey)) |
0 | 95 |
|
96 |
||
97 |
from logilab.common.testlib import TestCase |
|
98 |
from rql import RQLHelper |
|
99 |
from cubicweb.devtools.fake import FakeRepo, FakeSession |
|
100 |
from cubicweb.server import set_debug |
|
101 |
from cubicweb.server.querier import QuerierHelper |
|
102 |
from cubicweb.server.session import Session |
|
103 |
from cubicweb.server.sources.rql2sql import remove_unused_solutions |
|
104 |
||
105 |
class RQLGeneratorTC(TestCase): |
|
106 |
schema = None # set this in concret test |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
107 |
|
0 | 108 |
def setUp(self): |
109 |
self.rqlhelper = RQLHelper(self.schema, special_relations={'eid': 'uid', |
|
110 |
'has_text': 'fti'}) |
|
111 |
self.qhelper = QuerierHelper(FakeRepo(self.schema), self.schema) |
|
112 |
ExecutionPlan._check_permissions = _dummy_check_permissions |
|
113 |
rqlannotation._select_principal = _select_principal |
|
114 |
||
115 |
def tearDown(self): |
|
116 |
ExecutionPlan._check_permissions = _orig_check_permissions |
|
117 |
rqlannotation._select_principal = _orig_select_principal |
|
2069
7c0a1b90b1c6
set_debug method on rql2sql test as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
118 |
|
7c0a1b90b1c6
set_debug method on rql2sql test as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
119 |
def set_debug(self, debug): |
7c0a1b90b1c6
set_debug method on rql2sql test as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
120 |
set_debug(debug) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
121 |
|
0 | 122 |
def _prepare(self, rql): |
123 |
#print '******************** prepare', rql |
|
124 |
union = self.rqlhelper.parse(rql) |
|
125 |
#print '********* parsed', union.as_string() |
|
126 |
self.rqlhelper.compute_solutions(union) |
|
127 |
#print '********* solutions', solutions |
|
128 |
self.rqlhelper.simplify(union) |
|
129 |
#print '********* simplified', union.as_string() |
|
130 |
plan = self.qhelper.plan_factory(union, {}, FakeSession()) |
|
131 |
plan.preprocess(union) |
|
132 |
for select in union.children: |
|
133 |
select.solutions.sort() |
|
134 |
#print '********* ppsolutions', solutions |
|
135 |
return union |
|
136 |
||
137 |
||
138 |
class BaseQuerierTC(TestCase): |
|
139 |
repo = None # set this in concret test |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
140 |
|
0 | 141 |
def setUp(self): |
142 |
self.o = self.repo.querier |
|
143 |
self.session = self.repo._sessions.values()[0] |
|
144 |
self.ueid = self.session.user.eid |
|
145 |
assert self.ueid != -1 |
|
146 |
self.repo._type_source_cache = {} # clear cache |
|
147 |
self.pool = self.session.set_pool() |
|
148 |
self.maxeid = self.get_max_eid() |
|
149 |
do_monkey_patch() |
|
150 |
||
151 |
def get_max_eid(self): |
|
152 |
return self.session.unsafe_execute('Any MAX(X)')[0][0] |
|
153 |
def cleanup(self): |
|
154 |
self.session.unsafe_execute('DELETE Any X WHERE X eid > %s' % self.maxeid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
155 |
|
0 | 156 |
def tearDown(self): |
157 |
undo_monkey_patch() |
|
158 |
self.session.rollback() |
|
159 |
self.cleanup() |
|
160 |
self.commit() |
|
161 |
self.repo._free_pool(self.pool) |
|
162 |
assert self.session.user.eid != -1 |
|
163 |
||
164 |
def set_debug(self, debug): |
|
165 |
set_debug(debug) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
166 |
|
0 | 167 |
def _rqlhelper(self): |
168 |
rqlhelper = self.o._rqlhelper |
|
169 |
# reset uid_func so it don't try to get type from eids |
|
170 |
rqlhelper._analyser.uid_func = None |
|
171 |
rqlhelper._analyser.uid_func_mapping = {} |
|
172 |
return rqlhelper |
|
173 |
||
174 |
def _prepare_plan(self, rql, kwargs=None): |
|
175 |
rqlhelper = self._rqlhelper() |
|
176 |
rqlst = rqlhelper.parse(rql) |
|
177 |
rqlhelper.compute_solutions(rqlst, kwargs=kwargs) |
|
178 |
rqlhelper.simplify(rqlst) |
|
179 |
for select in rqlst.children: |
|
180 |
select.solutions.sort() |
|
181 |
return self.o.plan_factory(rqlst, kwargs, self.session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
182 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
183 |
def _prepare(self, rql, kwargs=None): |
0 | 184 |
plan = self._prepare_plan(rql, kwargs) |
185 |
plan.preprocess(plan.rqlst) |
|
186 |
rqlst = plan.rqlst.children[0] |
|
187 |
rqlst.solutions = remove_unused_solutions(rqlst, rqlst.solutions, {}, self.repo.schema)[0] |
|
188 |
return rqlst |
|
189 |
||
190 |
def _user_session(self, groups=('guests',), ueid=None): |
|
191 |
# use self.session.user.eid to get correct owned_by relation, unless explicit eid |
|
192 |
if ueid is None: |
|
193 |
ueid = self.session.user.eid |
|
194 |
u = self.repo._build_user(self.session, ueid) |
|
195 |
u._groups = set(groups) |
|
196 |
s = Session(u, self.repo) |
|
197 |
s._threaddata.pool = self.pool |
|
198 |
return u, s |
|
199 |
||
200 |
def execute(self, rql, args=None, eid_key=None, build_descr=True): |
|
201 |
return self.o.execute(self.session, rql, args, eid_key, build_descr) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
202 |
|
0 | 203 |
def commit(self): |
204 |
self.session.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
205 |
self.session.set_pool() |
0 | 206 |
|
207 |
||
208 |
class BasePlannerTC(BaseQuerierTC): |
|
209 |
||
210 |
def _prepare_plan(self, rql, kwargs=None): |
|
211 |
rqlst = self.o.parse(rql, annotate=True) |
|
212 |
self.o.solutions(self.session, rqlst, kwargs) |
|
213 |
if rqlst.TYPE == 'select': |
|
214 |
self.o._rqlhelper.annotate(rqlst) |
|
215 |
for select in rqlst.children: |
|
216 |
select.solutions.sort() |
|
217 |
else: |
|
218 |
rqlst.solutions.sort() |
|
219 |
return self.o.plan_factory(rqlst, kwargs, self.session) |
|
220 |
||
221 |
||
222 |
# monkey patch some methods to get predicatable results ####################### |
|
223 |
||
224 |
from cubicweb.server.rqlrewrite import RQLRewriter |
|
225 |
_orig_insert_snippets = RQLRewriter.insert_snippets |
|
226 |
_orig_build_variantes = RQLRewriter.build_variantes |
|
227 |
||
228 |
def _insert_snippets(self, snippets, varexistsmap=None): |
|
229 |
_orig_insert_snippets(self, sorted(snippets, snippet_cmp), varexistsmap) |
|
230 |
||
231 |
def _build_variantes(self, newsolutions): |
|
232 |
variantes = _orig_build_variantes(self, newsolutions) |
|
233 |
sortedvariantes = [] |
|
234 |
for variante in variantes: |
|
1132 | 235 |
orderedkeys = sorted((k[1], k[2], v) for k, v in variante.iteritems()) |
0 | 236 |
variante = DumbOrderedDict(sorted(variante.iteritems(), |
1132 | 237 |
lambda a, b: cmp((a[0][1],a[0][2],a[1]), |
238 |
(b[0][1],b[0][2],b[1])))) |
|
0 | 239 |
sortedvariantes.append( (orderedkeys, variante) ) |
240 |
return [v for ok, v in sorted(sortedvariantes)] |
|
241 |
||
242 |
from cubicweb.server.querier import ExecutionPlan |
|
243 |
_orig_check_permissions = ExecutionPlan._check_permissions |
|
244 |
_orig_init_temp_table = ExecutionPlan.init_temp_table |
|
245 |
||
246 |
def _check_permissions(*args, **kwargs): |
|
247 |
res, restricted = _orig_check_permissions(*args, **kwargs) |
|
1132 | 248 |
res = DumbOrderedDict(sorted(res.iteritems(), lambda a, b: cmp(a[1], b[1]))) |
0 | 249 |
return res, restricted |
250 |
||
251 |
def _dummy_check_permissions(self, rqlst): |
|
252 |
return {(): rqlst.solutions}, set() |
|
253 |
||
254 |
def _init_temp_table(self, table, selection, solution): |
|
255 |
if self.tablesinorder is None: |
|
256 |
tablesinorder = self.tablesinorder = {} |
|
257 |
else: |
|
258 |
tablesinorder = self.tablesinorder |
|
259 |
if not table in tablesinorder: |
|
260 |
tablesinorder[table] = 'table%s' % len(tablesinorder) |
|
261 |
return _orig_init_temp_table(self, table, selection, solution) |
|
262 |
||
263 |
from cubicweb.server import rqlannotation |
|
264 |
_orig_select_principal = rqlannotation._select_principal |
|
265 |
||
266 |
def _select_principal(scope, relations): |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
0
diff
changeset
|
267 |
return _orig_select_principal(scope, relations, |
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
0
diff
changeset
|
268 |
_sort=lambda rels: sorted(rels, key=lambda x: x.r_type)) |
0 | 269 |
|
270 |
try: |
|
271 |
from cubicweb.server.msplanner import PartPlanInformation |
|
272 |
except ImportError: |
|
273 |
class PartPlanInformation(object): |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
274 |
def merge_input_maps(self, *args): |
0 | 275 |
pass |
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
276 |
def _choose_term(self, sourceterms): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
277 |
pass |
0 | 278 |
_orig_merge_input_maps = PartPlanInformation.merge_input_maps |
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
279 |
_orig_choose_term = PartPlanInformation._choose_term |
0 | 280 |
|
281 |
def _merge_input_maps(*args): |
|
282 |
return sorted(_orig_merge_input_maps(*args)) |
|
283 |
||
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
284 |
def _choose_term(self, sourceterms): |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
285 |
# predictable order for test purpose |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
286 |
def get_key(x): |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
287 |
try: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
288 |
# variable |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
289 |
return x.name |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
290 |
except AttributeError: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
291 |
try: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
292 |
# relation |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
293 |
return x.r_type |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
294 |
except AttributeError: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
295 |
# const |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
296 |
return x.value |
1236
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
297 |
return _orig_choose_term(self, DumbOrderedDict2(sourceterms, get_key)) |
0 | 298 |
|
299 |
||
300 |
def do_monkey_patch(): |
|
301 |
RQLRewriter.insert_snippets = _insert_snippets |
|
302 |
RQLRewriter.build_variantes = _build_variantes |
|
303 |
ExecutionPlan._check_permissions = _check_permissions |
|
304 |
ExecutionPlan.tablesinorder = None |
|
305 |
ExecutionPlan.init_temp_table = _init_temp_table |
|
306 |
PartPlanInformation.merge_input_maps = _merge_input_maps |
|
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
307 |
PartPlanInformation._choose_term = _choose_term |
0 | 308 |
|
309 |
def undo_monkey_patch(): |
|
310 |
RQLRewriter.insert_snippets = _orig_insert_snippets |
|
311 |
RQLRewriter.build_variantes = _orig_build_variantes |
|
312 |
ExecutionPlan._check_permissions = _orig_check_permissions |
|
313 |
ExecutionPlan.init_temp_table = _orig_init_temp_table |
|
314 |
PartPlanInformation.merge_input_maps = _orig_merge_input_maps |
|
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
315 |
PartPlanInformation._choose_term = _orig_choose_term |