author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 08 Sep 2009 15:30:49 +0200 | |
branch | 3.5 |
changeset 3111 | 7b405bb305ab |
parent 2668 | 979c7ccb4a86 |
child 2770 | 356e9d7c356d |
child 2773 | b2530e3e0afb |
child 3226 | 10f4f525044c |
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(): |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
47 |
if rschema.is_final(): |
0 | 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 |
||
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
175 |
def view(self, vid, rset=None, req=None, template='main-template', |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
176 |
**kwargs): |
0 | 177 |
"""This method tests the view `vid` on `rset` using `template` |
178 |
||
179 |
If no error occured while rendering the view, the HTML is analyzed |
|
180 |
and parsed. |
|
181 |
||
182 |
:returns: an instance of `cubicweb.devtools.htmlparser.PageInfo` |
|
183 |
encapsulation the generated HTML |
|
184 |
""" |
|
1142 | 185 |
req = req or rset and rset.req or self.request() |
0 | 186 |
req.form['vid'] = vid |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
187 |
kwargs['rset'] = rset |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
188 |
viewsreg = self.vreg['views'] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
189 |
view = viewsreg.select(vid, req, **kwargs) |
0 | 190 |
# set explicit test description |
191 |
if rset is not None: |
|
2070 | 192 |
self.set_description("testing %s, mod=%s (%s)" % ( |
193 |
vid, view.__module__, rset.printable_rql())) |
|
0 | 194 |
else: |
2070 | 195 |
self.set_description("testing %s, mod=%s (no rset)" % ( |
196 |
vid, view.__module__)) |
|
0 | 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 |
kwargs['view'] = view |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
201 |
templateview = viewsreg.select(template, req, **kwargs) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
202 |
viewfunc = lambda **k: viewsreg.main_template(req, template, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
203 |
**kwargs) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
204 |
kwargs.pop('rset') |
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 |
return self._test_view(viewfunc, view, template, kwargs) |
0 | 206 |
|
207 |
||
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
|
208 |
def _test_view(self, viewfunc, view, template='main-template', kwargs={}): |
0 | 209 |
"""this method does the actual call to the view |
210 |
||
211 |
If no error occured while rendering the view, the HTML is analyzed |
|
212 |
and parsed. |
|
213 |
||
214 |
:returns: an instance of `cubicweb.devtools.htmlparser.PageInfo` |
|
215 |
encapsulation the generated HTML |
|
216 |
""" |
|
217 |
output = None |
|
218 |
try: |
|
219 |
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
|
220 |
return self._check_html(output, view, template) |
0 | 221 |
except (SystemExit, KeyboardInterrupt): |
222 |
raise |
|
223 |
except: |
|
224 |
# hijack exception: generative tests stop when the exception |
|
225 |
# is not an AssertionError |
|
226 |
klass, exc, tcbk = sys.exc_info() |
|
227 |
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
|
228 |
msg = '[%s in %s] %s' % (klass, view.id, exc) |
0 | 229 |
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
|
230 |
msg = '[%s in %s] undisplayable exception' % (klass, view.id) |
0 | 231 |
if output is not None: |
232 |
position = getattr(exc, "position", (0,))[0] |
|
233 |
if position: |
|
234 |
# define filter |
|
235 |
output = output.splitlines() |
|
236 |
width = int(log(len(output), 10)) + 1 |
|
237 |
line_template = " %" + ("%i" % width) + "i: %s" |
|
238 |
# XXX no need to iterate the whole file except to get |
|
239 |
# the line number |
|
240 |
output = '\n'.join(line_template % (idx + 1, line) |
|
241 |
for idx, line in enumerate(output) |
|
242 |
if line_context_filter(idx+1, position)) |
|
1133 | 243 |
msg += '\nfor output:\n%s' % output |
0 | 244 |
raise AssertionError, msg, tcbk |
245 |
||
427
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
246 |
|
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
247 |
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
|
248 |
return unprotected_entities(self.schema, strict=True) |
1605 | 249 |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
250 |
def iter_automatic_rsets(self, limit=10): |
0 | 251 |
"""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
|
252 |
etypes = self.to_test_etypes() |
2219
bb5098e74b82
protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2070
diff
changeset
|
253 |
if not etypes: |
bb5098e74b82
protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2070
diff
changeset
|
254 |
return |
0 | 255 |
for etype in etypes: |
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
256 |
yield self.execute('Any X LIMIT %s WHERE X is %s' % (limit, etype)) |
0 | 257 |
etype1 = etypes.pop() |
1775
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
258 |
try: |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
259 |
etype2 = etypes.pop() |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
260 |
except KeyError: |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
261 |
etype2 = etype1 |
0 | 262 |
# test a mixed query (DISTINCT/GROUP to avoid getting duplicate |
263 |
# X which make muledit view failing for instance (html validation fails |
|
264 |
# because of some duplicate "id" attributes) |
|
265 |
yield self.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' % (etype1, etype2)) |
|
266 |
# test some application-specific queries if defined |
|
267 |
for rql in self.application_rql: |
|
268 |
yield self.execute(rql) |
|
269 |
||
1605 | 270 |
|
0 | 271 |
def list_views_for(self, rset): |
272 |
"""returns the list of views that can be applied on `rset`""" |
|
273 |
req = rset.req |
|
274 |
only_once_vids = ('primary', 'secondary', 'text') |
|
275 |
req.data['ex'] = ValueError("whatever") |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
276 |
viewsvreg = self.vreg['views'] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
277 |
for vid, views in viewsvreg.items(): |
0 | 278 |
if vid[0] == '_': |
279 |
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
|
280 |
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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
try: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
287 |
view = viewsvreg.select_best(views, req, rset=rset) |
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
288 |
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
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
# 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
|
293 |
# 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
|
294 |
except NoSelectableObject: |
0 | 295 |
continue |
296 |
||
297 |
def list_actions_for(self, rset): |
|
298 |
"""returns the list of actions that can be applied on `rset`""" |
|
299 |
req = rset.req |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
300 |
for action in self.vreg['actions'].possible_objects(req, rset=rset): |
0 | 301 |
yield action |
302 |
||
303 |
def list_boxes_for(self, rset): |
|
304 |
"""returns the list of boxes that can be applied on `rset`""" |
|
305 |
req = rset.req |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
306 |
for box in self.vreg['boxes'].possible_objects(req, rset=rset): |
0 | 307 |
yield box |
1605 | 308 |
|
0 | 309 |
def list_startup_views(self): |
310 |
"""returns the list of startup views""" |
|
311 |
req = self.request() |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
312 |
for view in self.vreg['views'].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
|
313 |
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
|
314 |
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
|
315 |
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
|
316 |
not_selected(self.vreg, view) |
1605 | 317 |
|
0 | 318 |
def _test_everything_for(self, rset): |
319 |
"""this method tries to find everything that can be tested |
|
320 |
for `rset` and yields a callable test (as needed in generative tests) |
|
321 |
""" |
|
322 |
propdefs = self.vreg['propertydefs'] |
|
323 |
# make all components visible |
|
324 |
for k, v in propdefs.items(): |
|
325 |
if k.endswith('visible') and not v['default']: |
|
326 |
propdefs[k]['default'] = True |
|
327 |
for view in self.list_views_for(rset): |
|
328 |
backup_rset = rset._prepare_copy(rset.rows, rset.description) |
|
329 |
yield InnerTest(self._testname(rset, view.id, 'view'), |
|
330 |
self.view, view.id, rset, |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
563
diff
changeset
|
331 |
rset.req.reset_headers(), 'main-template') |
0 | 332 |
# We have to do this because some views modify the |
333 |
# resultset's syntax tree |
|
334 |
rset = backup_rset |
|
335 |
for action in self.list_actions_for(rset): |
|
336 |
yield InnerTest(self._testname(rset, action.id, 'action'), action.url) |
|
337 |
for box in self.list_boxes_for(rset): |
|
1773 | 338 |
yield InnerTest(self._testname(rset, box.id, 'box'), box.render) |
0 | 339 |
|
340 |
@staticmethod |
|
341 |
def _testname(rset, objid, objtype): |
|
342 |
return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype) |
|
1605 | 343 |
|
0 | 344 |
|
345 |
class AutomaticWebTest(WebTest): |
|
346 |
"""import this if you wan automatic tests to be ran""" |
|
347 |
## one each |
|
348 |
def test_one_each_config(self): |
|
349 |
self.auto_populate(1) |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
350 |
for rset in self.iter_automatic_rsets(limit=1): |
0 | 351 |
for testargs in self._test_everything_for(rset): |
352 |
yield testargs |
|
353 |
||
354 |
## ten each |
|
355 |
def test_ten_each_config(self): |
|
356 |
self.auto_populate(10) |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
357 |
for rset in self.iter_automatic_rsets(limit=10): |
0 | 358 |
for testargs in self._test_everything_for(rset): |
359 |
yield testargs |
|
1605 | 360 |
|
0 | 361 |
## startup views |
362 |
def test_startup_views(self): |
|
363 |
for vid in self.list_startup_views(): |
|
364 |
req = self.request() |
|
365 |
yield self.view, vid, None, req |
|
366 |
||
367 |
||
368 |
class RealDBTest(WebTest): |
|
369 |
||
370 |
def iter_individual_rsets(self, etypes=None, limit=None): |
|
371 |
etypes = etypes or unprotected_entities(self.schema, strict=True) |
|
372 |
for etype in etypes: |
|
373 |
rset = self.execute('Any X WHERE X is %s' % etype) |
|
374 |
for row in xrange(len(rset)): |
|
375 |
if limit and row > limit: |
|
376 |
break |
|
377 |
rset2 = rset.limit(limit=1, offset=row) |
|
378 |
yield rset2 |
|
379 |
||
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
380 |
def not_selected(vreg, appobject): |
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 |
try: |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
382 |
vreg._selected[appobject.__class__] -= 1 |
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
|
383 |
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
|
384 |
pass |
1605 | 385 |
|
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
|
386 |
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
|
387 |
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
|
388 |
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
|
389 |
requestcls=testclass.requestcls) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
390 |
for reg in env.vreg.values(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
391 |
reg._selected = {} |
2668
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
392 |
try: |
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
393 |
orig_select_best = reg.__class__.__orig_select_best |
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
394 |
except: |
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
395 |
orig_select_best = reg.__class__.select_best |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
396 |
def instr_select_best(self, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
397 |
selected = orig_select_best(self, *args, **kwargs) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
398 |
try: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
399 |
self._selected[selected.__class__] += 1 |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
400 |
except KeyError: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
401 |
self._selected[selected.__class__] = 1 |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
402 |
except AttributeError: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
403 |
pass # occurs on reg used to restore database |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
404 |
return selected |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
405 |
reg.__class__.select_best = instr_select_best |
2668
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
406 |
reg.__class__.__orig_select_best = orig_select_best |
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
|
407 |
|
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
408 |
def print_untested_objects(testclass, skipregs=('hooks', 'etypes')): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
409 |
for regname, reg in testclass._env.vreg.iteritems(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
410 |
if regname in skipregs: |
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
|
411 |
continue |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
412 |
for appobjects in reg.itervalues(): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
413 |
for appobject in appobjects: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
414 |
if not reg._selected.get(appobject): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
415 |
print 'not tested', regname, appobject |