author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 09 Sep 2009 18:38:25 +0200 | |
branch | 3.5 |
changeset 3155 | 14e30e2520d7 |
parent 2920 | 64322aa83a1d |
child 3225 | 464c5b9c6fa0 |
permissions | -rw-r--r-- |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2298
diff
changeset
|
1 |
"""This module provides misc utilities to test instances |
0 | 2 |
|
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1648
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:
1648
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 copy import deepcopy |
|
11 |
||
12 |
import simplejson |
|
13 |
||
14 |
from logilab.common.testlib import TestCase |
|
15 |
from logilab.common.pytest import nocoverage |
|
16 |
from logilab.common.umessage import message_from_string |
|
17 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
18 |
from logilab.common.deprecation import deprecated |
1394
ff1e912cbc39
remove pmainactions and deprecate paddrelactions
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1383
diff
changeset
|
19 |
|
0 | 20 |
from cubicweb.devtools import init_test_database, TestServerConfiguration, ApptestConfiguration |
21 |
from cubicweb.devtools._apptest import TestEnvironment |
|
22 |
from cubicweb.devtools.fake import FakeRequest |
|
23 |
||
24 |
from cubicweb.dbapi import repo_connect, ConnectionProperties, ProgrammingError |
|
25 |
||
26 |
||
27 |
MAILBOX = [] |
|
28 |
class Email: |
|
29 |
def __init__(self, recipients, msg): |
|
30 |
self.recipients = recipients |
|
31 |
self.msg = msg |
|
32 |
||
33 |
@property |
|
34 |
def message(self): |
|
35 |
return message_from_string(self.msg) |
|
1486 | 36 |
|
2298
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
37 |
@property |
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
38 |
def subject(self): |
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
39 |
return self.message.get('Subject') |
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
40 |
|
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
41 |
@property |
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
42 |
def content(self): |
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
43 |
return self.message.get_payload(decode=True) |
8dffb4cce3b7
nicer api to test sent email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2293
diff
changeset
|
44 |
|
0 | 45 |
def __repr__(self): |
46 |
return '<Email to %s with subject %s>' % (','.join(self.recipients), |
|
47 |
self.message.get('Subject')) |
|
1486 | 48 |
|
0 | 49 |
class MockSMTP: |
50 |
def __init__(self, server, port): |
|
51 |
pass |
|
52 |
def close(self): |
|
53 |
pass |
|
54 |
def sendmail(self, helo_addr, recipients, msg): |
|
55 |
MAILBOX.append(Email(recipients, msg)) |
|
56 |
||
2221
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
57 |
from cubicweb import cwconfig |
d9b85a7b0bdd
create sendmails method on cwconfig, use it in SendMailOp
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
58 |
cwconfig.SMTP = MockSMTP |
0 | 59 |
|
60 |
||
61 |
def get_versions(self, checkversions=False): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2298
diff
changeset
|
62 |
"""return the a dictionary containing cubes used by this instance |
0 | 63 |
as key with their version as value, including cubicweb version. This is a |
64 |
public method, not requiring a session id. |
|
65 |
||
66 |
replace Repository.get_versions by this method if you don't want versions |
|
67 |
checking |
|
68 |
""" |
|
69 |
vcconf = {'cubicweb': self.config.cubicweb_version()} |
|
70 |
self.config.bootstrap_cubes() |
|
71 |
for pk in self.config.cubes(): |
|
17
62ce3e6126e0
work in progress: fix cubicweb/server tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
72 |
version = self.config.cube_version(pk) |
0 | 73 |
vcconf[pk] = version |
74 |
self.config._cubes = None |
|
75 |
return vcconf |
|
76 |
||
77 |
||
78 |
@property |
|
79 |
def late_binding_env(self): |
|
80 |
"""builds TestEnvironment as late as possible""" |
|
81 |
if not hasattr(self, '_env'): |
|
82 |
self.__class__._env = TestEnvironment('data', configcls=self.configcls, |
|
83 |
requestcls=self.requestcls) |
|
84 |
return self._env |
|
85 |
||
86 |
||
87 |
class autoenv(type): |
|
88 |
"""automatically set environment on EnvBasedTC subclasses if necessary |
|
89 |
""" |
|
90 |
def __new__(mcs, name, bases, classdict): |
|
91 |
env = classdict.get('env') |
|
92 |
# try to find env in one of the base classes |
|
93 |
if env is None: |
|
94 |
for base in bases: |
|
95 |
env = getattr(base, 'env', None) |
|
96 |
if env is not None: |
|
97 |
classdict['env'] = env |
|
98 |
break |
|
99 |
if not classdict.get('__abstract__') and not classdict.get('env'): |
|
100 |
classdict['env'] = late_binding_env |
|
101 |
return super(autoenv, mcs).__new__(mcs, name, bases, classdict) |
|
102 |
||
103 |
||
104 |
class EnvBasedTC(TestCase): |
|
105 |
"""abstract class for test using an apptest environment |
|
106 |
""" |
|
107 |
__metaclass__ = autoenv |
|
108 |
__abstract__ = True |
|
109 |
env = None |
|
110 |
configcls = ApptestConfiguration |
|
111 |
requestcls = FakeRequest |
|
1486 | 112 |
|
0 | 113 |
# user / session management ############################################### |
114 |
||
115 |
def user(self, req=None): |
|
116 |
if req is None: |
|
117 |
req = self.env.create_request() |
|
118 |
return self.env.cnx.user(req) |
|
119 |
else: |
|
120 |
return req.user |
|
121 |
||
122 |
def create_user(self, *args, **kwargs): |
|
123 |
return self.env.create_user(*args, **kwargs) |
|
124 |
||
387
dbe9997ffc7e
password can now be given as optional argument
sylvain.thenault@logilab.fr
parents:
17
diff
changeset
|
125 |
def login(self, login, password=None): |
dbe9997ffc7e
password can now be given as optional argument
sylvain.thenault@logilab.fr
parents:
17
diff
changeset
|
126 |
return self.env.login(login, password) |
0 | 127 |
|
128 |
def restore_connection(self): |
|
129 |
self.env.restore_connection() |
|
1486 | 130 |
|
0 | 131 |
# db api ################################################################## |
132 |
||
133 |
@nocoverage |
|
134 |
def cursor(self, req=None): |
|
135 |
return self.env.cnx.cursor(req or self.request()) |
|
1486 | 136 |
|
0 | 137 |
@nocoverage |
138 |
def execute(self, *args, **kwargs): |
|
139 |
return self.env.execute(*args, **kwargs) |
|
140 |
||
141 |
@nocoverage |
|
142 |
def commit(self): |
|
143 |
self.env.cnx.commit() |
|
1486 | 144 |
|
0 | 145 |
@nocoverage |
146 |
def rollback(self): |
|
147 |
try: |
|
148 |
self.env.cnx.rollback() |
|
149 |
except ProgrammingError: |
|
150 |
pass |
|
1486 | 151 |
|
0 | 152 |
# other utilities ######################################################### |
153 |
def set_debug(self, debugmode): |
|
154 |
from cubicweb.server import set_debug |
|
155 |
set_debug(debugmode) |
|
1486 | 156 |
|
0 | 157 |
@property |
158 |
def config(self): |
|
159 |
return self.vreg.config |
|
160 |
||
161 |
def session(self): |
|
162 |
"""return current server side session (using default manager account)""" |
|
163 |
return self.env.repo._sessions[self.env.cnx.sessionid] |
|
1486 | 164 |
|
0 | 165 |
def request(self, *args, **kwargs): |
166 |
"""return a web interface request""" |
|
167 |
return self.env.create_request(*args, **kwargs) |
|
168 |
||
169 |
@nocoverage |
|
170 |
def rset_and_req(self, *args, **kwargs): |
|
171 |
return self.env.get_rset_and_req(*args, **kwargs) |
|
1486 | 172 |
|
0 | 173 |
def entity(self, rql, args=None, eidkey=None, req=None): |
174 |
return self.execute(rql, args, eidkey, req=req).get_entity(0, 0) |
|
1486 | 175 |
|
0 | 176 |
def etype_instance(self, etype, req=None): |
177 |
req = req or self.request() |
|
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:
2616
diff
changeset
|
178 |
e = self.env.vreg['etypes'].etype_class(etype)(req) |
0 | 179 |
e.eid = None |
180 |
return e |
|
1486 | 181 |
|
0 | 182 |
def add_entity(self, etype, **kwargs): |
183 |
rql = ['INSERT %s X' % etype] |
|
184 |
||
185 |
# dict for replacement in RQL Request |
|
186 |
rql_args = {} |
|
187 |
||
188 |
if kwargs: # |
|
189 |
rql.append(':') |
|
190 |
# dict to define new entities variables |
|
191 |
entities = {} |
|
192 |
||
193 |
# assignement part of the request |
|
194 |
sub_rql = [] |
|
195 |
for key, value in kwargs.iteritems(): |
|
196 |
# entities |
|
1486 | 197 |
if hasattr(value, 'eid'): |
0 | 198 |
new_value = "%s__" % key.upper() |
1486 | 199 |
|
0 | 200 |
entities[new_value] = value.eid |
201 |
rql_args[new_value] = value.eid |
|
1486 | 202 |
|
0 | 203 |
sub_rql.append("X %s %s" % (key, new_value)) |
204 |
# final attributes |
|
1486 | 205 |
else: |
0 | 206 |
sub_rql.append('X %s %%(%s)s' % (key, key)) |
207 |
rql_args[key] = value |
|
208 |
rql.append(', '.join(sub_rql)) |
|
209 |
||
210 |
||
211 |
if entities: |
|
212 |
rql.append('WHERE') |
|
213 |
# WHERE part of the request (to link entity to they eid) |
|
214 |
sub_rql = [] |
|
215 |
for key, value in entities.iteritems(): |
|
216 |
sub_rql.append("%s eid %%(%s)s" % (key, key)) |
|
217 |
rql.append(', '.join(sub_rql)) |
|
218 |
||
219 |
rql = ' '.join(rql) |
|
220 |
rset = self.execute(rql, rql_args) |
|
221 |
return rset.get_entity(0, 0) |
|
222 |
||
223 |
def set_option(self, optname, value): |
|
224 |
self.vreg.config.global_set_option(optname, value) |
|
225 |
||
226 |
def pviews(self, req, rset): |
|
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:
2616
diff
changeset
|
227 |
return sorted((a.id, a.__class__) for a in self.vreg['views'].possible_views(req, rset=rset)) |
1486 | 228 |
|
0 | 229 |
def pactions(self, req, rset, skipcategories=('addrelated', 'siteactions', 'useractions')): |
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:
2616
diff
changeset
|
230 |
return [(a.id, a.__class__) for a in self.vreg['actions'].possible_vobjects(req, rset=rset) |
0 | 231 |
if a.category not in skipcategories] |
1394
ff1e912cbc39
remove pmainactions and deprecate paddrelactions
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1383
diff
changeset
|
232 |
|
ff1e912cbc39
remove pmainactions and deprecate paddrelactions
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1383
diff
changeset
|
233 |
def pactions_by_cats(self, req, rset, categories=('addrelated',)): |
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:
2616
diff
changeset
|
234 |
return [(a.id, a.__class__) for a in self.vreg['actions'].possible_vobjects(req, rset=rset) |
1394
ff1e912cbc39
remove pmainactions and deprecate paddrelactions
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1383
diff
changeset
|
235 |
if a.category in categories] |
1486 | 236 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
237 |
paddrelactions = deprecated()(pactions_by_cats) |
1394
ff1e912cbc39
remove pmainactions and deprecate paddrelactions
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1383
diff
changeset
|
238 |
|
0 | 239 |
def pactionsdict(self, req, rset, skipcategories=('addrelated', 'siteactions', 'useractions')): |
240 |
res = {} |
|
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:
2616
diff
changeset
|
241 |
for a in self.vreg['actions'].possible_vobjects(req, rset=rset): |
0 | 242 |
if a.category not in skipcategories: |
243 |
res.setdefault(a.category, []).append(a.__class__) |
|
244 |
return res |
|
245 |
||
1486 | 246 |
|
0 | 247 |
def remote_call(self, fname, *args): |
248 |
"""remote call simulation""" |
|
249 |
dump = simplejson.dumps |
|
250 |
args = [dump(arg) for arg in args] |
|
1648 | 251 |
req = self.request(fname=fname, pageid='123', arg=args) |
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
252 |
ctrl = self.vreg['controllers'].select('json', req) |
0 | 253 |
return ctrl.publish(), req |
254 |
||
255 |
# default test setup and teardown ######################################### |
|
1486 | 256 |
|
0 | 257 |
def setup_database(self): |
258 |
pass |
|
259 |
||
260 |
def setUp(self): |
|
261 |
self.restore_connection() |
|
262 |
session = self.session() |
|
263 |
#self.maxeid = self.execute('Any MAX(X)') |
|
264 |
session.set_pool() |
|
265 |
self.maxeid = session.system_sql('SELECT MAX(eid) FROM entities').fetchone()[0] |
|
266 |
self.app = self.env.app |
|
267 |
self.vreg = self.env.app.vreg |
|
268 |
self.schema = self.vreg.schema |
|
269 |
self.vreg.config.mode = 'test' |
|
270 |
# set default-dest-addrs to a dumb email address to avoid mailbox or |
|
271 |
# mail queue pollution |
|
272 |
self.set_option('default-dest-addrs', ['whatever']) |
|
273 |
self.setup_database() |
|
274 |
self.commit() |
|
275 |
MAILBOX[:] = [] # reset mailbox |
|
1486 | 276 |
|
0 | 277 |
@nocoverage |
278 |
def tearDown(self): |
|
279 |
self.rollback() |
|
280 |
# self.env.restore_database() |
|
281 |
self.env.restore_connection() |
|
282 |
self.session().unsafe_execute('DELETE Any X WHERE X eid > %s' % self.maxeid) |
|
283 |
self.commit() |
|
284 |
||
2730
bb6fcb8c5d71
to make cw schemas importable, they have to be installed w/ cw code, not in /usr/share/cubicweb/schemas
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
285 |
# global resources accessors ############################################### |
0 | 286 |
|
287 |
# XXX |
|
288 |
try: |
|
289 |
from cubicweb.web import Redirect |
|
290 |
from urllib import unquote |
|
291 |
except ImportError: |
|
292 |
pass # cubicweb-web not installed |
|
293 |
else: |
|
294 |
class ControllerTC(EnvBasedTC): |
|
295 |
def setUp(self): |
|
296 |
super(ControllerTC, self).setUp() |
|
297 |
self.req = self.request() |
|
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
298 |
self.ctrl = self.vreg['controllers'].select('edit', self.req) |
0 | 299 |
|
300 |
def publish(self, req): |
|
301 |
assert req is self.ctrl.req |
|
302 |
try: |
|
303 |
result = self.ctrl.publish() |
|
304 |
req.cnx.commit() |
|
305 |
except Redirect: |
|
306 |
req.cnx.commit() |
|
307 |
raise |
|
308 |
return result |
|
309 |
||
310 |
def expect_redirect_publish(self, req=None): |
|
311 |
if req is not None: |
|
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
312 |
self.ctrl = self.vreg['controllers'].select('edit', req) |
0 | 313 |
else: |
314 |
req = self.req |
|
315 |
try: |
|
316 |
res = self.publish(req) |
|
317 |
except Redirect, ex: |
|
318 |
try: |
|
319 |
path, params = ex.location.split('?', 1) |
|
320 |
except: |
|
321 |
path, params = ex.location, "" |
|
322 |
req._url = path |
|
323 |
cleanup = lambda p: (p[0], unquote(p[1])) |
|
324 |
params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p) |
|
325 |
return req.relative_path(False), params # path.rsplit('/', 1)[-1], params |
|
326 |
else: |
|
327 |
self.fail('expected a Redirect exception') |
|
328 |
||
329 |
||
330 |
def make_late_binding_repo_property(attrname): |
|
331 |
@property |
|
332 |
def late_binding(self): |
|
333 |
"""builds cnx as late as possible""" |
|
334 |
if not hasattr(self, attrname): |
|
335 |
# sets explicit test mode here to avoid autoreload |
|
336 |
from cubicweb.cwconfig import CubicWebConfiguration |
|
337 |
CubicWebConfiguration.mode = 'test' |
|
338 |
cls = self.__class__ |
|
339 |
config = self.repo_config or TestServerConfiguration('data') |
|
340 |
cls._repo, cls._cnx = init_test_database('sqlite', config=config) |
|
341 |
return getattr(self, attrname) |
|
342 |
return late_binding |
|
343 |
||
344 |
||
345 |
class autorepo(type): |
|
346 |
"""automatically set repository on RepositoryBasedTC subclasses if necessary |
|
347 |
""" |
|
348 |
def __new__(mcs, name, bases, classdict): |
|
349 |
repo = classdict.get('repo') |
|
350 |
# try to find repo in one of the base classes |
|
351 |
if repo is None: |
|
352 |
for base in bases: |
|
353 |
repo = getattr(base, 'repo', None) |
|
354 |
if repo is not None: |
|
355 |
classdict['repo'] = repo |
|
356 |
break |
|
357 |
if name != 'RepositoryBasedTC' and not classdict.get('repo'): |
|
358 |
classdict['repo'] = make_late_binding_repo_property('_repo') |
|
359 |
classdict['cnx'] = make_late_binding_repo_property('_cnx') |
|
360 |
return super(autorepo, mcs).__new__(mcs, name, bases, classdict) |
|
361 |
||
362 |
||
363 |
class RepositoryBasedTC(TestCase): |
|
364 |
"""abstract class for test using direct repository connections |
|
365 |
""" |
|
366 |
__metaclass__ = autorepo |
|
367 |
repo_config = None # set a particular config instance if necessary |
|
1486 | 368 |
|
0 | 369 |
# user / session management ############################################### |
370 |
||
371 |
def create_user(self, user, groups=('users',), password=None, commit=True): |
|
372 |
if password is None: |
|
373 |
password = user |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
374 |
eid = self.execute('INSERT CWUser X: X login %(x)s, X upassword %(p)s', |
0 | 375 |
{'x': unicode(user), 'p': password})[0][0] |
376 |
groups = ','.join(repr(group) for group in groups) |
|
377 |
self.execute('SET X in_group Y WHERE X eid %%(x)s, Y name IN (%s)' % groups, |
|
378 |
{'x': eid}) |
|
379 |
if commit: |
|
380 |
self.commit() |
|
1486 | 381 |
self.session.reset_pool() |
0 | 382 |
return eid |
1486 | 383 |
|
0 | 384 |
def login(self, login, password=None): |
385 |
cnx = repo_connect(self.repo, unicode(login), password or login, |
|
386 |
ConnectionProperties('inmemory')) |
|
387 |
self.cnxs.append(cnx) |
|
388 |
return cnx |
|
389 |
||
390 |
def current_session(self): |
|
391 |
return self.repo._sessions[self.cnxs[-1].sessionid] |
|
1486 | 392 |
|
0 | 393 |
def restore_connection(self): |
394 |
assert len(self.cnxs) == 1, self.cnxs |
|
395 |
cnx = self.cnxs.pop() |
|
396 |
try: |
|
397 |
cnx.close() |
|
398 |
except Exception, ex: |
|
399 |
print "exception occured while closing connection", ex |
|
1486 | 400 |
|
0 | 401 |
# db api ################################################################## |
402 |
||
403 |
def execute(self, rql, args=None, eid_key=None): |
|
404 |
assert self.session.id == self.cnxid |
|
405 |
rset = self.__execute(self.cnxid, rql, args, eid_key) |
|
406 |
rset.vreg = self.vreg |
|
407 |
rset.req = self.session |
|
408 |
# call to set_pool is necessary to avoid pb when using |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2298
diff
changeset
|
409 |
# instance entities for convenience |
0 | 410 |
self.session.set_pool() |
411 |
return rset |
|
1486 | 412 |
|
0 | 413 |
def commit(self): |
414 |
self.__commit(self.cnxid) |
|
1486 | 415 |
self.session.set_pool() |
416 |
||
0 | 417 |
def rollback(self): |
418 |
self.__rollback(self.cnxid) |
|
1486 | 419 |
self.session.set_pool() |
420 |
||
0 | 421 |
def close(self): |
422 |
self.__close(self.cnxid) |
|
423 |
||
424 |
# other utilities ######################################################### |
|
1486 | 425 |
|
0 | 426 |
def set_debug(self, debugmode): |
427 |
from cubicweb.server import set_debug |
|
428 |
set_debug(debugmode) |
|
1486 | 429 |
|
0 | 430 |
def set_option(self, optname, value): |
431 |
self.vreg.config.global_set_option(optname, value) |
|
1486 | 432 |
|
0 | 433 |
def add_entity(self, etype, **kwargs): |
434 |
restrictions = ', '.join('X %s %%(%s)s' % (key, key) for key in kwargs) |
|
435 |
rql = 'INSERT %s X' % etype |
|
436 |
if kwargs: |
|
437 |
rql += ': %s' % ', '.join('X %s %%(%s)s' % (key, key) for key in kwargs) |
|
438 |
rset = self.execute(rql, kwargs) |
|
439 |
return rset.get_entity(0, 0) |
|
440 |
||
441 |
def default_user_password(self): |
|
442 |
config = self.repo.config #TestConfiguration('data') |
|
443 |
user = unicode(config.sources()['system']['db-user']) |
|
444 |
passwd = config.sources()['system']['db-password'] |
|
445 |
return user, passwd |
|
1486 | 446 |
|
0 | 447 |
def close_connections(self): |
448 |
for cnx in self.cnxs: |
|
449 |
try: |
|
450 |
cnx.rollback() |
|
451 |
cnx.close() |
|
452 |
except: |
|
453 |
continue |
|
454 |
self.cnxs = [] |
|
455 |
||
456 |
pactions = EnvBasedTC.pactions.im_func |
|
457 |
pactionsdict = EnvBasedTC.pactionsdict.im_func |
|
1486 | 458 |
|
0 | 459 |
# default test setup and teardown ######################################### |
1486 | 460 |
|
0 | 461 |
def _prepare(self): |
462 |
MAILBOX[:] = [] # reset mailbox |
|
463 |
if hasattr(self, 'cnxid'): |
|
464 |
return |
|
465 |
repo = self.repo |
|
466 |
self.__execute = repo.execute |
|
467 |
self.__commit = repo.commit |
|
468 |
self.__rollback = repo.rollback |
|
469 |
self.__close = repo.close |
|
976 | 470 |
self.cnxid = self.cnx.sessionid |
0 | 471 |
self.session = repo._sessions[self.cnxid] |
472 |
self.cnxs = [] |
|
473 |
# reset caches, they may introduce bugs among tests |
|
474 |
repo._type_source_cache = {} |
|
475 |
repo._extid_cache = {} |
|
476 |
repo.querier._rql_cache = {} |
|
477 |
for source in repo.sources: |
|
478 |
source.reset_caches() |
|
479 |
for s in repo.sources: |
|
480 |
if hasattr(s, '_cache'): |
|
481 |
s._cache = {} |
|
482 |
||
483 |
@property |
|
484 |
def config(self): |
|
485 |
return self.repo.config |
|
486 |
||
487 |
@property |
|
488 |
def vreg(self): |
|
489 |
return self.repo.vreg |
|
490 |
||
491 |
@property |
|
492 |
def schema(self): |
|
493 |
return self.repo.schema |
|
1486 | 494 |
|
0 | 495 |
def setUp(self): |
496 |
self._prepare() |
|
497 |
self.session.set_pool() |
|
498 |
self.maxeid = self.session.system_sql('SELECT MAX(eid) FROM entities').fetchone()[0] |
|
1486 | 499 |
|
1263 | 500 |
def tearDown(self): |
0 | 501 |
self.close_connections() |
502 |
self.rollback() |
|
503 |
self.session.unsafe_execute('DELETE Any X WHERE X eid > %(x)s', {'x': self.maxeid}) |
|
504 |
self.commit() |
|
1486 | 505 |