author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 25 Sep 2009 11:53:06 +0200 | |
changeset 3478 | f043f71017a3 |
parent 3454 | ad1cddc06997 |
child 3652 | 9fba30110377 |
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""" |
|
85 |
pass |
|
1482 | 86 |
|
0 | 87 |
def add_header(self, header, value): |
88 |
"""set an output HTTP header""" |
|
89 |
pass |
|
1482 | 90 |
|
0 | 91 |
def remove_header(self, header): |
92 |
"""remove an output HTTP header""" |
|
93 |
pass |
|
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 |
""" |
|
106 |
pass |
|
107 |
||
108 |
def remove_cookie(self, cookie, key): |
|
109 |
"""remove a cookie by expiring it""" |
|
110 |
pass |
|
111 |
||
112 |
def validate_cache(self): |
|
113 |
pass |
|
114 |
||
115 |
# session compatibility (in some test are using this class to test server |
|
116 |
# side views...) |
|
117 |
def actual_session(self): |
|
118 |
"""return the original parent session if any, else self""" |
|
119 |
return self |
|
120 |
||
121 |
def unsafe_execute(self, *args, **kwargs): |
|
122 |
"""return the original parent session if any, else self""" |
|
123 |
kwargs.pop('propagate', None) |
|
124 |
return self.execute(*args, **kwargs) |
|
125 |
||
126 |
||
127 |
class FakeUser(object): |
|
128 |
login = 'toto' |
|
129 |
eid = 0 |
|
130 |
def in_groups(self, groups): |
|
131 |
return True |
|
132 |
||
133 |
||
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
|
134 |
class FakeSession(RequestSessionBase): |
0 | 135 |
def __init__(self, repo=None, user=None): |
136 |
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
|
137 |
self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False)) |
0 | 138 |
self.pool = FakePool() |
139 |
self.user = user or FakeUser() |
|
140 |
self.is_internal_session = False |
|
141 |
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
|
142 |
self.transaction_data = {} |
1482 | 143 |
|
0 | 144 |
def execute(self, *args): |
145 |
pass |
|
2178
a73bf75a1ef9
fake unsafe_execute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
146 |
unsafe_execute = execute |
2703
27c04321fc81
[cleanup] delete-trailing-whitespace + removed debug print
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
147 |
|
0 | 148 |
def commit(self, *args): |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
149 |
self.transaction_data.clear() |
0 | 150 |
def close(self, *args): |
151 |
pass |
|
152 |
def system_sql(self, sql, args=None): |
|
153 |
pass |
|
154 |
||
155 |
def decorate_rset(self, rset, propagate=False): |
|
156 |
rset.vreg = self.vreg |
|
157 |
rset.req = self |
|
158 |
return rset |
|
159 |
||
160 |
def set_entity_cache(self, entity): |
|
161 |
pass |
|
1482 | 162 |
|
0 | 163 |
class FakeRepo(object): |
164 |
querier = None |
|
165 |
def __init__(self, schema, vreg=None, config=None): |
|
166 |
self.extids = {} |
|
167 |
self.eids = {} |
|
168 |
self._count = 0 |
|
169 |
self.schema = schema |
|
170 |
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
|
171 |
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
|
172 |
self.vreg.schema = schema |
0 | 173 |
|
174 |
def internal_session(self): |
|
175 |
return FakeSession(self) |
|
1481 | 176 |
|
177 |
def extid2eid(self, source, extid, etype, session, insert=True, |
|
178 |
recreate=False): |
|
0 | 179 |
try: |
180 |
return self.extids[extid] |
|
181 |
except KeyError: |
|
182 |
if not insert: |
|
183 |
return None |
|
184 |
self._count += 1 |
|
185 |
eid = self._count |
|
186 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
187 |
self.extids[extid] = eid |
|
188 |
self.eids[eid] = extid |
|
189 |
source.after_entity_insertion(session, extid, entity) |
|
190 |
return eid |
|
1482 | 191 |
|
0 | 192 |
def eid2extid(self, source, eid, session=None): |
193 |
return self.eids[eid] |
|
194 |
||
195 |
||
196 |
class FakeSource(object): |
|
197 |
dbhelper = get_adv_func_helper('sqlite') |
|
198 |
indexer = get_indexer('sqlite', 'UTF8') |
|
199 |
dbhelper.fti_uid_attr = indexer.uid_attr |
|
200 |
dbhelper.fti_table = indexer.table |
|
201 |
dbhelper.fti_restriction_sql = indexer.restriction_sql |
|
202 |
dbhelper.fti_need_distinct_query = indexer.need_distinct |
|
203 |
def __init__(self, uri): |
|
204 |
self.uri = uri |
|
205 |
||
1482 | 206 |
|
0 | 207 |
class FakePool(object): |
208 |
def source(self, uri): |
|
209 |
return FakeSource(uri) |