author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Wed, 02 Jun 2010 16:12:18 +0000 | |
branch | stable |
changeset 5639 | 4acb860159e4 |
parent 5426 | 0d4853a6e5ee |
child 5768 | 1e73a466aa69 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""some utilities to ease repository testing |
19 |
||
20 |
This module contains functions to initialize a new repository. |
|
21 |
||
22 |
""" |
|
23 |
__docformat__ = "restructuredtext en" |
|
24 |
||
25 |
from pprint import pprint |
|
26 |
||
2074
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
27 |
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
|
28 |
|
0 | 29 |
def tuplify(list): |
30 |
for i in range(len(list)): |
|
31 |
if type(list[i]) is not type(()): |
|
32 |
list[i] = tuple(list[i]) |
|
33 |
return list |
|
34 |
||
35 |
def snippet_cmp(a, b): |
|
36 |
a = (a[0], [e.expression for e in a[1]]) |
|
37 |
b = (b[0], [e.expression for e in b[1]]) |
|
38 |
return cmp(a, b) |
|
39 |
||
40 |
def test_plan(self, rql, expected, kwargs=None): |
|
41 |
plan = self._prepare_plan(rql, kwargs) |
|
42 |
self.planner.build_plan(plan) |
|
43 |
try: |
|
44 |
self.assertEquals(len(plan.steps), len(expected), |
|
45 |
'expected %s steps, got %s' % (len(expected), len(plan.steps))) |
|
46 |
# step order is important |
|
47 |
for i, step in enumerate(plan.steps): |
|
48 |
compare_steps(self, step.test_repr(), expected[i]) |
|
49 |
except AssertionError: |
|
50 |
pprint([step.test_repr() for step in plan.steps]) |
|
51 |
raise |
|
52 |
||
53 |
def compare_steps(self, step, expected): |
|
54 |
try: |
|
55 |
self.assertEquals(step[0], expected[0], 'expected step type %s, got %s' % (expected[0], step[0])) |
|
56 |
if len(step) > 2 and isinstance(step[1], list) and isinstance(expected[1], list): |
|
57 |
queries, equeries = step[1], expected[1] |
|
58 |
self.assertEquals(len(queries), len(equeries), |
|
59 |
'expected %s queries, got %s' % (len(equeries), len(queries))) |
|
60 |
for i, (rql, sol) in enumerate(queries): |
|
61 |
self.assertEquals(rql, equeries[i][0]) |
|
1789 | 62 |
self.assertEquals(sorted(sol), sorted(equeries[i][1])) |
0 | 63 |
idx = 2 |
64 |
else: |
|
65 |
idx = 1 |
|
66 |
self.assertEquals(step[idx:-1], expected[idx:-1], |
|
67 |
'expected step characteristic \n%s\n, got\n%s' % (expected[1:-1], step[1:-1])) |
|
68 |
self.assertEquals(len(step[-1]), len(expected[-1]), |
|
69 |
'got %s child steps, expected %s' % (len(step[-1]), len(expected[-1]))) |
|
70 |
except AssertionError: |
|
71 |
print 'error on step ', |
|
72 |
pprint(step[:-1]) |
|
73 |
raise |
|
74 |
children = step[-1] |
|
75 |
if step[0] in ('UnionFetchStep', 'UnionStep'): |
|
76 |
# sort children |
|
77 |
children = sorted(children) |
|
78 |
expectedchildren = sorted(expected[-1]) |
|
79 |
else: |
|
80 |
expectedchildren = expected[-1] |
|
81 |
for i, substep in enumerate(children): |
|
82 |
compare_steps(self, substep, expectedchildren[i]) |
|
83 |
||
84 |
||
85 |
class DumbOrderedDict(list): |
|
86 |
def __iter__(self): |
|
87 |
return self.iterkeys() |
|
88 |
def __contains__(self, key): |
|
89 |
return key in self.iterkeys() |
|
90 |
def __getitem__(self, key): |
|
1236
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
91 |
for key_, value in list.__iter__(self): |
0 | 92 |
if key == key_: |
93 |
return value |
|
94 |
raise KeyError(key) |
|
95 |
def iterkeys(self): |
|
96 |
return (x for x, y in list.__iter__(self)) |
|
97 |
def iteritems(self): |
|
98 |
return (x for x in list.__iter__(self)) |
|
1236
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
99 |
def items(self): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
100 |
return [x for x in list.__iter__(self)] |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
101 |
|
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
102 |
class DumbOrderedDict2(object): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
103 |
def __init__(self, origdict, sortkey): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
104 |
self.origdict = origdict |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
105 |
self.sortkey = sortkey |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
106 |
def __getattr__(self, attr): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
107 |
return getattr(self.origdict, attr) |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
108 |
def __iter__(self): |
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
109 |
return iter(sorted(self.origdict, key=self.sortkey)) |
0 | 110 |
|
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
|
111 |
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
|
112 |
"""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
|
113 |
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
|
114 |
""" |
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_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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
|
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
|
124 |
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
|
125 |
"""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
|
126 |
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
|
127 |
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
|
128 |
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
|
129 |
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
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
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
|
135 |
|
0 | 136 |
|
137 |
from logilab.common.testlib import TestCase |
|
138 |
from rql import RQLHelper |
|
139 |
from cubicweb.devtools.fake import FakeRepo, FakeSession |
|
140 |
from cubicweb.server import set_debug |
|
141 |
from cubicweb.server.querier import QuerierHelper |
|
142 |
from cubicweb.server.session import Session |
|
143 |
from cubicweb.server.sources.rql2sql import remove_unused_solutions |
|
144 |
||
145 |
class RQLGeneratorTC(TestCase): |
|
146 |
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
|
147 |
|
0 | 148 |
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
|
149 |
self.repo = FakeRepo(self.schema) |
0 | 150 |
self.rqlhelper = RQLHelper(self.schema, special_relations={'eid': 'uid', |
151 |
'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
|
152 |
self.qhelper = QuerierHelper(self.repo, self.schema) |
0 | 153 |
ExecutionPlan._check_permissions = _dummy_check_permissions |
154 |
rqlannotation._select_principal = _select_principal |
|
155 |
||
156 |
def tearDown(self): |
|
157 |
ExecutionPlan._check_permissions = _orig_check_permissions |
|
158 |
rqlannotation._select_principal = _orig_select_principal |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2074
diff
changeset
|
159 |
|
2069
7c0a1b90b1c6
set_debug method on rql2sql test as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
160 |
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
|
161 |
set_debug(debug) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
162 |
|
0 | 163 |
def _prepare(self, rql): |
164 |
#print '******************** prepare', rql |
|
165 |
union = self.rqlhelper.parse(rql) |
|
166 |
#print '********* parsed', union.as_string() |
|
167 |
self.rqlhelper.compute_solutions(union) |
|
168 |
#print '********* solutions', solutions |
|
169 |
self.rqlhelper.simplify(union) |
|
170 |
#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
|
171 |
plan = self.qhelper.plan_factory(union, {}, FakeSession(self.repo)) |
0 | 172 |
plan.preprocess(union) |
173 |
for select in union.children: |
|
174 |
select.solutions.sort() |
|
175 |
#print '********* ppsolutions', solutions |
|
176 |
return union |
|
177 |
||
178 |
||
179 |
class BaseQuerierTC(TestCase): |
|
180 |
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
|
181 |
|
0 | 182 |
def setUp(self): |
183 |
self.o = self.repo.querier |
|
184 |
self.session = self.repo._sessions.values()[0] |
|
185 |
self.ueid = self.session.user.eid |
|
186 |
assert self.ueid != -1 |
|
187 |
self.repo._type_source_cache = {} # clear cache |
|
188 |
self.pool = self.session.set_pool() |
|
189 |
self.maxeid = self.get_max_eid() |
|
190 |
do_monkey_patch() |
|
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
191 |
self._dumb_sessions = [] |
0 | 192 |
|
193 |
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
|
194 |
return self.session.execute('Any MAX(X)')[0][0] |
0 | 195 |
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
|
196 |
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
|
197 |
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
|
198 |
|
0 | 199 |
def tearDown(self): |
200 |
undo_monkey_patch() |
|
201 |
self.session.rollback() |
|
202 |
self.cleanup() |
|
203 |
self.commit() |
|
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
204 |
# properly close dumb sessions |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
205 |
for session in self._dumb_sessions: |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
206 |
session.rollback() |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
207 |
session.close() |
0 | 208 |
self.repo._free_pool(self.pool) |
209 |
assert self.session.user.eid != -1 |
|
210 |
||
211 |
def set_debug(self, debug): |
|
212 |
set_debug(debug) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
213 |
|
0 | 214 |
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
|
215 |
rqlhelper = self.repo.vreg.rqlhelper |
0 | 216 |
# reset uid_func so it don't try to get type from eids |
217 |
rqlhelper._analyser.uid_func = None |
|
218 |
rqlhelper._analyser.uid_func_mapping = {} |
|
219 |
return rqlhelper |
|
220 |
||
4184
7002e91d304a
should not simplify previous to preprocess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
221 |
def _prepare_plan(self, rql, kwargs=None, simplify=True): |
0 | 222 |
rqlhelper = self._rqlhelper() |
223 |
rqlst = rqlhelper.parse(rql) |
|
224 |
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
|
225 |
if simplify: |
7002e91d304a
should not simplify previous to preprocess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
226 |
rqlhelper.simplify(rqlst) |
0 | 227 |
for select in rqlst.children: |
228 |
select.solutions.sort() |
|
229 |
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
|
230 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
231 |
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
|
232 |
plan = self._prepare_plan(rql, kwargs, simplify=False) |
0 | 233 |
plan.preprocess(plan.rqlst) |
234 |
rqlst = plan.rqlst.children[0] |
|
235 |
rqlst.solutions = remove_unused_solutions(rqlst, rqlst.solutions, {}, self.repo.schema)[0] |
|
236 |
return rqlst |
|
237 |
||
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
|
238 |
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
|
239 |
"""lightweight session using the current user with hi-jacked groups""" |
0 | 240 |
# 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
|
241 |
u = self.repo._build_user(self.session, self.session.user.eid) |
0 | 242 |
u._groups = set(groups) |
243 |
s = Session(u, self.repo) |
|
244 |
s._threaddata.pool = self.pool |
|
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
245 |
# register session to ensure it gets closed |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
246 |
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
|
247 |
return s |
0 | 248 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
249 |
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
|
250 |
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
|
251 |
|
0 | 252 |
def commit(self): |
253 |
self.session.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
254 |
self.session.set_pool() |
0 | 255 |
|
256 |
||
257 |
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
|
258 |
newsources = 0 |
2074
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
259 |
def setup(self): |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
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
|
264 |
# XXX source_defs |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
265 |
self.o = self.repo.querier |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
266 |
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
|
267 |
self.pool = self.session.set_pool() |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
268 |
self.schema = self.o.schema |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
269 |
self.sources = self.o._repo.sources |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
270 |
self.system = self.sources[-1] |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
271 |
do_monkey_patch() |
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
272 |
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
|
273 |
|
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
274 |
def add_source(self, sourcecls, uri): |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
275 |
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
|
276 |
{'uri': uri})) |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
277 |
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
|
278 |
setattr(self, uri, self.sources[-1]) |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
279 |
self.newsources += 1 |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
280 |
|
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
281 |
def tearDown(self): |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
282 |
while self.newsources: |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
283 |
source = self.sources.pop(-1) |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
284 |
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
|
285 |
self.newsources -= 1 |
9e268cb6202e
enhance BasePlannerTC to ease subclasses definition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2069
diff
changeset
|
286 |
undo_monkey_patch() |
4773
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
287 |
for session in self._dumb_sessions: |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
288 |
session._threaddata.pool = None |
6ab9ca63531f
[testlib] properly close dumb sessions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4766
diff
changeset
|
289 |
session.close() |
0 | 290 |
|
291 |
def _prepare_plan(self, rql, kwargs=None): |
|
292 |
rqlst = self.o.parse(rql, annotate=True) |
|
293 |
self.o.solutions(self.session, rqlst, kwargs) |
|
294 |
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
|
295 |
self.repo.vreg.rqlhelper.annotate(rqlst) |
0 | 296 |
for select in rqlst.children: |
297 |
select.solutions.sort() |
|
298 |
else: |
|
299 |
rqlst.solutions.sort() |
|
300 |
return self.o.plan_factory(rqlst, kwargs, self.session) |
|
301 |
||
302 |
||
303 |
# monkey patch some methods to get predicatable results ####################### |
|
304 |
||
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
|
305 |
from cubicweb.rqlrewrite import RQLRewriter |
0 | 306 |
_orig_insert_snippets = RQLRewriter.insert_snippets |
307 |
_orig_build_variantes = RQLRewriter.build_variantes |
|
308 |
||
309 |
def _insert_snippets(self, snippets, varexistsmap=None): |
|
310 |
_orig_insert_snippets(self, sorted(snippets, snippet_cmp), varexistsmap) |
|
311 |
||
312 |
def _build_variantes(self, newsolutions): |
|
313 |
variantes = _orig_build_variantes(self, newsolutions) |
|
314 |
sortedvariantes = [] |
|
315 |
for variante in variantes: |
|
1132 | 316 |
orderedkeys = sorted((k[1], k[2], v) for k, v in variante.iteritems()) |
0 | 317 |
variante = DumbOrderedDict(sorted(variante.iteritems(), |
1132 | 318 |
lambda a, b: cmp((a[0][1],a[0][2],a[1]), |
319 |
(b[0][1],b[0][2],b[1])))) |
|
0 | 320 |
sortedvariantes.append( (orderedkeys, variante) ) |
321 |
return [v for ok, v in sorted(sortedvariantes)] |
|
322 |
||
323 |
from cubicweb.server.querier import ExecutionPlan |
|
324 |
_orig_check_permissions = ExecutionPlan._check_permissions |
|
325 |
_orig_init_temp_table = ExecutionPlan.init_temp_table |
|
326 |
||
327 |
def _check_permissions(*args, **kwargs): |
|
328 |
res, restricted = _orig_check_permissions(*args, **kwargs) |
|
1132 | 329 |
res = DumbOrderedDict(sorted(res.iteritems(), lambda a, b: cmp(a[1], b[1]))) |
0 | 330 |
return res, restricted |
331 |
||
332 |
def _dummy_check_permissions(self, rqlst): |
|
333 |
return {(): rqlst.solutions}, set() |
|
334 |
||
335 |
def _init_temp_table(self, table, selection, solution): |
|
336 |
if self.tablesinorder is None: |
|
337 |
tablesinorder = self.tablesinorder = {} |
|
338 |
else: |
|
339 |
tablesinorder = self.tablesinorder |
|
340 |
if not table in tablesinorder: |
|
341 |
tablesinorder[table] = 'table%s' % len(tablesinorder) |
|
342 |
return _orig_init_temp_table(self, table, selection, solution) |
|
343 |
||
344 |
from cubicweb.server import rqlannotation |
|
345 |
_orig_select_principal = rqlannotation._select_principal |
|
346 |
||
347 |
def _select_principal(scope, relations): |
|
599
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
0
diff
changeset
|
348 |
return _orig_select_principal(scope, relations, |
9ef680acd92a
fix select principal so results are predictable during tests
Sylvain <syt@logilab.fr>
parents:
0
diff
changeset
|
349 |
_sort=lambda rels: sorted(rels, key=lambda x: x.r_type)) |
0 | 350 |
|
351 |
try: |
|
352 |
from cubicweb.server.msplanner import PartPlanInformation |
|
353 |
except ImportError: |
|
354 |
class PartPlanInformation(object): |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
355 |
def merge_input_maps(self, *args): |
0 | 356 |
pass |
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
357 |
def _choose_term(self, sourceterms): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1789
diff
changeset
|
358 |
pass |
0 | 359 |
_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
|
360 |
_orig_choose_term = PartPlanInformation._choose_term |
0 | 361 |
|
362 |
def _merge_input_maps(*args): |
|
363 |
return sorted(_orig_merge_input_maps(*args)) |
|
364 |
||
1235
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
365 |
def _choose_term(self, sourceterms): |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
366 |
# predictable order for test purpose |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
367 |
def get_key(x): |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
368 |
try: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
369 |
# variable |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
370 |
return x.name |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
371 |
except AttributeError: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
372 |
try: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
373 |
# relation |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
374 |
return x.r_type |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
375 |
except AttributeError: |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
376 |
# const |
9c081452efc2
actually some monkey patching is still needed for predictability
sylvain.thenault@logilab.fr
parents:
1232
diff
changeset
|
377 |
return x.value |
1236
aeb46e43138d
fix choose_term monkey-patching
sylvain.thenault@logilab.fr
parents:
1235
diff
changeset
|
378 |
return _orig_choose_term(self, DumbOrderedDict2(sourceterms, get_key)) |
0 | 379 |
|
380 |
||
381 |
def do_monkey_patch(): |
|
382 |
RQLRewriter.insert_snippets = _insert_snippets |
|
383 |
RQLRewriter.build_variantes = _build_variantes |
|
384 |
ExecutionPlan._check_permissions = _check_permissions |
|
385 |
ExecutionPlan.tablesinorder = None |
|
386 |
ExecutionPlan.init_temp_table = _init_temp_table |
|
387 |
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
|
388 |
PartPlanInformation._choose_term = _choose_term |
0 | 389 |
|
390 |
def undo_monkey_patch(): |
|
391 |
RQLRewriter.insert_snippets = _orig_insert_snippets |
|
392 |
RQLRewriter.build_variantes = _orig_build_variantes |
|
393 |
ExecutionPlan._check_permissions = _orig_check_permissions |
|
394 |
ExecutionPlan.init_temp_table = _orig_init_temp_table |
|
395 |
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
|
396 |
PartPlanInformation._choose_term = _orig_choose_term |