author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Jun 2009 15:06:55 +0200 | |
changeset 2057 | 0a0cbccafcb5 |
parent 1977 | 606923dff11b |
child 2058 | 7ef12c03447c |
child 2070 | d98f5be5e371 |
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 |
# print "testing ", vid, |
186 |
# if rset: |
|
187 |
# print rset, len(rset), id(rset) |
|
188 |
# else: |
|
1605 | 189 |
# print |
0 | 190 |
req.form['vid'] = vid |
191 |
view = self.vreg.select_view(vid, req, rset, **kwargs) |
|
192 |
# set explicit test description |
|
193 |
if rset is not None: |
|
194 |
self.set_description("testing %s, mod=%s (%s)" % (vid, view.__module__, rset.printable_rql())) |
|
195 |
else: |
|
196 |
self.set_description("testing %s, mod=%s (no rset)" % (vid, view.__module__)) |
|
197 |
if template is None: # raw view testing, no template |
|
1773 | 198 |
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
|
199 |
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
|
200 |
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
|
201 |
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
|
202 |
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
|
203 |
return self._test_view(viewfunc, view, template, kwargs) |
0 | 204 |
|
205 |
||
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
|
206 |
def _test_view(self, viewfunc, view, template='main-template', kwargs={}): |
0 | 207 |
"""this method does the actual call to the view |
208 |
||
209 |
If no error occured while rendering the view, the HTML is analyzed |
|
210 |
and parsed. |
|
211 |
||
212 |
:returns: an instance of `cubicweb.devtools.htmlparser.PageInfo` |
|
213 |
encapsulation the generated HTML |
|
214 |
""" |
|
215 |
output = None |
|
216 |
try: |
|
217 |
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
|
218 |
return self._check_html(output, view, template) |
0 | 219 |
except (SystemExit, KeyboardInterrupt): |
220 |
raise |
|
221 |
except: |
|
222 |
# hijack exception: generative tests stop when the exception |
|
223 |
# is not an AssertionError |
|
224 |
klass, exc, tcbk = sys.exc_info() |
|
225 |
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
|
226 |
msg = '[%s in %s] %s' % (klass, view.id, exc) |
0 | 227 |
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
|
228 |
msg = '[%s in %s] undisplayable exception' % (klass, view.id) |
0 | 229 |
if output is not None: |
230 |
position = getattr(exc, "position", (0,))[0] |
|
231 |
if position: |
|
232 |
# define filter |
|
233 |
output = output.splitlines() |
|
234 |
width = int(log(len(output), 10)) + 1 |
|
235 |
line_template = " %" + ("%i" % width) + "i: %s" |
|
236 |
# XXX no need to iterate the whole file except to get |
|
237 |
# the line number |
|
238 |
output = '\n'.join(line_template % (idx + 1, line) |
|
239 |
for idx, line in enumerate(output) |
|
240 |
if line_context_filter(idx+1, position)) |
|
1133 | 241 |
msg += '\nfor output:\n%s' % output |
0 | 242 |
raise AssertionError, msg, tcbk |
243 |
||
427
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
244 |
|
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
245 |
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
|
246 |
return unprotected_entities(self.schema, strict=True) |
1605 | 247 |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
248 |
def iter_automatic_rsets(self, limit=10): |
0 | 249 |
"""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
|
250 |
etypes = self.to_test_etypes() |
0 | 251 |
for etype in etypes: |
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
252 |
yield self.execute('Any X LIMIT %s WHERE X is %s' % (limit, etype)) |
0 | 253 |
etype1 = etypes.pop() |
1775
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
254 |
try: |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
255 |
etype2 = etypes.pop() |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
256 |
except KeyError: |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
257 |
etype2 = etype1 |
0 | 258 |
# test a mixed query (DISTINCT/GROUP to avoid getting duplicate |
259 |
# X which make muledit view failing for instance (html validation fails |
|
260 |
# because of some duplicate "id" attributes) |
|
261 |
yield self.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' % (etype1, etype2)) |
|
262 |
# test some application-specific queries if defined |
|
263 |
for rql in self.application_rql: |
|
264 |
yield self.execute(rql) |
|
265 |
||
1605 | 266 |
|
0 | 267 |
def list_views_for(self, rset): |
268 |
"""returns the list of views that can be applied on `rset`""" |
|
269 |
req = rset.req |
|
270 |
only_once_vids = ('primary', 'secondary', 'text') |
|
271 |
req.data['ex'] = ValueError("whatever") |
|
272 |
for vid, views in self.vreg.registry('views').items(): |
|
273 |
if vid[0] == '_': |
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
# 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
|
288 |
# 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
|
289 |
except NoSelectableObject: |
0 | 290 |
continue |
291 |
||
292 |
def list_actions_for(self, rset): |
|
293 |
"""returns the list of actions that can be applied on `rset`""" |
|
294 |
req = rset.req |
|
295 |
for action in self.vreg.possible_objects('actions', req, rset): |
|
296 |
yield action |
|
297 |
||
298 |
def list_boxes_for(self, rset): |
|
299 |
"""returns the list of boxes that can be applied on `rset`""" |
|
300 |
req = rset.req |
|
301 |
for box in self.vreg.possible_objects('boxes', req, rset): |
|
302 |
yield box |
|
1605 | 303 |
|
0 | 304 |
def list_startup_views(self): |
305 |
"""returns the list of startup views""" |
|
306 |
req = self.request() |
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
not_selected(self.vreg, view) |
1605 | 312 |
|
0 | 313 |
def _test_everything_for(self, rset): |
314 |
"""this method tries to find everything that can be tested |
|
315 |
for `rset` and yields a callable test (as needed in generative tests) |
|
316 |
""" |
|
317 |
propdefs = self.vreg['propertydefs'] |
|
318 |
# make all components visible |
|
319 |
for k, v in propdefs.items(): |
|
320 |
if k.endswith('visible') and not v['default']: |
|
321 |
propdefs[k]['default'] = True |
|
322 |
for view in self.list_views_for(rset): |
|
323 |
backup_rset = rset._prepare_copy(rset.rows, rset.description) |
|
324 |
yield InnerTest(self._testname(rset, view.id, 'view'), |
|
325 |
self.view, view.id, rset, |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
563
diff
changeset
|
326 |
rset.req.reset_headers(), 'main-template') |
0 | 327 |
# We have to do this because some views modify the |
328 |
# resultset's syntax tree |
|
329 |
rset = backup_rset |
|
330 |
for action in self.list_actions_for(rset): |
|
331 |
yield InnerTest(self._testname(rset, action.id, 'action'), action.url) |
|
332 |
for box in self.list_boxes_for(rset): |
|
1773 | 333 |
yield InnerTest(self._testname(rset, box.id, 'box'), box.render) |
0 | 334 |
|
335 |
@staticmethod |
|
336 |
def _testname(rset, objid, objtype): |
|
337 |
return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype) |
|
1605 | 338 |
|
0 | 339 |
|
340 |
class AutomaticWebTest(WebTest): |
|
341 |
"""import this if you wan automatic tests to be ran""" |
|
342 |
## one each |
|
343 |
def test_one_each_config(self): |
|
344 |
self.auto_populate(1) |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
345 |
for rset in self.iter_automatic_rsets(limit=1): |
0 | 346 |
for testargs in self._test_everything_for(rset): |
347 |
yield testargs |
|
348 |
||
349 |
## ten each |
|
350 |
def test_ten_each_config(self): |
|
351 |
self.auto_populate(10) |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
352 |
for rset in self.iter_automatic_rsets(limit=10): |
0 | 353 |
for testargs in self._test_everything_for(rset): |
354 |
yield testargs |
|
1605 | 355 |
|
0 | 356 |
## startup views |
357 |
def test_startup_views(self): |
|
358 |
for vid in self.list_startup_views(): |
|
359 |
req = self.request() |
|
360 |
yield self.view, vid, None, req |
|
361 |
||
362 |
||
363 |
class RealDBTest(WebTest): |
|
364 |
||
365 |
def iter_individual_rsets(self, etypes=None, limit=None): |
|
366 |
etypes = etypes or unprotected_entities(self.schema, strict=True) |
|
367 |
for etype in etypes: |
|
368 |
rset = self.execute('Any X WHERE X is %s' % etype) |
|
369 |
for row in xrange(len(rset)): |
|
370 |
if limit and row > limit: |
|
371 |
break |
|
372 |
rset2 = rset.limit(limit=1, offset=row) |
|
373 |
yield rset2 |
|
374 |
||
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
|
375 |
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
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
pass |
1605 | 380 |
|
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
|
381 |
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
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
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
|
386 |
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
|
387 |
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
|
388 |
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
|
389 |
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
|
390 |
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
|
391 |
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
|
392 |
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
|
393 |
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
|
394 |
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
|
395 |
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
|
396 |
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
|
397 |
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
|
398 |
|
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 |
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
|
400 |
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
|
401 |
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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
print 'not tested', registry, vobject |