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