author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 09 Dec 2009 12:22:21 +0100 | |
changeset 4072 | ead446e70c28 |
parent 3675 | 5d143781a604 |
child 4252 | 6c4f109c2b03 |
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 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
14 |
from cubicweb.req import RequestSessionBase |
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
|
15 |
from cubicweb.cwvreg import CubicWebVRegistry |
0 | 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 |
||
39 |
class FakeRequest(CubicWebRequestBase): |
|
40 |
"""test implementation of an cubicweb request object""" |
|
41 |
||
42 |
def __init__(self, *args, **kwargs): |
|
43 |
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
|
44 |
kwargs['vreg'] = CubicWebVRegistry(FakeConfig(), initlog=False) |
0 | 45 |
kwargs['https'] = False |
46 |
self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop') |
|
47 |
super(FakeRequest, self).__init__(*args, **kwargs) |
|
48 |
self._session_data = {} |
|
49 |
self._headers = {} |
|
50 |
||
51 |
def header_accept_language(self): |
|
52 |
"""returns an ordered list of preferred languages""" |
|
53 |
return ('en',) |
|
54 |
||
55 |
def header_if_modified_since(self): |
|
56 |
return None |
|
57 |
||
58 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2471
diff
changeset
|
59 |
"""return the root url of the instance""" |
0 | 60 |
return BASE_URL |
61 |
||
62 |
def relative_path(self, includeparams=True): |
|
63 |
"""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
|
64 |
to the instance's root, but some other normalization may be needed |
0 | 65 |
so that the returned path may be used to compare to generated urls |
66 |
""" |
|
67 |
if self._url.startswith(BASE_URL): |
|
68 |
url = self._url[len(BASE_URL):] |
|
69 |
else: |
|
70 |
url = self._url |
|
71 |
if includeparams: |
|
72 |
return url |
|
73 |
return url.split('?', 1)[0] |
|
74 |
||
75 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
76 |
"""set output content type for this request. An optional filename |
|
77 |
may be given |
|
78 |
""" |
|
79 |
pass |
|
80 |
||
81 |
def set_header(self, header, value): |
|
82 |
"""set an output HTTP header""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
83 |
self._headers[header] = value |
1482 | 84 |
|
0 | 85 |
def add_header(self, header, value): |
86 |
"""set an output HTTP header""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
87 |
self._headers[header] = value # XXX |
1482 | 88 |
|
0 | 89 |
def remove_header(self, header): |
90 |
"""remove an output HTTP header""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
91 |
self._headers.pop(header, None) |
1482 | 92 |
|
0 | 93 |
def get_header(self, header, default=None): |
94 |
"""return the value associated with the given input header, |
|
95 |
raise KeyError if the header is not set |
|
96 |
""" |
|
97 |
return self._headers.get(header, default) |
|
98 |
||
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3652
diff
changeset
|
99 |
def set_cookie(self, cookie, key, maxage=300, expires=None): |
0 | 100 |
"""set / update a cookie key |
101 |
||
102 |
by default, cookie will be available for the next 5 minutes |
|
103 |
""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
104 |
morsel = cookie[key] |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
105 |
if maxage is not None: |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
106 |
morsel['Max-Age'] = maxage |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
107 |
if expires: |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
108 |
morsel['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S %z') |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
109 |
# make sure cookie is set on the correct path |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
110 |
morsel['path'] = self.base_url_path() |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
111 |
self.add_header('Set-Cookie', morsel.OutputString()) |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
112 |
self.add_header('Cookie', morsel.OutputString()) |
0 | 113 |
|
114 |
def remove_cookie(self, cookie, key): |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
115 |
self.remove_header('Set-Cookie') |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
116 |
self.remove_header('Cookie') |
0 | 117 |
|
118 |
def validate_cache(self): |
|
119 |
pass |
|
120 |
||
121 |
# session compatibility (in some test are using this class to test server |
|
122 |
# side views...) |
|
123 |
def actual_session(self): |
|
124 |
"""return the original parent session if any, else self""" |
|
125 |
return self |
|
126 |
||
127 |
def unsafe_execute(self, *args, **kwargs): |
|
128 |
"""return the original parent session if any, else self""" |
|
129 |
kwargs.pop('propagate', None) |
|
130 |
return self.execute(*args, **kwargs) |
|
131 |
||
132 |
||
133 |
class FakeUser(object): |
|
134 |
login = 'toto' |
|
135 |
eid = 0 |
|
136 |
def in_groups(self, groups): |
|
137 |
return True |
|
138 |
||
139 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
140 |
class FakeSession(RequestSessionBase): |
0 | 141 |
def __init__(self, repo=None, user=None): |
142 |
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
|
143 |
self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False)) |
0 | 144 |
self.pool = FakePool() |
145 |
self.user = user or FakeUser() |
|
146 |
self.is_internal_session = False |
|
147 |
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
|
148 |
self.transaction_data = {} |
1482 | 149 |
|
0 | 150 |
def execute(self, *args): |
151 |
pass |
|
2178
a73bf75a1ef9
fake unsafe_execute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
152 |
unsafe_execute = execute |
2703
27c04321fc81
[cleanup] delete-trailing-whitespace + removed debug print
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
153 |
|
0 | 154 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
155 |
self.transaction_data.clear() |
0 | 156 |
def close(self, *args): |
157 |
pass |
|
158 |
def system_sql(self, sql, args=None): |
|
159 |
pass |
|
160 |
||
161 |
def decorate_rset(self, rset, propagate=False): |
|
162 |
rset.vreg = self.vreg |
|
163 |
rset.req = self |
|
164 |
return rset |
|
165 |
||
166 |
def set_entity_cache(self, entity): |
|
167 |
pass |
|
1482 | 168 |
|
0 | 169 |
class FakeRepo(object): |
170 |
querier = None |
|
171 |
def __init__(self, schema, vreg=None, config=None): |
|
172 |
self.extids = {} |
|
173 |
self.eids = {} |
|
174 |
self._count = 0 |
|
175 |
self.schema = schema |
|
176 |
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
|
177 |
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
|
178 |
self.vreg.schema = schema |
0 | 179 |
|
180 |
def internal_session(self): |
|
181 |
return FakeSession(self) |
|
1481 | 182 |
|
183 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
184 |
recreate=False): |
|
0 | 185 |
try: |
186 |
return self.extids[extid] |
|
187 |
except KeyError: |
|
188 |
if not insert: |
|
189 |
return None |
|
190 |
self._count += 1 |
|
191 |
eid = self._count |
|
192 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
193 |
self.extids[extid] = eid |
|
194 |
self.eids[eid] = extid |
|
195 |
source.after_entity_insertion(session, extid, entity) |
|
196 |
return eid |
|
1482 | 197 |
|
0 | 198 |
def eid2extid(self, source, eid, session=None): |
199 |
return self.eids[eid] |
|
200 |
||
201 |
||
202 |
class FakeSource(object): |
|
203 |
dbhelper = get_adv_func_helper('sqlite') |
|
204 |
indexer = get_indexer('sqlite', 'UTF8') |
|
205 |
dbhelper.fti_uid_attr = indexer.uid_attr |
|
206 |
dbhelper.fti_table = indexer.table |
|
207 |
dbhelper.fti_restriction_sql = indexer.restriction_sql |
|
208 |
dbhelper.fti_need_distinct_query = indexer.need_distinct |
|
209 |
def __init__(self, uri): |
|
210 |
self.uri = uri |
|
211 |
||
1482 | 212 |
|
0 | 213 |
class FakePool(object): |
214 |
def source(self, uri): |
|
215 |
return FakeSource(uri) |