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