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