author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 17 Feb 2009 23:46:48 +0100 | |
branch | tls-sprint |
changeset 727 | 30fe8f5afbd8 |
parent 563 | a690996639ca |
child 823 | cb8ccbef8fa5 |
child 1004 | 625e59773119 |
permissions | -rw-r--r-- |
0 | 1 |
"""this module contains base classes for web tests |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
import sys |
|
10 |
from math import log |
|
11 |
||
12 |
from logilab.common.debugger import Debugger |
|
13 |
from logilab.common.testlib import InnerTest |
|
14 |
from logilab.common.pytest import nocoverage |
|
15 |
||
16 |
from rql import parse |
|
17 |
||
18 |
from cubicweb.devtools import VIEW_VALIDATORS |
|
19 |
from cubicweb.devtools.apptest import EnvBasedTC |
|
20 |
from cubicweb.devtools._apptest import unprotected_entities, SYSTEM_RELATIONS |
|
21 |
from cubicweb.devtools.htmlparser import DTDValidator, SaxOnlyValidator, HTMLValidator |
|
22 |
from cubicweb.devtools.fill import insert_entity_queries, make_relations_queries |
|
23 |
||
24 |
from cubicweb.sobjects.notification import NotificationView |
|
25 |
||
26 |
from cubicweb.vregistry import NoSelectableObject |
|
27 |
from cubicweb.web.action import Action |
|
28 |
from cubicweb.web.views.basetemplates import TheMainTemplate |
|
29 |
||
30 |
||
31 |
## TODO ############### |
|
32 |
# creation tests: make sure an entity was actually created |
|
33 |
# Existing Test Environment |
|
34 |
||
35 |
class CubicWebDebugger(Debugger): |
|
36 |
||
37 |
def do_view(self, arg): |
|
38 |
import webbrowser |
|
39 |
data = self._getval(arg) |
|
40 |
file('/tmp/toto.html', 'w').write(data) |
|
41 |
webbrowser.open('file:///tmp/toto.html') |
|
42 |
||
43 |
def how_many_dict(schema, cursor, how_many, skip): |
|
44 |
"""compute how many entities by type we need to be able to satisfy relations |
|
45 |
cardinality |
|
46 |
""" |
|
47 |
# compute how many entities by type we need to be able to satisfy relation constraint |
|
48 |
relmap = {} |
|
49 |
for rschema in schema.relations(): |
|
50 |
if rschema.meta or rschema.is_final(): # skip meta relations |
|
51 |
continue |
|
52 |
for subj, obj in rschema.iter_rdefs(): |
|
53 |
card = rschema.rproperty(subj, obj, 'cardinality') |
|
54 |
if card[0] in '1?' and len(rschema.subjects(obj)) == 1: |
|
55 |
relmap.setdefault((rschema, subj), []).append(str(obj)) |
|
56 |
if card[1] in '1?' and len(rschema.objects(subj)) == 1: |
|
57 |
relmap.setdefault((rschema, obj), []).append(str(subj)) |
|
58 |
unprotected = unprotected_entities(schema) |
|
59 |
for etype in skip: |
|
60 |
unprotected.add(etype) |
|
61 |
howmanydict = {} |
|
62 |
for etype in unprotected_entities(schema, strict=True): |
|
63 |
howmanydict[str(etype)] = cursor.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0] |
|
64 |
if etype in unprotected: |
|
65 |
howmanydict[str(etype)] += how_many |
|
66 |
for (rschema, etype), targets in relmap.iteritems(): |
|
67 |
# XXX should 1. check no cycle 2. propagate changes |
|
68 |
relfactor = sum(howmanydict[e] for e in targets) |
|
69 |
howmanydict[str(etype)] = max(relfactor, howmanydict[etype]) |
|
70 |
return howmanydict |
|
71 |
||
72 |
||
73 |
def line_context_filter(line_no, center, before=3, after=None): |
|
74 |
"""return true if line are in context |
|
75 |
if after is None: after = before""" |
|
76 |
if after is None: |
|
77 |
after = before |
|
78 |
return center - before <= line_no <= center + after |
|
79 |
||
80 |
## base webtest class ######################################################### |
|
563
a690996639ca
[testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
562
diff
changeset
|
81 |
VALMAP = {None: None, 'dtd': DTDValidator, 'xml': SaxOnlyValidator} |
a690996639ca
[testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
562
diff
changeset
|
82 |
|
0 | 83 |
class WebTest(EnvBasedTC): |
84 |
"""base class for web tests""" |
|
85 |
__abstract__ = True |
|
86 |
||
87 |
pdbclass = CubicWebDebugger |
|
88 |
# this is a hook to be able to define a list of rql queries |
|
89 |
# that are application dependent and cannot be guessed automatically |
|
90 |
application_rql = [] |
|
91 |
||
92 |
# validators are used to validate (XML, DTD, whatever) view's content |
|
93 |
# validators availables are : |
|
94 |
# DTDValidator : validates XML + declared DTD |
|
95 |
# SaxOnlyValidator : guarantees XML is well formed |
|
96 |
# None : do not try to validate anything |
|
97 |
# validators used must be imported from from.devtools.htmlparser |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
98 |
content_type_validators = { |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
99 |
# maps MIME type : validator name |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
100 |
# |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
101 |
# do not set html validators here, we need HTMLValidator for html |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
102 |
# snippets |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
103 |
#'text/html': DTDValidator, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
104 |
#'application/xhtml+xml': DTDValidator, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
105 |
'application/xml': SaxOnlyValidator, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
106 |
'text/xml': SaxOnlyValidator, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
107 |
'text/plain': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
108 |
'text/comma-separated-values': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
109 |
'text/x-vcard': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
110 |
'text/calendar': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
111 |
'application/json': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
112 |
'image/png': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
113 |
} |
562
bdadb26c4a3c
old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents:
549
diff
changeset
|
114 |
# maps vid : validator name (override content_type_validators) |
563
a690996639ca
[testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
562
diff
changeset
|
115 |
vid_validators = dict((vid, VALMAP[valkey]) |
562
bdadb26c4a3c
old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents:
549
diff
changeset
|
116 |
for vid, valkey in VIEW_VALIDATORS.iteritems()) |
bdadb26c4a3c
old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents:
549
diff
changeset
|
117 |
|
0 | 118 |
no_auto_populate = () |
562
bdadb26c4a3c
old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents:
549
diff
changeset
|
119 |
ignored_relations = () |
0 | 120 |
|
121 |
def custom_populate(self, how_many, cursor): |
|
122 |
pass |
|
123 |
||
124 |
def post_populate(self, cursor): |
|
125 |
pass |
|
126 |
||
127 |
@nocoverage |
|
128 |
def auto_populate(self, how_many): |
|
129 |
"""this method populates the database with `how_many` entities |
|
130 |
of each possible type. It also inserts random relations between them |
|
131 |
""" |
|
132 |
cu = self.cursor() |
|
133 |
self.custom_populate(how_many, cu) |
|
134 |
vreg = self.vreg |
|
135 |
howmanydict = how_many_dict(self.schema, cu, how_many, self.no_auto_populate) |
|
136 |
for etype in unprotected_entities(self.schema): |
|
137 |
if etype in self.no_auto_populate: |
|
138 |
continue |
|
139 |
nb = howmanydict.get(etype, how_many) |
|
140 |
for rql, args in insert_entity_queries(etype, self.schema, vreg, nb): |
|
141 |
cu.execute(rql, args) |
|
142 |
edict = {} |
|
143 |
for etype in unprotected_entities(self.schema, strict=True): |
|
144 |
rset = cu.execute('%s X' % etype) |
|
145 |
edict[str(etype)] = set(row[0] for row in rset.rows) |
|
146 |
existingrels = {} |
|
147 |
ignored_relations = SYSTEM_RELATIONS + self.ignored_relations |
|
148 |
for rschema in self.schema.relations(): |
|
149 |
if rschema.is_final() or rschema in ignored_relations: |
|
150 |
continue |
|
151 |
rset = cu.execute('DISTINCT Any X,Y WHERE X %s Y' % rschema) |
|
152 |
existingrels.setdefault(rschema.type, set()).update((x,y) for x, y in rset) |
|
153 |
q = make_relations_queries(self.schema, edict, cu, ignored_relations, |
|
154 |
existingrels=existingrels) |
|
155 |
for rql, args in q: |
|
156 |
cu.execute(rql, args) |
|
157 |
self.post_populate(cu) |
|
158 |
self.commit() |
|
159 |
||
160 |
@nocoverage |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
161 |
def _check_html(self, output, view, template='main'): |
0 | 162 |
"""raises an exception if the HTML is invalid""" |
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
163 |
try: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
164 |
validatorclass = self.vid_validators[view.id] |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
165 |
except KeyError: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
166 |
if template is None: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
167 |
default_validator = HTMLValidator |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
168 |
else: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
169 |
default_validator = DTDValidator |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
170 |
validatorclass = self.content_type_validators.get(view.content_type, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
171 |
default_validator) |
0 | 172 |
if validatorclass is None: |
173 |
return None |
|
174 |
validator = validatorclass() |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
175 |
return validator.parse_string(output.strip()) |
0 | 176 |
|
177 |
||
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
178 |
def view(self, vid, rset, req=None, template='main', **kwargs): |
0 | 179 |
"""This method tests the view `vid` on `rset` using `template` |
180 |
||
181 |
If no error occured while rendering the view, the HTML is analyzed |
|
182 |
and parsed. |
|
183 |
||
184 |
:returns: an instance of `cubicweb.devtools.htmlparser.PageInfo` |
|
185 |
encapsulation the generated HTML |
|
186 |
""" |
|
187 |
req = req or rset.req |
|
188 |
# print "testing ", vid, |
|
189 |
# if rset: |
|
190 |
# print rset, len(rset), id(rset) |
|
191 |
# else: |
|
192 |
# print |
|
193 |
req.form['vid'] = vid |
|
194 |
view = self.vreg.select_view(vid, req, rset, **kwargs) |
|
195 |
# set explicit test description |
|
196 |
if rset is not None: |
|
197 |
self.set_description("testing %s, mod=%s (%s)" % (vid, view.__module__, rset.printable_rql())) |
|
198 |
else: |
|
199 |
self.set_description("testing %s, mod=%s (no rset)" % (vid, view.__module__)) |
|
200 |
viewfunc = lambda **k: self.vreg.main_template(req, template, **kwargs) |
|
201 |
if template is None: # raw view testing, no template |
|
202 |
viewfunc = view.dispatch |
|
203 |
elif template == 'main': |
|
204 |
_select_view_and_rset = TheMainTemplate._select_view_and_rset |
|
205 |
# patch TheMainTemplate.process_rql to avoid recomputing resultset |
|
535
ad9ac2169089
set self.rset else we're missing some stuff since TheMainTemplate instance as None as rset
sylvain.thenault@logilab.fr
parents:
534
diff
changeset
|
206 |
def __select_view_and_rset(self, view=view, rset=rset): |
ad9ac2169089
set self.rset else we're missing some stuff since TheMainTemplate instance as None as rset
sylvain.thenault@logilab.fr
parents:
534
diff
changeset
|
207 |
self.rset = rset |
ad9ac2169089
set self.rset else we're missing some stuff since TheMainTemplate instance as None as rset
sylvain.thenault@logilab.fr
parents:
534
diff
changeset
|
208 |
return view, rset |
ad9ac2169089
set self.rset else we're missing some stuff since TheMainTemplate instance as None as rset
sylvain.thenault@logilab.fr
parents:
534
diff
changeset
|
209 |
TheMainTemplate._select_view_and_rset = __select_view_and_rset |
0 | 210 |
try: |
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
211 |
return self._test_view(viewfunc, view, template, **kwargs) |
0 | 212 |
finally: |
213 |
if template == 'main': |
|
214 |
TheMainTemplate._select_view_and_rset = _select_view_and_rset |
|
215 |
||
216 |
||
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
217 |
def _test_view(self, viewfunc, view, template='main', **kwargs): |
0 | 218 |
"""this method does the actual call to the view |
219 |
||
220 |
If no error occured while rendering the view, the HTML is analyzed |
|
221 |
and parsed. |
|
222 |
||
223 |
:returns: an instance of `cubicweb.devtools.htmlparser.PageInfo` |
|
224 |
encapsulation the generated HTML |
|
225 |
""" |
|
226 |
output = None |
|
227 |
try: |
|
228 |
output = viewfunc(**kwargs) |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
229 |
return self._check_html(output, view, template) |
0 | 230 |
except (SystemExit, KeyboardInterrupt): |
231 |
raise |
|
232 |
except: |
|
233 |
# hijack exception: generative tests stop when the exception |
|
234 |
# is not an AssertionError |
|
235 |
klass, exc, tcbk = sys.exc_info() |
|
236 |
try: |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
237 |
msg = '[%s in %s] %s' % (klass, view.id, exc) |
0 | 238 |
except: |
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
239 |
msg = '[%s in %s] undisplayable exception' % (klass, view.id) |
0 | 240 |
if output is not None: |
241 |
position = getattr(exc, "position", (0,))[0] |
|
242 |
if position: |
|
243 |
# define filter |
|
244 |
output = output.splitlines() |
|
245 |
width = int(log(len(output), 10)) + 1 |
|
246 |
line_template = " %" + ("%i" % width) + "i: %s" |
|
247 |
# XXX no need to iterate the whole file except to get |
|
248 |
# the line number |
|
249 |
output = '\n'.join(line_template % (idx + 1, line) |
|
250 |
for idx, line in enumerate(output) |
|
251 |
if line_context_filter(idx+1, position)) |
|
252 |
msg+= '\nfor output:\n%s' % output |
|
253 |
raise AssertionError, msg, tcbk |
|
254 |
||
427
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
255 |
|
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
256 |
def to_test_etypes(self): |
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
257 |
return unprotected_entities(self.schema, strict=True) |
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
258 |
|
0 | 259 |
def iter_automatic_rsets(self): |
260 |
"""generates basic resultsets for each entity type""" |
|
427
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
261 |
etypes = self.to_test_etypes() |
0 | 262 |
for etype in etypes: |
263 |
yield self.execute('Any X WHERE X is %s' % etype) |
|
264 |
||
265 |
etype1 = etypes.pop() |
|
266 |
etype2 = etypes.pop() |
|
267 |
# test a mixed query (DISTINCT/GROUP to avoid getting duplicate |
|
268 |
# X which make muledit view failing for instance (html validation fails |
|
269 |
# because of some duplicate "id" attributes) |
|
270 |
yield self.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' % (etype1, etype2)) |
|
271 |
# test some application-specific queries if defined |
|
272 |
for rql in self.application_rql: |
|
273 |
yield self.execute(rql) |
|
274 |
||
275 |
||
276 |
def list_views_for(self, rset): |
|
277 |
"""returns the list of views that can be applied on `rset`""" |
|
278 |
req = rset.req |
|
279 |
only_once_vids = ('primary', 'secondary', 'text') |
|
280 |
req.data['ex'] = ValueError("whatever") |
|
281 |
for vid, views in self.vreg.registry('views').items(): |
|
282 |
if vid[0] == '_': |
|
283 |
continue |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
284 |
if rset.rowcount > 1 and vid in only_once_vids: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
285 |
continue |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
286 |
views = [view for view in views |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
287 |
if view.category != 'startupview' |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
288 |
and not issubclass(view, NotificationView)] |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
289 |
if views: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
290 |
try: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
291 |
view = self.vreg.select(views, req, rset) |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
292 |
if view.linkable(): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
293 |
yield view |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
294 |
else: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
295 |
not_selected(self.vreg, view) |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
296 |
# else the view is expected to be used as subview and should |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
297 |
# not be tested directly |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
298 |
except NoSelectableObject: |
0 | 299 |
continue |
300 |
||
301 |
def list_actions_for(self, rset): |
|
302 |
"""returns the list of actions that can be applied on `rset`""" |
|
303 |
req = rset.req |
|
304 |
for action in self.vreg.possible_objects('actions', req, rset): |
|
305 |
yield action |
|
306 |
||
307 |
def list_boxes_for(self, rset): |
|
308 |
"""returns the list of boxes that can be applied on `rset`""" |
|
309 |
req = rset.req |
|
310 |
for box in self.vreg.possible_objects('boxes', req, rset): |
|
311 |
yield box |
|
312 |
||
313 |
def list_startup_views(self): |
|
314 |
"""returns the list of startup views""" |
|
315 |
req = self.request() |
|
316 |
for view in self.vreg.possible_views(req, None): |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
317 |
if view.category == 'startupview': |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
318 |
yield view.id |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
319 |
else: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
320 |
not_selected(self.vreg, view) |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
321 |
|
0 | 322 |
def _test_everything_for(self, rset): |
323 |
"""this method tries to find everything that can be tested |
|
324 |
for `rset` and yields a callable test (as needed in generative tests) |
|
325 |
""" |
|
326 |
propdefs = self.vreg['propertydefs'] |
|
327 |
# make all components visible |
|
328 |
for k, v in propdefs.items(): |
|
329 |
if k.endswith('visible') and not v['default']: |
|
330 |
propdefs[k]['default'] = True |
|
331 |
for view in self.list_views_for(rset): |
|
332 |
backup_rset = rset._prepare_copy(rset.rows, rset.description) |
|
333 |
yield InnerTest(self._testname(rset, view.id, 'view'), |
|
334 |
self.view, view.id, rset, |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
335 |
rset.req.reset_headers(), 'main') |
0 | 336 |
# We have to do this because some views modify the |
337 |
# resultset's syntax tree |
|
338 |
rset = backup_rset |
|
339 |
for action in self.list_actions_for(rset): |
|
340 |
# XXX this seems a bit dummy |
|
341 |
#yield InnerTest(self._testname(rset, action.id, 'action'), |
|
342 |
# self.failUnless, |
|
343 |
# isinstance(action, Action)) |
|
344 |
yield InnerTest(self._testname(rset, action.id, 'action'), action.url) |
|
345 |
for box in self.list_boxes_for(rset): |
|
346 |
yield InnerTest(self._testname(rset, box.id, 'box'), box.dispatch) |
|
347 |
||
348 |
@staticmethod |
|
349 |
def _testname(rset, objid, objtype): |
|
350 |
return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype) |
|
351 |
||
352 |
||
353 |
class AutomaticWebTest(WebTest): |
|
354 |
"""import this if you wan automatic tests to be ran""" |
|
355 |
## one each |
|
356 |
def test_one_each_config(self): |
|
357 |
self.auto_populate(1) |
|
358 |
for rset in self.iter_automatic_rsets(): |
|
359 |
for testargs in self._test_everything_for(rset): |
|
360 |
yield testargs |
|
361 |
||
362 |
## ten each |
|
363 |
def test_ten_each_config(self): |
|
364 |
self.auto_populate(10) |
|
365 |
for rset in self.iter_automatic_rsets(): |
|
366 |
for testargs in self._test_everything_for(rset): |
|
367 |
yield testargs |
|
368 |
||
369 |
## startup views |
|
370 |
def test_startup_views(self): |
|
371 |
for vid in self.list_startup_views(): |
|
372 |
req = self.request() |
|
373 |
yield self.view, vid, None, req |
|
374 |
||
375 |
||
376 |
class RealDBTest(WebTest): |
|
377 |
||
378 |
def iter_individual_rsets(self, etypes=None, limit=None): |
|
379 |
etypes = etypes or unprotected_entities(self.schema, strict=True) |
|
380 |
for etype in etypes: |
|
381 |
rset = self.execute('Any X WHERE X is %s' % etype) |
|
382 |
for row in xrange(len(rset)): |
|
383 |
if limit and row > limit: |
|
384 |
break |
|
385 |
rset2 = rset.limit(limit=1, offset=row) |
|
386 |
yield rset2 |
|
387 |
||
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
388 |
def not_selected(vreg, vobject): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
389 |
try: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
390 |
vreg._selected[vobject.__class__] -= 1 |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
391 |
except (KeyError, AttributeError): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
392 |
pass |
0 | 393 |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
394 |
def vreg_instrumentize(testclass): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
395 |
from cubicweb.devtools.apptest import TestEnvironment |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
396 |
env = testclass._env = TestEnvironment('data', configcls=testclass.configcls, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
397 |
requestcls=testclass.requestcls) |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
398 |
vreg = env.vreg |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
399 |
vreg._selected = {} |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
400 |
orig_select = vreg.__class__.select |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
401 |
def instr_select(self, *args, **kwargs): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
402 |
selected = orig_select(self, *args, **kwargs) |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
403 |
try: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
404 |
self._selected[selected.__class__] += 1 |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
405 |
except KeyError: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
406 |
self._selected[selected.__class__] = 1 |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
407 |
except AttributeError: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
408 |
pass # occurs on vreg used to restore database |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
409 |
return selected |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
410 |
vreg.__class__.select = instr_select |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
411 |
|
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
412 |
def print_untested_objects(testclass, skipregs=('hooks', 'etypes')): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
413 |
vreg = testclass._env.vreg |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
414 |
for registry, vobjectsdict in vreg.items(): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
415 |
if registry in skipregs: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
416 |
continue |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
417 |
for vobjects in vobjectsdict.values(): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
418 |
for vobject in vobjects: |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
419 |
if not vreg._selected.get(vobject): |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
420 |
print 'not tested', registry, vobject |