author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 08 Mar 2010 17:57:29 +0100 | |
changeset 4831 | c5aec27c1bf7 |
parent 4459 | f628abfb3a6c |
child 4835 | 13b0b96d7982 |
permissions | -rw-r--r-- |
0 | 1 |
"""Fake objects to ease testing of cubicweb without a fully working environment |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3255
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
||
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4459
diff
changeset
|
10 |
from logilab.db import get_db_helper |
0 | 11 |
|
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
|
12 |
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
|
13 |
from cubicweb.cwvreg import CubicWebVRegistry |
0 | 14 |
from cubicweb.web.request import CubicWebRequestBase |
15 |
from cubicweb.devtools import BASE_URL, BaseApptestConfiguration |
|
16 |
||
17 |
||
18 |
class FakeConfig(dict, BaseApptestConfiguration): |
|
19 |
translations = {} |
|
20 |
apphome = None |
|
21 |
def __init__(self, appid='data', apphome=None, cubes=()): |
|
22 |
self.appid = appid |
|
23 |
self.apphome = apphome |
|
24 |
self._cubes = cubes |
|
25 |
self['auth-mode'] = 'cookie' |
|
1482 | 26 |
self['uid'] = None |
0 | 27 |
self['base-url'] = BASE_URL |
28 |
self['rql-cache-size'] = 100 |
|
1482 | 29 |
|
0 | 30 |
def cubes(self, expand=False): |
31 |
return self._cubes |
|
1482 | 32 |
|
0 | 33 |
def sources(self): |
34 |
return {} |
|
35 |
||
36 |
||
37 |
class FakeRequest(CubicWebRequestBase): |
|
38 |
"""test implementation of an cubicweb request object""" |
|
39 |
||
40 |
def __init__(self, *args, **kwargs): |
|
41 |
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
|
42 |
kwargs['vreg'] = CubicWebVRegistry(FakeConfig(), initlog=False) |
0 | 43 |
kwargs['https'] = False |
44 |
self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop') |
|
45 |
super(FakeRequest, self).__init__(*args, **kwargs) |
|
46 |
self._session_data = {} |
|
47 |
self._headers = {} |
|
48 |
||
49 |
def header_accept_language(self): |
|
50 |
"""returns an ordered list of preferred languages""" |
|
51 |
return ('en',) |
|
52 |
||
53 |
def header_if_modified_since(self): |
|
54 |
return None |
|
55 |
||
56 |
def base_url(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2471
diff
changeset
|
57 |
"""return the root url of the instance""" |
0 | 58 |
return BASE_URL |
59 |
||
60 |
def relative_path(self, includeparams=True): |
|
61 |
"""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
|
62 |
to the instance's root, but some other normalization may be needed |
0 | 63 |
so that the returned path may be used to compare to generated urls |
64 |
""" |
|
65 |
if self._url.startswith(BASE_URL): |
|
66 |
url = self._url[len(BASE_URL):] |
|
67 |
else: |
|
68 |
url = self._url |
|
69 |
if includeparams: |
|
70 |
return url |
|
71 |
return url.split('?', 1)[0] |
|
72 |
||
73 |
def set_content_type(self, content_type, filename=None, encoding=None): |
|
74 |
"""set output content type for this request. An optional filename |
|
75 |
may be given |
|
76 |
""" |
|
77 |
pass |
|
78 |
||
4457
297a63704761
mimick actual request set_header method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
79 |
def set_header(self, header, value, raw=True): |
0 | 80 |
"""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
|
81 |
self._headers[header] = value |
1482 | 82 |
|
0 | 83 |
def add_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 # XXX |
1482 | 86 |
|
0 | 87 |
def remove_header(self, header): |
88 |
"""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
|
89 |
self._headers.pop(header, None) |
1482 | 90 |
|
0 | 91 |
def get_header(self, header, default=None): |
92 |
"""return the value associated with the given input header, |
|
93 |
raise KeyError if the header is not set |
|
94 |
""" |
|
95 |
return self._headers.get(header, default) |
|
96 |
||
3653
ef71abb1e77b
[req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3652
diff
changeset
|
97 |
def set_cookie(self, cookie, key, maxage=300, expires=None): |
0 | 98 |
"""set / update a cookie key |
99 |
||
100 |
by default, cookie will be available for the next 5 minutes |
|
101 |
""" |
|
3652
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
if expires: |
9fba30110377
[testlib] nicer http headers and cookie faking in fake request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3454
diff
changeset
|
106 |
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
|
107 |
# 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
|
108 |
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
|
109 |
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
|
110 |
self.add_header('Cookie', morsel.OutputString()) |
0 | 111 |
|
112 |
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
|
113 |
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
|
114 |
self.remove_header('Cookie') |
0 | 115 |
|
116 |
def validate_cache(self): |
|
117 |
pass |
|
118 |
||
119 |
# session compatibility (in some test are using this class to test server |
|
120 |
# side views...) |
|
121 |
def actual_session(self): |
|
122 |
"""return the original parent session if any, else self""" |
|
123 |
return self |
|
124 |
||
125 |
def unsafe_execute(self, *args, **kwargs): |
|
126 |
"""return the original parent session if any, else self""" |
|
127 |
kwargs.pop('propagate', None) |
|
128 |
return self.execute(*args, **kwargs) |
|
129 |
||
130 |
||
131 |
class FakeUser(object): |
|
132 |
login = 'toto' |
|
133 |
eid = 0 |
|
134 |
def in_groups(self, groups): |
|
135 |
return True |
|
136 |
||
137 |
||
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
|
138 |
class FakeSession(RequestSessionBase): |
0 | 139 |
def __init__(self, repo=None, user=None): |
140 |
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
|
141 |
self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False)) |
0 | 142 |
self.pool = FakePool() |
143 |
self.user = user or FakeUser() |
|
144 |
self.is_internal_session = False |
|
145 |
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
|
146 |
self.transaction_data = {} |
1482 | 147 |
|
0 | 148 |
def execute(self, *args): |
149 |
pass |
|
2178
a73bf75a1ef9
fake unsafe_execute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
150 |
unsafe_execute = execute |
2703
27c04321fc81
[cleanup] delete-trailing-whitespace + removed debug print
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
151 |
|
0 | 152 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
153 |
self.transaction_data.clear() |
0 | 154 |
def close(self, *args): |
155 |
pass |
|
156 |
def system_sql(self, sql, args=None): |
|
157 |
pass |
|
158 |
||
159 |
def decorate_rset(self, rset, propagate=False): |
|
160 |
rset.vreg = self.vreg |
|
161 |
rset.req = self |
|
162 |
return rset |
|
163 |
||
164 |
def set_entity_cache(self, entity): |
|
165 |
pass |
|
1482 | 166 |
|
0 | 167 |
class FakeRepo(object): |
168 |
querier = None |
|
169 |
def __init__(self, schema, vreg=None, config=None): |
|
170 |
self.extids = {} |
|
171 |
self.eids = {} |
|
172 |
self._count = 0 |
|
173 |
self.schema = schema |
|
174 |
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
|
175 |
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
|
176 |
self.vreg.schema = schema |
0 | 177 |
|
178 |
def internal_session(self): |
|
179 |
return FakeSession(self) |
|
1481 | 180 |
|
181 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
182 |
recreate=False): |
|
0 | 183 |
try: |
184 |
return self.extids[extid] |
|
185 |
except KeyError: |
|
186 |
if not insert: |
|
187 |
return None |
|
188 |
self._count += 1 |
|
189 |
eid = self._count |
|
190 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
191 |
self.extids[extid] = eid |
|
192 |
self.eids[eid] = extid |
|
193 |
source.after_entity_insertion(session, extid, entity) |
|
194 |
return eid |
|
1482 | 195 |
|
0 | 196 |
def eid2extid(self, source, eid, session=None): |
197 |
return self.eids[eid] |
|
198 |
||
199 |
||
200 |
class FakeSource(object): |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4459
diff
changeset
|
201 |
dbhelper = get_db_helper('sqlite') |
0 | 202 |
def __init__(self, uri): |
203 |
self.uri = uri |
|
204 |
||
1482 | 205 |
|
0 | 206 |
class FakePool(object): |
207 |
def source(self, uri): |
|
208 |
return FakeSource(uri) |