author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Fri, 22 May 2009 00:06:20 +0200 | |
changeset 1897 | ff2abadcf0cf |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""Hidden internals for the devtools.apptest module |
2 |
||
3 |
:organization: Logilab |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
import sys, traceback |
|
10 |
||
11 |
from logilab.common.pytest import pause_tracing, resume_tracing |
|
12 |
||
13 |
import yams.schema |
|
14 |
||
15 |
from cubicweb.dbapi import repo_connect, ConnectionProperties, ProgrammingError |
|
16 |
from cubicweb.cwvreg import CubicWebRegistry |
|
17 |
||
18 |
from cubicweb.web.application import CubicWebPublisher |
|
19 |
from cubicweb.web import Redirect |
|
20 |
||
21 |
from cubicweb.devtools import ApptestConfiguration, init_test_database |
|
22 |
from cubicweb.devtools.fake import FakeRequest |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
23 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
24 |
SYSTEM_ENTITIES = ('CWGroup', 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
25 |
'CWAttribute', 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
26 |
'CWConstraint', 'CWConstraintType', 'CWProperty', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
27 |
'CWEType', 'CWRType', |
0 | 28 |
'State', 'Transition', 'TrInfo', |
29 |
'RQLExpression', |
|
30 |
) |
|
31 |
SYSTEM_RELATIONS = ( |
|
32 |
# virtual relation |
|
33 |
'identity', |
|
34 |
# metadata |
|
35 |
'is', 'is_instance_of', 'owned_by', 'created_by', 'specializes', |
|
36 |
# workflow related |
|
37 |
'state_of', 'transition_of', 'initial_state', 'allowed_transition', |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
38 |
'destination_state', 'in_state', 'wf_info_for', 'from_state', 'to_state', |
0 | 39 |
'condition', |
40 |
# permission |
|
41 |
'in_group', 'require_group', 'require_permission', |
|
42 |
'read_permission', 'update_permission', 'delete_permission', 'add_permission', |
|
43 |
# eproperty |
|
44 |
'for_user', |
|
45 |
# schema definition |
|
46 |
'relation_type', 'from_entity', 'to_entity', |
|
47 |
'constrained_by', 'cstrtype', 'widget', |
|
48 |
# deducted from other relations |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
49 |
'primary_email', |
0 | 50 |
) |
51 |
||
52 |
def unprotected_entities(app_schema, strict=False): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
53 |
"""returned a Set of each non final entity type, excluding CWGroup, and CWUser... |
0 | 54 |
""" |
55 |
if strict: |
|
56 |
protected_entities = yams.schema.BASE_TYPES |
|
57 |
else: |
|
58 |
protected_entities = yams.schema.BASE_TYPES.union(set(SYSTEM_ENTITIES)) |
|
59 |
entities = set(app_schema.entities()) |
|
60 |
return entities - protected_entities |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
61 |
|
0 | 62 |
|
63 |
def ignore_relations(*relations): |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
64 |
global SYSTEM_RELATIONS |
0 | 65 |
SYSTEM_RELATIONS += relations |
66 |
||
67 |
class TestEnvironment(object): |
|
68 |
"""TestEnvironment defines a context (e.g. a config + a given connection) in |
|
69 |
which the tests are executed |
|
70 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
71 |
|
0 | 72 |
def __init__(self, appid, reporter=None, verbose=False, |
73 |
configcls=ApptestConfiguration, requestcls=FakeRequest): |
|
74 |
config = configcls(appid) |
|
75 |
self.requestcls = requestcls |
|
76 |
self.cnx = None |
|
77 |
config.db_perms = False |
|
78 |
source = config.sources()['system'] |
|
79 |
if verbose: |
|
80 |
print "init test database ..." |
|
81 |
self.vreg = vreg = CubicWebRegistry(config) |
|
82 |
self.admlogin = source['db-user'] |
|
83 |
# restore database <=> init database |
|
84 |
self.restore_database() |
|
85 |
if verbose: |
|
86 |
print "init done" |
|
87 |
config.repository = lambda x=None: self.repo |
|
88 |
self.app = CubicWebPublisher(config, vreg=vreg) |
|
89 |
self.verbose = verbose |
|
90 |
schema = self.vreg.schema |
|
91 |
# else we may run into problems since email address are ususally share in app tests |
|
92 |
# XXX should not be necessary anymore |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
93 |
schema.rschema('primary_email').set_rproperty('CWUser', 'EmailAddress', 'composite', False) |
0 | 94 |
self.deletable_entities = unprotected_entities(schema) |
95 |
||
96 |
def restore_database(self): |
|
97 |
"""called by unittests' tearDown to restore the original database |
|
98 |
""" |
|
99 |
try: |
|
100 |
pause_tracing() |
|
101 |
if self.cnx: |
|
102 |
self.cnx.close() |
|
103 |
source = self.vreg.config.sources()['system'] |
|
104 |
self.repo, self.cnx = init_test_database(driver=source['db-driver'], |
|
105 |
vreg=self.vreg) |
|
106 |
self._orig_cnx = self.cnx |
|
107 |
resume_tracing() |
|
108 |
except: |
|
109 |
resume_tracing() |
|
110 |
traceback.print_exc() |
|
111 |
sys.exit(1) |
|
112 |
# XXX cnx decoration is usually done by the repository authentication manager, |
|
113 |
# necessary in authentication tests |
|
114 |
self.cnx.vreg = self.vreg |
|
115 |
self.cnx.login = source['db-user'] |
|
116 |
self.cnx.password = source['db-password'] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
117 |
|
0 | 118 |
|
119 |
def create_user(self, login, groups=('users',), req=None): |
|
120 |
req = req or self.create_request() |
|
121 |
cursor = self._orig_cnx.cursor(req) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
122 |
rset = cursor.execute('INSERT CWUser X: X login %(login)s, X upassword %(passwd)s,' |
0 | 123 |
'X in_state S WHERE S name "activated"', |
124 |
{'login': unicode(login), 'passwd': login.encode('utf8')}) |
|
125 |
user = rset.get_entity(0, 0) |
|
126 |
cursor.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
|
127 |
% ','.join(repr(g) for g in groups), |
|
128 |
{'x': user.eid}, 'x') |
|
129 |
user.clear_related_cache('in_group', 'subject') |
|
130 |
self._orig_cnx.commit() |
|
131 |
return user |
|
132 |
||
387
dbe9997ffc7e
password can now be given as optional argument
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
133 |
def login(self, login, password=None): |
0 | 134 |
if login == self.admlogin: |
135 |
self.restore_connection() |
|
136 |
else: |
|
387
dbe9997ffc7e
password can now be given as optional argument
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
137 |
self.cnx = repo_connect(self.repo, unicode(login), |
dbe9997ffc7e
password can now be given as optional argument
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
138 |
password or str(login), |
0 | 139 |
ConnectionProperties('inmemory')) |
140 |
if login == self.vreg.config.anonymous_user()[0]: |
|
141 |
self.cnx.anonymous_connection = True |
|
142 |
return self.cnx |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
143 |
|
0 | 144 |
def restore_connection(self): |
145 |
if not self.cnx is self._orig_cnx: |
|
146 |
try: |
|
147 |
self.cnx.close() |
|
148 |
except ProgrammingError: |
|
149 |
pass # already closed |
|
150 |
self.cnx = self._orig_cnx |
|
151 |
||
152 |
############################################################################ |
|
153 |
||
154 |
def execute(self, rql, args=None, eidkey=None, req=None): |
|
155 |
"""executes <rql>, builds a resultset, and returns a couple (rset, req) |
|
156 |
where req is a FakeRequest |
|
157 |
""" |
|
158 |
req = req or self.create_request(rql=rql) |
|
159 |
return self.cnx.cursor(req).execute(unicode(rql), args, eidkey) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
160 |
|
0 | 161 |
def create_request(self, rql=None, **kwargs): |
162 |
"""executes <rql>, builds a resultset, and returns a |
|
163 |
couple (rset, req) where req is a FakeRequest |
|
164 |
""" |
|
165 |
if rql: |
|
166 |
kwargs['rql'] = rql |
|
167 |
req = self.requestcls(self.vreg, form=kwargs) |
|
168 |
req.set_connection(self.cnx) |
|
169 |
return req |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
170 |
|
0 | 171 |
def get_rset_and_req(self, rql, optional_args=None, args=None, eidkey=None): |
172 |
"""executes <rql>, builds a resultset, and returns a |
|
173 |
couple (rset, req) where req is a FakeRequest |
|
174 |
""" |
|
175 |
return (self.execute(rql, args, eidkey), |
|
176 |
self.create_request(rql=rql, **optional_args or {})) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
177 |
|
0 | 178 |
def check_view(self, rql, vid, optional_args, template='main'): |
179 |
"""checks if vreg.view() raises an exception in this environment |
|
180 |
||
181 |
If any exception is raised in this method, it will be considered |
|
182 |
as a TestFailure |
|
183 |
""" |
|
184 |
return self.call_view(vid, rql, |
|
185 |
template=template, optional_args=optional_args) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
186 |
|
0 | 187 |
def call_view(self, vid, rql, template='main', optional_args=None): |
188 |
"""shortcut for self.vreg.view()""" |
|
189 |
assert template |
|
190 |
if optional_args is None: |
|
191 |
optional_args = {} |
|
192 |
optional_args['vid'] = vid |
|
193 |
req = self.create_request(rql=rql, **optional_args) |
|
194 |
return self.vreg.main_template(req, template) |
|
195 |
||
196 |
def call_edit(self, req): |
|
197 |
"""shortcut for self.app.edit()""" |
|
198 |
controller = self.app.select_controller('edit', req) |
|
199 |
try: |
|
200 |
controller.publish() |
|
201 |
except Redirect: |
|
202 |
result = 'success' |
|
203 |
else: |
|
204 |
raise Exception('edit should raise Redirect on success') |
|
205 |
req.cnx.commit() |
|
206 |
return result |
|
207 |
||
208 |
def iter_possible_views(self, req, rset): |
|
209 |
"""returns a list of possible vids for <rql>""" |
|
210 |
for view in self.vreg.possible_views(req, rset): |
|
211 |
if view.category == 'startupview': |
|
212 |
continue |
|
213 |
yield view.id |
|
214 |
if rset.rowcount == 1: |
|
215 |
yield 'edition' |
|
216 |
||
217 |
def iter_startup_views(self, req): |
|
218 |
"""returns the list of startup views""" |
|
219 |
for view in self.vreg.possible_views(req, None): |
|
220 |
if view.category != 'startupview': |
|
221 |
continue |
|
222 |
yield view.id |
|
223 |
||
224 |
def iter_possible_actions(self, req, rset): |
|
225 |
"""returns a list of possible vids for <rql>""" |
|
226 |
for action in self.vreg.possible_vobjects('actions', req, rset): |
|
227 |
yield action |
|
228 |
||
229 |
class ExistingTestEnvironment(TestEnvironment): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
230 |
|
0 | 231 |
def __init__(self, appid, sourcefile, verbose=False): |
232 |
config = ApptestConfiguration(appid, sourcefile=sourcefile) |
|
233 |
if verbose: |
|
234 |
print "init test database ..." |
|
235 |
source = config.sources()['system'] |
|
236 |
self.vreg = CubicWebRegistry(config) |
|
1133 | 237 |
self.cnx = init_test_database(driver=source['db-driver'], |
238 |
vreg=self.vreg)[1] |
|
0 | 239 |
if verbose: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
240 |
print "init done" |
0 | 241 |
self.app = CubicWebPublisher(config, vreg=self.vreg) |
242 |
self.verbose = verbose |
|
243 |
# this is done when the publisher is opening a connection |
|
244 |
self.cnx.vreg = self.vreg |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
245 |
|
0 | 246 |
def setup(self, config=None): |
247 |
"""config is passed by TestSuite but is ignored in this environment""" |
|
248 |
cursor = self.cnx.cursor() |
|
249 |
self.last_eid = cursor.execute('Any X WHERE X creation_date D ORDERBY D DESC LIMIT 1').rows[0][0] |
|
250 |
||
251 |
def cleanup(self): |
|
252 |
"""cancel inserted elements during tests""" |
|
253 |
cursor = self.cnx.cursor() |
|
254 |
cursor.execute('DELETE Any X WHERE X eid > %(x)s', {'x' : self.last_eid}, eid_key='x') |
|
255 |
print "cleaning done" |
|
256 |
self.cnx.commit() |