devtools/testlib.py
changeset 2058 7ef12c03447c
parent 1977 606923dff11b
child 2126 a25859917ccc
equal deleted inserted replaced
2057:0a0cbccafcb5 2058:7ef12c03447c
   170             return None
   170             return None
   171         validator = validatorclass()
   171         validator = validatorclass()
   172         return validator.parse_string(output.strip())
   172         return validator.parse_string(output.strip())
   173 
   173 
   174 
   174 
   175     def view(self, vid, rset, req=None, template='main-template', **kwargs):
   175     def view(self, vid, rset=None, req=None, template='main-template',
       
   176              **kwargs):
   176         """This method tests the view `vid` on `rset` using `template`
   177         """This method tests the view `vid` on `rset` using `template`
   177 
   178 
   178         If no error occured while rendering the view, the HTML is analyzed
   179         If no error occured while rendering the view, the HTML is analyzed
   179         and parsed.
   180         and parsed.
   180 
   181 
   181         :returns: an instance of `cubicweb.devtools.htmlparser.PageInfo`
   182         :returns: an instance of `cubicweb.devtools.htmlparser.PageInfo`
   182                   encapsulation the generated HTML
   183                   encapsulation the generated HTML
   183         """
   184         """
   184         req = req or rset and rset.req or self.request()
   185         req = req or rset and rset.req or self.request()
   185         # print "testing ", vid,
       
   186         # if rset:
       
   187         #     print rset, len(rset), id(rset)
       
   188         # else:
       
   189         #     print
       
   190         req.form['vid'] = vid
   186         req.form['vid'] = vid
   191         view = self.vreg.select_view(vid, req, rset, **kwargs)
   187         kwargs['rset'] = rset
       
   188         view = self.vreg.select('views', vid, req, **kwargs)
   192         # set explicit test description
   189         # set explicit test description
   193         if rset is not None:
   190         if rset is not None:
   194             self.set_description("testing %s, mod=%s (%s)" % (vid, view.__module__, rset.printable_rql()))
   191             self.set_description("testing %s, mod=%s (%s)" % (
       
   192                 vid, view.__module__, rset.printable_rql()))
   195         else:
   193         else:
   196             self.set_description("testing %s, mod=%s (no rset)" % (vid, view.__module__))
   194             self.set_description("testing %s, mod=%s (no rset)" % (
       
   195                 vid, view.__module__))
   197         if template is None: # raw view testing, no template
   196         if template is None: # raw view testing, no template
   198             viewfunc = view.render
   197             viewfunc = view.render
   199         else:
   198         else:
   200             templateview = self.vreg.select_view(template, req, rset, view=view, **kwargs)
       
   201             kwargs['view'] = view
   199             kwargs['view'] = view
   202             viewfunc = lambda **k: self.vreg.main_template(req, template, **kwargs)
   200             templateview = self.vreg.select('views', template, req, **kwargs)
       
   201             viewfunc = lambda **k: self.vreg.main_template(req, template,
       
   202                                                            **kwargs)
       
   203         kwargs.pop('rset')
   203         return self._test_view(viewfunc, view, template, kwargs)
   204         return self._test_view(viewfunc, view, template, kwargs)
   204 
   205 
   205 
   206 
   206     def _test_view(self, viewfunc, view, template='main-template', kwargs={}):
   207     def _test_view(self, viewfunc, view, template='main-template', kwargs={}):
   207         """this method does the actual call to the view
   208         """this method does the actual call to the view
   277             views = [view for view in views
   278             views = [view for view in views
   278                      if view.category != 'startupview'
   279                      if view.category != 'startupview'
   279                      and not issubclass(view, NotificationView)]
   280                      and not issubclass(view, NotificationView)]
   280             if views:
   281             if views:
   281                 try:
   282                 try:
   282                     view = self.vreg.select(views, req, rset)
   283                     view = self.vreg.select_best(views, req, rset=rset)
   283                     if view.linkable():
   284                     if view.linkable():
   284                         yield view
   285                         yield view
   285                     else:
   286                     else:
   286                         not_selected(self.vreg, view)
   287                         not_selected(self.vreg, view)
   287                     # else the view is expected to be used as subview and should
   288                     # else the view is expected to be used as subview and should
   290                     continue
   291                     continue
   291 
   292 
   292     def list_actions_for(self, rset):
   293     def list_actions_for(self, rset):
   293         """returns the list of actions that can be applied on `rset`"""
   294         """returns the list of actions that can be applied on `rset`"""
   294         req = rset.req
   295         req = rset.req
   295         for action in self.vreg.possible_objects('actions', req, rset):
   296         for action in self.vreg.possible_objects('actions', req, rset=rset):
   296             yield action
   297             yield action
   297 
   298 
   298     def list_boxes_for(self, rset):
   299     def list_boxes_for(self, rset):
   299         """returns the list of boxes that can be applied on `rset`"""
   300         """returns the list of boxes that can be applied on `rset`"""
   300         req = rset.req
   301         req = rset.req
   301         for box in self.vreg.possible_objects('boxes', req, rset):
   302         for box in self.vreg.possible_objects('boxes', req, rset=rset):
   302             yield box
   303             yield box
   303 
   304 
   304     def list_startup_views(self):
   305     def list_startup_views(self):
   305         """returns the list of startup views"""
   306         """returns the list of startup views"""
   306         req = self.request()
   307         req = self.request()
   382     from cubicweb.devtools.apptest import TestEnvironment
   383     from cubicweb.devtools.apptest import TestEnvironment
   383     env = testclass._env = TestEnvironment('data', configcls=testclass.configcls,
   384     env = testclass._env = TestEnvironment('data', configcls=testclass.configcls,
   384                                            requestcls=testclass.requestcls)
   385                                            requestcls=testclass.requestcls)
   385     vreg = env.vreg
   386     vreg = env.vreg
   386     vreg._selected = {}
   387     vreg._selected = {}
   387     orig_select = vreg.__class__.select
   388     orig_select_best = vreg.__class__.select_best
   388     def instr_select(self, *args, **kwargs):
   389     def instr_select_best(self, *args, **kwargs):
   389         selected = orig_select(self, *args, **kwargs)
   390         selected = orig_select(self, *args, **kwargs)
   390         try:
   391         try:
   391             self._selected[selected.__class__] += 1
   392             self._selected[selected.__class__] += 1
   392         except KeyError:
   393         except KeyError:
   393             self._selected[selected.__class__] = 1
   394             self._selected[selected.__class__] = 1
   394         except AttributeError:
   395         except AttributeError:
   395             pass # occurs on vreg used to restore database
   396             pass # occurs on vreg used to restore database
   396         return selected
   397         return selected
   397     vreg.__class__.select = instr_select
   398     vreg.__class__.select_best = instr_select_best
   398 
   399 
   399 def print_untested_objects(testclass, skipregs=('hooks', 'etypes')):
   400 def print_untested_objects(testclass, skipregs=('hooks', 'etypes')):
   400     vreg = testclass._env.vreg
   401     vreg = testclass._env.vreg
   401     for registry, vobjectsdict in vreg.items():
   402     for registry, vobjectsdict in vreg.items():
   402         if registry in skipregs:
   403         if registry in skipregs: