author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 07 Apr 2010 14:26:35 +0200 | |
changeset 5174 | 78438ad513ca |
parent 4835 | 13b0b96d7982 |
child 5423 | e15abfdcce38 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
6 |
:copyright: 2003-2010 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 |
|
4766
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
98 |
def schema_eids_idx(schema): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
99 |
"""return a dictionary mapping schema types to their eids so we can reread |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
100 |
it from the fs instead of the db (too costly) between tests |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
101 |
""" |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
102 |
schema_eids = {} |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
103 |
for x in schema.entities(): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
104 |
schema_eids[x] = x.eid |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
105 |
for x in schema.relations(): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
106 |
schema_eids[x] = x.eid |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
107 |
for rdef in x.rdefs.itervalues(): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
108 |
schema_eids[(rdef.subject, rdef.rtype, rdef.object)] = rdef.eid |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
109 |
return schema_eids |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
110 |
|
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
111 |
def restore_schema_eids_idx(schema, schema_eids): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
112 |
"""rebuild schema eid index""" |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
113 |
for x in schema.entities(): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
114 |
x.eid = schema_eids[x] |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
115 |
schema._eid_index[x.eid] = x |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
116 |
for x in schema.relations(): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
117 |
x.eid = schema_eids[x] |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
118 |
schema._eid_index[x.eid] = x |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
119 |
for rdef in x.rdefs.itervalues(): |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
120 |
rdef.eid = schema_eids[(rdef.subject, rdef.rtype, rdef.object)] |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
121 |
schema._eid_index[rdef.eid] = rdef |
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4674
diff
changeset
|
122 |
|
0 | 123 |
|
124 |
from logilab.common.testlib import TestCase |
|
125 |
from rql import RQLHelper |
|
126 |
from cubicweb.devtools.fake import FakeRepo, FakeSession |
|
127 |
from cubicweb.server import set_debug |
|
128 |
from cubicweb.server.querier import QuerierHelper |
|
129 |
from cubicweb.server.session import Session |
|
130 |
from cubicweb.server.sources.rql2sql import remove_unused_solutions |
|
131 |
||
132 |
class RQLGeneratorTC(TestCase): |
|
133 |
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
|
134 |
|
0 | 135 |
def setUp(self): |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
136 |
self.repo = FakeRepo(self.schema) |
0 | 137 |
self.rqlhelper = RQLHelper(self.schema, special_relations={'eid': 'uid', |
138 |
'has_text': 'fti'}) |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
139 |
self.qhelper = QuerierHelper(self.repo, self.schema) |
0 | 140 |
ExecutionPlan._check_permissions = _dummy_check_permissions |
141 |
rqlannotation._select_principal = _select_principal |
|
142 |
||
143 |
def tearDown(self): |
|
144 |
ExecutionPlan._check_permissions = _orig_check_permissions |
|
145 |
rqlannotation._select_principal = _orig_select_principal |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2074
diff
changeset
|
146 |
|
2069
7c0a1b90b1c6
set_debug method on rql2sql test as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
147 |
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
|
148 |
set_debug(debug) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
149 |
|
0 | 150 |
def _prepare(self, rql): |
151 |
#print '******************** prepare', rql |
|
152 |
union = self.rqlhelper.parse(rql) |
|
153 |
#print '********* parsed', union.as_string() |
|
154 |
self.rqlhelper.compute_solutions(union) |
|
155 |
#print '********* solutions', solutions |
|
156 |
self.rqlhelper.simplify(union) |
|
157 |
#print '********* simplified', union.as_string() |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
158 |
plan = self.qhelper.plan_factory(union, {}, FakeSession(self.repo)) |
0 | 159 |
plan.preprocess(union) |
160 |
for select in union.children: |
|
161 |
select.solutions.sort() |
|
162 |
#print '********* ppsolutions', solutions |
|
163 |
return union |
|
164 |
||
165 |
||
166 |
class BaseQuerierTC(TestCase): |
|
167 |
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
|
168 |
|
0 | 169 |
def setUp(self): |
170 |
self.o = self.repo.querier |
|
171 |
self.session = self.repo._sessions.values()[0] |
|
172 |
self.ueid = self.session.user.eid |
|
173 |
assert self.ueid != -1 |
|
174 |
self.repo._type_source_cache = {} # clear cache |
|
175 |
self.pool = self.session.set_pool() |
|
176 |
self.maxeid = self.get_max_eid() |
|
177 |
do_monkey_patch() |
|
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
178 |
self._dumb_sessions = [] |
0 | 179 |
|
180 |
def get_max_eid(self): |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4773
diff
changeset
|
181 |
return self.session.execute('Any MAX(X)')[0][0] |
0 | 182 |
def cleanup(self): |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4773
diff
changeset
|
183 |
self.session.set_pool() |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4773
diff
changeset
|
184 |
self.session.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
|
185 |
|
0 | 186 |
def tearDown(self): |
187 |
undo_monkey_patch() |
|
188 |
self.session.rollback() |
|
189 |
self.cleanup() |
|
190 |
self.commit() |
|
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
191 |
# properly close dumb sessions |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
192 |
for session in self._dumb_sessions: |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
193 |
session.rollback() |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
194 |
session.close() |
0 | 195 |
self.repo._free_pool(self.pool) |
196 |
assert self.session.user.eid != -1 |
|
197 |
||
198 |
def set_debug(self, debug): |
|
199 |
set_debug(debug) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
200 |
|
0 | 201 |
def _rqlhelper(self): |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
202 |
rqlhelper = self.repo.vreg.rqlhelper |
0 | 203 |
# reset uid_func so it don't try to get type from eids |
204 |
rqlhelper._analyser.uid_func = None |
|
205 |
rqlhelper._analyser.uid_func_mapping = {} |
|
206 |
return rqlhelper |
|
207 |
||
4184
7002e91d304a
should not simplify previous to preprocess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
208 |
def _prepare_plan(self, rql, kwargs=None, simplify=True): |
0 | 209 |
rqlhelper = self._rqlhelper() |
210 |
rqlst = rqlhelper.parse(rql) |
|
211 |
rqlhelper.compute_solutions(rqlst, kwargs=kwargs) |
|
4184
7002e91d304a
should not simplify previous to preprocess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
212 |
if simplify: |
7002e91d304a
should not simplify previous to preprocess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
213 |
rqlhelper.simplify(rqlst) |
0 | 214 |
for select in rqlst.children: |
215 |
select.solutions.sort() |
|
216 |
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
|
217 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
218 |
def _prepare(self, rql, kwargs=None): |
4184
7002e91d304a
should not simplify previous to preprocess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
219 |
plan = self._prepare_plan(rql, kwargs, simplify=False) |
0 | 220 |
plan.preprocess(plan.rqlst) |
221 |
rqlst = plan.rqlst.children[0] |
|
222 |
rqlst.solutions = remove_unused_solutions(rqlst, rqlst.solutions, {}, self.repo.schema)[0] |
|
223 |
return rqlst |
|
224 |
||
4674
3d509dbb473a
[test api] rename _user_session to user_groups_session, fix its arguments and return only the session, not (user, session) to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
225 |
def user_groups_session(self, *groups): |
3d509dbb473a
[test api] rename _user_session to user_groups_session, fix its arguments and return only the session, not (user, session) to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
226 |
"""lightweight session using the current user with hi-jacked groups""" |
0 | 227 |
# use self.session.user.eid to get correct owned_by relation, unless explicit eid |
4674
3d509dbb473a
[test api] rename _user_session to user_groups_session, fix its arguments and return only the session, not (user, session) to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
228 |
u = self.repo._build_user(self.session, self.session.user.eid) |
0 | 229 |
u._groups = set(groups) |
230 |
s = Session(u, self.repo) |
|
231 |
s._threaddata.pool = self.pool |
|
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
232 |
# register session to ensure it gets closed |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
233 |
self._dumb_sessions.append(s) |
4674
3d509dbb473a
[test api] rename _user_session to user_groups_session, fix its arguments and return only the session, not (user, session) to make things clearer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
234 |
return s |
0 | 235 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
236 |
def execute(self, rql, args=None, build_descr=True): |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
237 |
return self.o.execute(self.session, rql, args, build_descr) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
238 |
|
0 | 239 |
def commit(self): |
240 |
self.session.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
241 |
self.session.set_pool() |
0 | 242 |
|
243 |
||
244 |
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
|
245 |
newsources = 0 |
2074
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
246 |
def setup(self): |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
247 |
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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
# XXX source_defs |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
252 |
self.o = self.repo.querier |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
253 |
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
|
254 |
self.pool = self.session.set_pool() |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
255 |
self.schema = self.o.schema |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
256 |
self.sources = self.o._repo.sources |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
257 |
self.system = self.sources[-1] |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
258 |
do_monkey_patch() |
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
259 |
self._dumb_sessions = [] # by hi-jacked parent setup |
2074
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
260 |
|
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
261 |
def add_source(self, sourcecls, uri): |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
262 |
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
|
263 |
{'uri': uri})) |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
264 |
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
|
265 |
setattr(self, uri, self.sources[-1]) |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
266 |
self.newsources += 1 |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
267 |
|
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
268 |
def tearDown(self): |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
269 |
while self.newsources: |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
270 |
source = self.sources.pop(-1) |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
271 |
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
|
272 |
self.newsources -= 1 |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
273 |
undo_monkey_patch() |
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
274 |
for session in self._dumb_sessions: |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
275 |
session._threaddata.pool = None |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
276 |
session.close() |
0 | 277 |
|
278 |
def _prepare_plan(self, rql, kwargs=None): |
|
279 |
rqlst = self.o.parse(rql, annotate=True) |
|
280 |
self.o.solutions(self.session, rqlst, kwargs) |
|
281 |
if rqlst.TYPE == 'select': |
|
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
282 |
self.repo.vreg.rqlhelper.annotate(rqlst) |
0 | 283 |
for select in rqlst.children: |
284 |
select.solutions.sort() |
|
285 |
else: |
|
286 |
rqlst.solutions.sort() |
|
287 |
return self.o.plan_factory(rqlst, kwargs, self.session) |
|
288 |
||
289 |
||
290 |
# monkey patch some methods to get predicatable results ####################### |
|
291 |
||
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
292 |
from cubicweb.rqlrewrite import RQLRewriter |
0 | 293 |
_orig_insert_snippets = RQLRewriter.insert_snippets |
294 |
_orig_build_variantes = RQLRewriter.build_variantes |
|
295 |
||
296 |
def _insert_snippets(self, snippets, varexistsmap=None): |
|
297 |
_orig_insert_snippets(self, sorted(snippets, snippet_cmp), varexistsmap) |
|
298 |
||
299 |
def _build_variantes(self, newsolutions): |
|
300 |
variantes = _orig_build_variantes(self, newsolutions) |
|
301 |
sortedvariantes = [] |
|
302 |
for variante in variantes: |
|
1132 | 303 |
orderedkeys = sorted((k[1], k[2], v) for k, v in variante.iteritems()) |
0 | 304 |
variante = DumbOrderedDict(sorted(variante.iteritems(), |
1132 | 305 |
lambda a, b: cmp((a[0][1],a[0][2],a[1]), |
306 |
(b[0][1],b[0][2],b[1])))) |
|
0 | 307 |
sortedvariantes.append( (orderedkeys, variante) ) |
308 |
return [v for ok, v in sorted(sortedvariantes)] |
|
309 |
||
310 |
from cubicweb.server.querier import ExecutionPlan |
|
311 |
_orig_check_permissions = ExecutionPlan._check_permissions |
|
312 |
_orig_init_temp_table = ExecutionPlan.init_temp_table |
|
313 |
||
314 |
def _check_permissions(*args, **kwargs): |
|
315 |
res, restricted = _orig_check_permissions(*args, **kwargs) |
|
1132 | 316 |
res = DumbOrderedDict(sorted(res.iteritems(), lambda a, b: cmp(a[1], b[1]))) |
0 | 317 |
return res, restricted |
318 |
||
319 |
def _dummy_check_permissions(self, rqlst): |
|
320 |
return {(): rqlst.solutions}, set() |
|
321 |
||
322 |
def _init_temp_table(self, table, selection, solution): |
|
323 |
if self.tablesinorder is None: |
|
324 |
tablesinorder = self.tablesinorder = {} |
|
325 |
else: |
|
326 |
tablesinorder = self.tablesinorder |
|
327 |
if not table in tablesinorder: |
|
328 |
tablesinorder[table] = 'table%s' % len(tablesinorder) |
|
329 |
return _orig_init_temp_table(self, table, selection, solution) |
|
330 |
||
331 |
from cubicweb.server import rqlannotation |
|
332 |
_orig_select_principal = rqlannotation._select_principal |
|
333 |
||
334 |
def _select_principal(scope, relations): |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
0
diff
changeset
|
335 |
return _orig_select_principal(scope, relations, |
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
0
diff
changeset
|
336 |
_sort=lambda rels: sorted(rels, key=lambda x: x.r_type)) |
0 | 337 |
|
338 |
try: |
|
339 |
from cubicweb.server.msplanner import PartPlanInformation |
|
340 |
except ImportError: |
|
341 |
class PartPlanInformation(object): |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
342 |
def merge_input_maps(self, *args): |
0 | 343 |
pass |
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
344 |
def _choose_term(self, sourceterms): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
345 |
pass |
0 | 346 |
_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
|
347 |
_orig_choose_term = PartPlanInformation._choose_term |
0 | 348 |
|
349 |
def _merge_input_maps(*args): |
|
350 |
return sorted(_orig_merge_input_maps(*args)) |
|
351 |
||
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
352 |
def _choose_term(self, sourceterms): |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
353 |
# predictable order for test purpose |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
354 |
def get_key(x): |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
355 |
try: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
356 |
# variable |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
357 |
return x.name |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
358 |
except AttributeError: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
359 |
try: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
360 |
# relation |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
361 |
return x.r_type |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
362 |
except AttributeError: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
363 |
# const |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
364 |
return x.value |
1236
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
365 |
return _orig_choose_term(self, DumbOrderedDict2(sourceterms, get_key)) |
0 | 366 |
|
367 |
||
368 |
def do_monkey_patch(): |
|
369 |
RQLRewriter.insert_snippets = _insert_snippets |
|
370 |
RQLRewriter.build_variantes = _build_variantes |
|
371 |
ExecutionPlan._check_permissions = _check_permissions |
|
372 |
ExecutionPlan.tablesinorder = None |
|
373 |
ExecutionPlan.init_temp_table = _init_temp_table |
|
374 |
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
|
375 |
PartPlanInformation._choose_term = _choose_term |
0 | 376 |
|
377 |
def undo_monkey_patch(): |
|
378 |
RQLRewriter.insert_snippets = _orig_insert_snippets |
|
379 |
RQLRewriter.build_variantes = _orig_build_variantes |
|
380 |
ExecutionPlan._check_permissions = _orig_check_permissions |
|
381 |
ExecutionPlan.init_temp_table = _orig_init_temp_table |
|
382 |
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
|
383 |
PartPlanInformation._choose_term = _orig_choose_term |