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