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