author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 24 Jul 2009 15:11:56 +0200 | |
changeset 2478 | 2c2d7f03aead |
parent 2476 | 1294a6bdf3bf |
child 2650 | 18aec79ec3a3 |
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 |
|
16 |
from cubicweb.web.request import CubicWebRequestBase |
|
17 |
from cubicweb.devtools import BASE_URL, BaseApptestConfiguration |
|
18 |
||
19 |
||
20 |
class FakeConfig(dict, BaseApptestConfiguration): |
|
21 |
translations = {} |
|
22 |
apphome = None |
|
23 |
def __init__(self, appid='data', apphome=None, cubes=()): |
|
24 |
self.appid = appid |
|
25 |
self.apphome = apphome |
|
26 |
self._cubes = cubes |
|
27 |
self['auth-mode'] = 'cookie' |
|
1482 | 28 |
self['uid'] = None |
0 | 29 |
self['base-url'] = BASE_URL |
30 |
self['rql-cache-size'] = 100 |
|
1482 | 31 |
|
0 | 32 |
def cubes(self, expand=False): |
33 |
return self._cubes |
|
1482 | 34 |
|
0 | 35 |
def sources(self): |
36 |
return {} |
|
37 |
||
38 |
class FakeVReg(object): |
|
39 |
def __init__(self, schema=None, config=None): |
|
40 |
self.schema = schema |
|
41 |
self.config = config or FakeConfig() |
|
42 |
self.properties = {'ui.encoding': 'UTF8', |
|
43 |
'ui.language': 'en', |
|
44 |
} |
|
1482 | 45 |
|
0 | 46 |
def property_value(self, key): |
47 |
return self.properties[key] |
|
48 |
||
49 |
_registries = { |
|
50 |
'controllers' : [Mock(id='view'), Mock(id='login'), |
|
51 |
Mock(id='logout'), Mock(id='edit')], |
|
2471
3e2b50ece726
begin to drop ref to secondary view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2181
diff
changeset
|
52 |
'views' : [Mock(id='primary'), Mock(id='oneline'), Mock(id='list')], |
0 | 53 |
} |
1482 | 54 |
|
0 | 55 |
def registry_objects(self, name, oid=None): |
56 |
return self._registries[name] |
|
1482 | 57 |
|
0 | 58 |
def etype_class(self, etype): |
59 |
class Entity(dict): |
|
60 |
e_schema = self.schema[etype] |
|
61 |
def __init__(self, session, eid, row=0, col=0): |
|
62 |
self.req = session |
|
63 |
self.eid = eid |
|
64 |
self.row, self.col = row, col |
|
65 |
def set_eid(self, eid): |
|
66 |
self.eid = self['eid'] = eid |
|
67 |
return Entity |
|
68 |
||
69 |
||
70 |
class FakeRequest(CubicWebRequestBase): |
|
71 |
"""test implementation of an cubicweb request object""" |
|
72 |
||
73 |
def __init__(self, *args, **kwargs): |
|
74 |
if not (args or 'vreg' in kwargs): |
|
75 |
kwargs['vreg'] = FakeVReg() |
|
76 |
kwargs['https'] = False |
|
77 |
self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop') |
|
78 |
super(FakeRequest, self).__init__(*args, **kwargs) |
|
79 |
self._session_data = {} |
|
80 |
self._headers = {} |
|
81 |
||
82 |
def header_accept_language(self): |
|
83 |
"""returns an ordered list of preferred languages""" |
|
84 |
return ('en',) |
|
85 |
||
86 |
def header_if_modified_since(self): |
|
87 |
return None |
|
88 |
||
89 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2471
diff
changeset
|
90 |
"""return the root url of the instance""" |
0 | 91 |
return BASE_URL |
92 |
||
93 |
def relative_path(self, includeparams=True): |
|
94 |
"""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
|
95 |
to the instance's root, but some other normalization may be needed |
0 | 96 |
so that the returned path may be used to compare to generated urls |
97 |
""" |
|
98 |
if self._url.startswith(BASE_URL): |
|
99 |
url = self._url[len(BASE_URL):] |
|
100 |
else: |
|
101 |
url = self._url |
|
102 |
if includeparams: |
|
103 |
return url |
|
104 |
return url.split('?', 1)[0] |
|
105 |
||
106 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
107 |
"""set output content type for this request. An optional filename |
|
108 |
may be given |
|
109 |
""" |
|
110 |
pass |
|
111 |
||
112 |
def set_header(self, header, value): |
|
113 |
"""set an output HTTP header""" |
|
114 |
pass |
|
1482 | 115 |
|
0 | 116 |
def add_header(self, header, value): |
117 |
"""set an output HTTP header""" |
|
118 |
pass |
|
1482 | 119 |
|
0 | 120 |
def remove_header(self, header): |
121 |
"""remove an output HTTP header""" |
|
122 |
pass |
|
1482 | 123 |
|
0 | 124 |
def get_header(self, header, default=None): |
125 |
"""return the value associated with the given input header, |
|
126 |
raise KeyError if the header is not set |
|
127 |
""" |
|
128 |
return self._headers.get(header, default) |
|
129 |
||
130 |
def set_cookie(self, cookie, key, maxage=300): |
|
131 |
"""set / update a cookie key |
|
132 |
||
133 |
by default, cookie will be available for the next 5 minutes |
|
134 |
""" |
|
135 |
pass |
|
136 |
||
137 |
def remove_cookie(self, cookie, key): |
|
138 |
"""remove a cookie by expiring it""" |
|
139 |
pass |
|
140 |
||
141 |
def validate_cache(self): |
|
142 |
pass |
|
143 |
||
144 |
# session compatibility (in some test are using this class to test server |
|
145 |
# side views...) |
|
146 |
def actual_session(self): |
|
147 |
"""return the original parent session if any, else self""" |
|
148 |
return self |
|
149 |
||
150 |
def unsafe_execute(self, *args, **kwargs): |
|
151 |
"""return the original parent session if any, else self""" |
|
152 |
kwargs.pop('propagate', None) |
|
153 |
return self.execute(*args, **kwargs) |
|
154 |
||
155 |
||
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
|
156 |
# 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
|
157 |
# 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
|
158 |
# """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
|
159 |
# 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
|
160 |
# 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
|
161 |
# 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
|
162 |
# 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
|
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 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
|
165 |
# """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
|
166 |
# 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
|
167 |
|
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 |
# 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
|
169 |
# 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
|
170 |
# 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
|
171 |
# 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
|
172 |
# 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
|
173 |
|
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
|
174 |
|
0 | 175 |
class FakeUser(object): |
176 |
login = 'toto' |
|
177 |
eid = 0 |
|
178 |
def in_groups(self, groups): |
|
179 |
return True |
|
180 |
||
181 |
||
182 |
class FakeSession(RequestSessionMixIn): |
|
183 |
def __init__(self, repo=None, user=None): |
|
184 |
self.repo = repo |
|
185 |
self.vreg = getattr(self.repo, 'vreg', FakeVReg()) |
|
186 |
self.pool = FakePool() |
|
187 |
self.user = user or FakeUser() |
|
188 |
self.is_internal_session = False |
|
189 |
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
|
190 |
self.transaction_data = {} |
1482 | 191 |
|
0 | 192 |
def execute(self, *args): |
193 |
pass |
|
2178
a73bf75a1ef9
fake unsafe_execute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
194 |
unsafe_execute = execute |
a73bf75a1ef9
fake unsafe_execute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
195 |
|
0 | 196 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
197 |
self.transaction_data.clear() |
0 | 198 |
def close(self, *args): |
199 |
pass |
|
200 |
def system_sql(self, sql, args=None): |
|
201 |
pass |
|
202 |
||
203 |
def decorate_rset(self, rset, propagate=False): |
|
204 |
rset.vreg = self.vreg |
|
205 |
rset.req = self |
|
206 |
return rset |
|
207 |
||
208 |
def set_entity_cache(self, entity): |
|
209 |
pass |
|
1482 | 210 |
|
0 | 211 |
class FakeRepo(object): |
212 |
querier = None |
|
213 |
def __init__(self, schema, vreg=None, config=None): |
|
214 |
self.extids = {} |
|
215 |
self.eids = {} |
|
216 |
self._count = 0 |
|
217 |
self.schema = schema |
|
218 |
self.vreg = vreg or FakeVReg() |
|
219 |
self.config = config or FakeConfig() |
|
220 |
||
221 |
def internal_session(self): |
|
222 |
return FakeSession(self) |
|
1481 | 223 |
|
224 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
225 |
recreate=False): |
|
0 | 226 |
try: |
227 |
return self.extids[extid] |
|
228 |
except KeyError: |
|
229 |
if not insert: |
|
230 |
return None |
|
231 |
self._count += 1 |
|
232 |
eid = self._count |
|
233 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
234 |
self.extids[extid] = eid |
|
235 |
self.eids[eid] = extid |
|
236 |
source.after_entity_insertion(session, extid, entity) |
|
237 |
return eid |
|
1482 | 238 |
|
0 | 239 |
def eid2extid(self, source, eid, session=None): |
240 |
return self.eids[eid] |
|
241 |
||
242 |
||
243 |
class FakeSource(object): |
|
244 |
dbhelper = get_adv_func_helper('sqlite') |
|
245 |
indexer = get_indexer('sqlite', 'UTF8') |
|
246 |
dbhelper.fti_uid_attr = indexer.uid_attr |
|
247 |
dbhelper.fti_table = indexer.table |
|
248 |
dbhelper.fti_restriction_sql = indexer.restriction_sql |
|
249 |
dbhelper.fti_need_distinct_query = indexer.need_distinct |
|
250 |
def __init__(self, uri): |
|
251 |
self.uri = uri |
|
252 |
||
1482 | 253 |
|
0 | 254 |
class FakePool(object): |
255 |
def source(self, uri): |
|
256 |
return FakeSource(uri) |
|
257 |
||
258 |
# commented until proven to be useful |
|
259 |
## from logging import getLogger |
|
260 |
## from cubicweb import set_log_methods |
|
261 |
## for cls in (FakeConfig, FakeVReg, FakeRequest, FakeSession, FakeRepo, |
|
262 |
## FakeSource, FakePool): |
|
263 |
## set_log_methods(cls, getLogger('fake')) |