author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 16 Sep 2009 16:36:55 +0200 | |
branch | 3.5 |
changeset 3250 | 65618fd34548 |
parent 3240 | 8604a15995d1 |
child 3255 | a1679842bd52 |
permissions | -rw-r--r-- |
0 | 1 |
"""Fake objects to ease testing of cubicweb without a fully working environment |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1872
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
: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:
1872
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from logilab.common.testlib import mock_object as Mock |
|
11 |
from logilab.common.adbh import get_adv_func_helper |
|
12 |
||
13 |
from indexer import get_indexer |
|
14 |
||
15 |
from cubicweb import RequestSessionMixIn |
|
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:
2703
diff
changeset
|
16 |
from cubicweb.cwvreg import CubicWebVRegistry |
0 | 17 |
from cubicweb.web.request import CubicWebRequestBase |
18 |
from cubicweb.devtools import BASE_URL, BaseApptestConfiguration |
|
19 |
||
20 |
||
21 |
class FakeConfig(dict, BaseApptestConfiguration): |
|
22 |
translations = {} |
|
23 |
apphome = None |
|
24 |
def __init__(self, appid='data', apphome=None, cubes=()): |
|
25 |
self.appid = appid |
|
26 |
self.apphome = apphome |
|
27 |
self._cubes = cubes |
|
28 |
self['auth-mode'] = 'cookie' |
|
1482 | 29 |
self['uid'] = None |
0 | 30 |
self['base-url'] = BASE_URL |
31 |
self['rql-cache-size'] = 100 |
|
1482 | 32 |
|
0 | 33 |
def cubes(self, expand=False): |
34 |
return self._cubes |
|
1482 | 35 |
|
0 | 36 |
def sources(self): |
37 |
return {} |
|
38 |
||
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
39 |
class FakeVReg(dict): |
0 | 40 |
def __init__(self, schema=None, config=None): |
41 |
self.schema = schema |
|
42 |
self.config = config or FakeConfig() |
|
43 |
self.properties = {'ui.encoding': 'UTF8', |
|
44 |
'ui.language': 'en', |
|
45 |
} |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
46 |
self.update({ |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
47 |
'controllers' : {'login': []}, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
48 |
'views' : {}, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
49 |
}) |
1482 | 50 |
|
0 | 51 |
def property_value(self, key): |
52 |
return self.properties[key] |
|
53 |
||
54 |
def etype_class(self, etype): |
|
55 |
class Entity(dict): |
|
56 |
e_schema = self.schema[etype] |
|
57 |
def __init__(self, session, eid, row=0, col=0): |
|
58 |
self.req = session |
|
59 |
self.eid = eid |
|
60 |
self.row, self.col = row, col |
|
61 |
def set_eid(self, eid): |
|
62 |
self.eid = self['eid'] = eid |
|
63 |
return Entity |
|
64 |
||
65 |
||
66 |
class FakeRequest(CubicWebRequestBase): |
|
67 |
"""test implementation of an cubicweb request object""" |
|
68 |
||
69 |
def __init__(self, *args, **kwargs): |
|
70 |
if not (args or 'vreg' in kwargs): |
|
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:
2703
diff
changeset
|
71 |
kwargs['vreg'] = CubicWebVRegistry(FakeConfig(), initlog=False) |
0 | 72 |
kwargs['https'] = False |
73 |
self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop') |
|
74 |
super(FakeRequest, self).__init__(*args, **kwargs) |
|
75 |
self._session_data = {} |
|
76 |
self._headers = {} |
|
77 |
||
78 |
def header_accept_language(self): |
|
79 |
"""returns an ordered list of preferred languages""" |
|
80 |
return ('en',) |
|
81 |
||
82 |
def header_if_modified_since(self): |
|
83 |
return None |
|
84 |
||
85 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2471
diff
changeset
|
86 |
"""return the root url of the instance""" |
0 | 87 |
return BASE_URL |
88 |
||
89 |
def relative_path(self, includeparams=True): |
|
90 |
"""return the normalized path of the request (ie at least relative |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2471
diff
changeset
|
91 |
to the instance's root, but some other normalization may be needed |
0 | 92 |
so that the returned path may be used to compare to generated urls |
93 |
""" |
|
94 |
if self._url.startswith(BASE_URL): |
|
95 |
url = self._url[len(BASE_URL):] |
|
96 |
else: |
|
97 |
url = self._url |
|
98 |
if includeparams: |
|
99 |
return url |
|
100 |
return url.split('?', 1)[0] |
|
101 |
||
102 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
103 |
"""set output content type for this request. An optional filename |
|
104 |
may be given |
|
105 |
""" |
|
106 |
pass |
|
107 |
||
108 |
def set_header(self, header, value): |
|
109 |
"""set an output HTTP header""" |
|
110 |
pass |
|
1482 | 111 |
|
0 | 112 |
def add_header(self, header, value): |
113 |
"""set an output HTTP header""" |
|
114 |
pass |
|
1482 | 115 |
|
0 | 116 |
def remove_header(self, header): |
117 |
"""remove an output HTTP header""" |
|
118 |
pass |
|
1482 | 119 |
|
0 | 120 |
def get_header(self, header, default=None): |
121 |
"""return the value associated with the given input header, |
|
122 |
raise KeyError if the header is not set |
|
123 |
""" |
|
124 |
return self._headers.get(header, default) |
|
125 |
||
126 |
def set_cookie(self, cookie, key, maxage=300): |
|
127 |
"""set / update a cookie key |
|
128 |
||
129 |
by default, cookie will be available for the next 5 minutes |
|
130 |
""" |
|
131 |
pass |
|
132 |
||
133 |
def remove_cookie(self, cookie, key): |
|
134 |
"""remove a cookie by expiring it""" |
|
135 |
pass |
|
136 |
||
137 |
def validate_cache(self): |
|
138 |
pass |
|
139 |
||
140 |
# session compatibility (in some test are using this class to test server |
|
141 |
# side views...) |
|
142 |
def actual_session(self): |
|
143 |
"""return the original parent session if any, else self""" |
|
144 |
return self |
|
145 |
||
146 |
def unsafe_execute(self, *args, **kwargs): |
|
147 |
"""return the original parent session if any, else self""" |
|
148 |
kwargs.pop('propagate', None) |
|
149 |
return self.execute(*args, **kwargs) |
|
150 |
||
151 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
152 |
# class FakeRequestNoCnx(FakeRequest): |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
153 |
# def get_session_data(self, key, default=None, pop=False): |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
154 |
# """return value associated to `key` in session data""" |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
155 |
# if pop: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
156 |
# return self._session_data.pop(key, default) |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
157 |
# else: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
158 |
# return self._session_data.get(key, default) |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
159 |
|
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
160 |
# def set_session_data(self, key, value): |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
161 |
# """set value associated to `key` in session data""" |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
162 |
# self._session_data[key] = value |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
163 |
|
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
164 |
# def del_session_data(self, key): |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
165 |
# try: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
166 |
# del self._session_data[key] |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
167 |
# except KeyError: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
168 |
# pass |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
169 |
|
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
170 |
|
0 | 171 |
class FakeUser(object): |
172 |
login = 'toto' |
|
173 |
eid = 0 |
|
174 |
def in_groups(self, groups): |
|
175 |
return True |
|
176 |
||
177 |
||
178 |
class FakeSession(RequestSessionMixIn): |
|
179 |
def __init__(self, repo=None, user=None): |
|
180 |
self.repo = repo |
|
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:
2703
diff
changeset
|
181 |
self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False)) |
0 | 182 |
self.pool = FakePool() |
183 |
self.user = user or FakeUser() |
|
184 |
self.is_internal_session = False |
|
185 |
self.is_super_session = self.user.eid == -1 |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
186 |
self.transaction_data = {} |
1482 | 187 |
|
0 | 188 |
def execute(self, *args): |
189 |
pass |
|
2178
a73bf75a1ef9
fake unsafe_execute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
190 |
unsafe_execute = execute |
2703
27c04321fc81
[cleanup] delete-trailing-whitespace + removed debug print
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
191 |
|
0 | 192 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
193 |
self.transaction_data.clear() |
0 | 194 |
def close(self, *args): |
195 |
pass |
|
196 |
def system_sql(self, sql, args=None): |
|
197 |
pass |
|
198 |
||
199 |
def decorate_rset(self, rset, propagate=False): |
|
200 |
rset.vreg = self.vreg |
|
201 |
rset.req = self |
|
202 |
return rset |
|
203 |
||
204 |
def set_entity_cache(self, entity): |
|
205 |
pass |
|
1482 | 206 |
|
0 | 207 |
class FakeRepo(object): |
208 |
querier = None |
|
209 |
def __init__(self, schema, vreg=None, config=None): |
|
210 |
self.extids = {} |
|
211 |
self.eids = {} |
|
212 |
self._count = 0 |
|
213 |
self.schema = schema |
|
214 |
self.config = config or FakeConfig() |
|
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:
2703
diff
changeset
|
215 |
self.vreg = vreg or CubicWebVRegistry(self.config, initlog=False) |
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:
2703
diff
changeset
|
216 |
self.vreg.schema = schema |
0 | 217 |
|
218 |
def internal_session(self): |
|
219 |
return FakeSession(self) |
|
1481 | 220 |
|
221 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
222 |
recreate=False): |
|
0 | 223 |
try: |
224 |
return self.extids[extid] |
|
225 |
except KeyError: |
|
226 |
if not insert: |
|
227 |
return None |
|
228 |
self._count += 1 |
|
229 |
eid = self._count |
|
230 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
231 |
self.extids[extid] = eid |
|
232 |
self.eids[eid] = extid |
|
233 |
source.after_entity_insertion(session, extid, entity) |
|
234 |
return eid |
|
1482 | 235 |
|
0 | 236 |
def eid2extid(self, source, eid, session=None): |
237 |
return self.eids[eid] |
|
238 |
||
239 |
||
240 |
class FakeSource(object): |
|
241 |
dbhelper = get_adv_func_helper('sqlite') |
|
242 |
indexer = get_indexer('sqlite', 'UTF8') |
|
243 |
dbhelper.fti_uid_attr = indexer.uid_attr |
|
244 |
dbhelper.fti_table = indexer.table |
|
245 |
dbhelper.fti_restriction_sql = indexer.restriction_sql |
|
246 |
dbhelper.fti_need_distinct_query = indexer.need_distinct |
|
247 |
def __init__(self, uri): |
|
248 |
self.uri = uri |
|
249 |
||
1482 | 250 |
|
0 | 251 |
class FakePool(object): |
252 |
def source(self, uri): |
|
253 |
return FakeSource(uri) |
|
254 |
||
255 |
# commented until proven to be useful |
|
256 |
## from logging import getLogger |
|
257 |
## from cubicweb import set_log_methods |
|
258 |
## for cls in (FakeConfig, FakeVReg, FakeRequest, FakeSession, FakeRepo, |
|
259 |
## FakeSource, FakePool): |
|
260 |
## set_log_methods(cls, getLogger('fake')) |