author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 23 Jul 2009 15:28:41 +0200 | |
changeset 2456 | aa25d6b244c8 |
parent 2058 | 7ef12c03447c |
child 2650 | 18aec79ec3a3 |
permissions | -rw-r--r-- |
0 | 1 |
"""Hidden internals for the devtools.apptest module |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
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:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys, traceback |
|
11 |
||
12 |
from logilab.common.pytest import pause_tracing, resume_tracing |
|
13 |
||
14 |
import yams.schema |
|
15 |
||
16 |
from cubicweb.dbapi import repo_connect, ConnectionProperties, ProgrammingError |
|
17 |
from cubicweb.cwvreg import CubicWebRegistry |
|
18 |
||
19 |
from cubicweb.web.application import CubicWebPublisher |
|
20 |
from cubicweb.web import Redirect |
|
21 |
||
22 |
from cubicweb.devtools import ApptestConfiguration, init_test_database |
|
23 |
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
|
24 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
25 |
SYSTEM_ENTITIES = ('CWGroup', 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
26 |
'CWAttribute', 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
27 |
'CWConstraint', 'CWConstraintType', 'CWProperty', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1138
diff
changeset
|
28 |
'CWEType', 'CWRType', |
0 | 29 |
'State', 'Transition', 'TrInfo', |
30 |
'RQLExpression', |
|
31 |
) |
|
32 |
SYSTEM_RELATIONS = ( |
|
33 |
# virtual relation |
|
34 |
'identity', |
|
35 |
# metadata |
|
36 |
'is', 'is_instance_of', 'owned_by', 'created_by', 'specializes', |
|
37 |
# workflow related |
|
38 |
'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
|
39 |
'destination_state', 'in_state', 'wf_info_for', 'from_state', 'to_state', |
0 | 40 |
'condition', |
41 |
# permission |
|
42 |
'in_group', 'require_group', 'require_permission', |
|
43 |
'read_permission', 'update_permission', 'delete_permission', 'add_permission', |
|
44 |
# eproperty |
|
45 |
'for_user', |
|
46 |
# schema definition |
|
47 |
'relation_type', 'from_entity', 'to_entity', |
|
48 |
'constrained_by', 'cstrtype', 'widget', |
|
49 |
# deducted from other relations |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
50 |
'primary_email', |
0 | 51 |
) |
52 |
||
53 |
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
|
54 |
"""returned a Set of each non final entity type, excluding CWGroup, and CWUser... |
0 | 55 |
""" |
56 |
if strict: |
|
57 |
protected_entities = yams.schema.BASE_TYPES |
|
58 |
else: |
|
59 |
protected_entities = yams.schema.BASE_TYPES.union(set(SYSTEM_ENTITIES)) |
|
60 |
entities = set(app_schema.entities()) |
|
61 |
return entities - protected_entities |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
62 |
|
0 | 63 |
|
64 |
def ignore_relations(*relations): |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1133
diff
changeset
|
65 |
global SYSTEM_RELATIONS |
0 | 66 |
SYSTEM_RELATIONS += relations |
67 |
||
68 |
class TestEnvironment(object): |
|
69 |
"""TestEnvironment defines a context (e.g. a config + a given connection) in |
|
70 |
which the tests are executed |
|
71 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
72 |
|
0 | 73 |
def __init__(self, appid, reporter=None, verbose=False, |
74 |
configcls=ApptestConfiguration, requestcls=FakeRequest): |
|
75 |
config = configcls(appid) |
|
76 |
self.requestcls = requestcls |
|
77 |
self.cnx = None |
|
78 |
config.db_perms = False |
|
79 |
source = config.sources()['system'] |
|
80 |
if verbose: |
|
81 |
print "init test database ..." |
|
82 |
self.vreg = vreg = CubicWebRegistry(config) |
|
83 |
self.admlogin = source['db-user'] |
|
84 |
# restore database <=> init database |
|
85 |
self.restore_database() |
|
86 |
if verbose: |
|
87 |
print "init done" |
|
88 |
config.repository = lambda x=None: self.repo |
|
89 |
self.app = CubicWebPublisher(config, vreg=vreg) |
|
90 |
self.verbose = verbose |
|
91 |
schema = self.vreg.schema |
|
92 |
# else we may run into problems since email address are ususally share in app tests |
|
93 |
# 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
|
94 |
schema.rschema('primary_email').set_rproperty('CWUser', 'EmailAddress', 'composite', False) |
0 | 95 |
self.deletable_entities = unprotected_entities(schema) |
96 |
||
97 |
def restore_database(self): |
|
98 |
"""called by unittests' tearDown to restore the original database |
|
99 |
""" |
|
100 |
try: |
|
101 |
pause_tracing() |
|
102 |
if self.cnx: |
|
103 |
self.cnx.close() |
|
104 |
source = self.vreg.config.sources()['system'] |
|
105 |
self.repo, self.cnx = init_test_database(driver=source['db-driver'], |
|
106 |
vreg=self.vreg) |
|
107 |
self._orig_cnx = self.cnx |
|
108 |
resume_tracing() |
|
109 |
except: |
|
110 |
resume_tracing() |
|
111 |
traceback.print_exc() |
|
112 |
sys.exit(1) |
|
113 |
# XXX cnx decoration is usually done by the repository authentication manager, |
|
114 |
# necessary in authentication tests |
|
115 |
self.cnx.vreg = self.vreg |
|
116 |
self.cnx.login = source['db-user'] |
|
117 |
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
|
118 |
|
0 | 119 |
|
120 |
def create_user(self, login, groups=('users',), req=None): |
|
121 |
req = req or self.create_request() |
|
122 |
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
|
123 |
rset = cursor.execute('INSERT CWUser X: X login %(login)s, X upassword %(passwd)s,' |
0 | 124 |
'X in_state S WHERE S name "activated"', |
125 |
{'login': unicode(login), 'passwd': login.encode('utf8')}) |
|
126 |
user = rset.get_entity(0, 0) |
|
127 |
cursor.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
|
128 |
% ','.join(repr(g) for g in groups), |
|
129 |
{'x': user.eid}, 'x') |
|
130 |
user.clear_related_cache('in_group', 'subject') |
|
131 |
self._orig_cnx.commit() |
|
132 |
return user |
|
133 |
||
387
dbe9997ffc7e
password can now be given as optional argument
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
134 |
def login(self, login, password=None): |
0 | 135 |
if login == self.admlogin: |
136 |
self.restore_connection() |
|
137 |
else: |
|
387
dbe9997ffc7e
password can now be given as optional argument
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
138 |
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
|
139 |
password or str(login), |
0 | 140 |
ConnectionProperties('inmemory')) |
141 |
if login == self.vreg.config.anonymous_user()[0]: |
|
142 |
self.cnx.anonymous_connection = True |
|
143 |
return self.cnx |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
144 |
|
0 | 145 |
def restore_connection(self): |
146 |
if not self.cnx is self._orig_cnx: |
|
147 |
try: |
|
148 |
self.cnx.close() |
|
149 |
except ProgrammingError: |
|
150 |
pass # already closed |
|
151 |
self.cnx = self._orig_cnx |
|
152 |
||
153 |
############################################################################ |
|
154 |
||
155 |
def execute(self, rql, args=None, eidkey=None, req=None): |
|
156 |
"""executes <rql>, builds a resultset, and returns a couple (rset, req) |
|
157 |
where req is a FakeRequest |
|
158 |
""" |
|
159 |
req = req or self.create_request(rql=rql) |
|
160 |
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
|
161 |
|
0 | 162 |
def create_request(self, rql=None, **kwargs): |
163 |
"""executes <rql>, builds a resultset, and returns a |
|
164 |
couple (rset, req) where req is a FakeRequest |
|
165 |
""" |
|
166 |
if rql: |
|
167 |
kwargs['rql'] = rql |
|
168 |
req = self.requestcls(self.vreg, form=kwargs) |
|
169 |
req.set_connection(self.cnx) |
|
170 |
return req |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
171 |
|
0 | 172 |
def get_rset_and_req(self, rql, optional_args=None, args=None, eidkey=None): |
173 |
"""executes <rql>, builds a resultset, and returns a |
|
174 |
couple (rset, req) where req is a FakeRequest |
|
175 |
""" |
|
176 |
return (self.execute(rql, args, eidkey), |
|
177 |
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
|
178 |
|
0 | 179 |
def check_view(self, rql, vid, optional_args, template='main'): |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
180 |
"""checks if rendering view raises an exception in this environment |
0 | 181 |
|
182 |
If any exception is raised in this method, it will be considered |
|
183 |
as a TestFailure |
|
184 |
""" |
|
185 |
return self.call_view(vid, rql, |
|
186 |
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
|
187 |
|
0 | 188 |
def call_view(self, vid, rql, template='main', optional_args=None): |
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()""" |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
198 |
controller = self.vreg.select('controllers', 'edit', req) |
0 | 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>""" |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
226 |
for action in self.vreg.possible_vobjects('actions', req, rset=rset): |
0 | 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() |