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