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