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