devtools/testlib.py
branchtls-sprint
changeset 1605 b239744627c7
parent 1152 c99ef2a2535c
child 1773 789ee507a8e0
equal deleted inserted replaced
1604:fd943737d630 1605:b239744627c7
   108         'image/png': None,
   108         'image/png': None,
   109         }
   109         }
   110     # maps vid : validator name (override content_type_validators)
   110     # maps vid : validator name (override content_type_validators)
   111     vid_validators = dict((vid, VALMAP[valkey])
   111     vid_validators = dict((vid, VALMAP[valkey])
   112                           for vid, valkey in VIEW_VALIDATORS.iteritems())
   112                           for vid, valkey in VIEW_VALIDATORS.iteritems())
   113     
   113 
   114     no_auto_populate = ()
   114     no_auto_populate = ()
   115     ignored_relations = ()    
   115     ignored_relations = ()
   116         
   116 
   117     def custom_populate(self, how_many, cursor):
   117     def custom_populate(self, how_many, cursor):
   118         pass
   118         pass
   119         
   119 
   120     def post_populate(self, cursor):
   120     def post_populate(self, cursor):
   121         pass
   121         pass
   122     
   122 
   123     @nocoverage
   123     @nocoverage
   124     def auto_populate(self, how_many):
   124     def auto_populate(self, how_many):
   125         """this method populates the database with `how_many` entities
   125         """this method populates the database with `how_many` entities
   126         of each possible type. It also inserts random relations between them
   126         of each possible type. It also inserts random relations between them
   127         """
   127         """
   183         req = req or rset and rset.req or self.request()
   183         req = req or rset and rset.req or self.request()
   184         # print "testing ", vid,
   184         # print "testing ", vid,
   185         # if rset:
   185         # if rset:
   186         #     print rset, len(rset), id(rset)
   186         #     print rset, len(rset), id(rset)
   187         # else:
   187         # else:
   188         #     print 
   188         #     print
   189         req.form['vid'] = vid
   189         req.form['vid'] = vid
   190         view = self.vreg.select_view(vid, req, rset, **kwargs)
   190         view = self.vreg.select_view(vid, req, rset, **kwargs)
   191         # set explicit test description
   191         # set explicit test description
   192         if rset is not None:
   192         if rset is not None:
   193             self.set_description("testing %s, mod=%s (%s)" % (vid, view.__module__, rset.printable_rql()))
   193             self.set_description("testing %s, mod=%s (%s)" % (vid, view.__module__, rset.printable_rql()))
   241             raise AssertionError, msg, tcbk
   241             raise AssertionError, msg, tcbk
   242 
   242 
   243 
   243 
   244     def to_test_etypes(self):
   244     def to_test_etypes(self):
   245         return unprotected_entities(self.schema, strict=True)
   245         return unprotected_entities(self.schema, strict=True)
   246     
   246 
   247     def iter_automatic_rsets(self, limit=10):
   247     def iter_automatic_rsets(self, limit=10):
   248         """generates basic resultsets for each entity type"""
   248         """generates basic resultsets for each entity type"""
   249         etypes = self.to_test_etypes()
   249         etypes = self.to_test_etypes()
   250         for etype in etypes:
   250         for etype in etypes:
   251             yield self.execute('Any X LIMIT %s WHERE X is %s' % (limit, etype))
   251             yield self.execute('Any X LIMIT %s WHERE X is %s' % (limit, etype))
   258         yield self.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' % (etype1, etype2))
   258         yield self.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' % (etype1, etype2))
   259         # test some application-specific queries if defined
   259         # test some application-specific queries if defined
   260         for rql in self.application_rql:
   260         for rql in self.application_rql:
   261             yield self.execute(rql)
   261             yield self.execute(rql)
   262 
   262 
   263                 
   263 
   264     def list_views_for(self, rset):
   264     def list_views_for(self, rset):
   265         """returns the list of views that can be applied on `rset`"""
   265         """returns the list of views that can be applied on `rset`"""
   266         req = rset.req
   266         req = rset.req
   267         only_once_vids = ('primary', 'secondary', 'text')
   267         only_once_vids = ('primary', 'secondary', 'text')
   268         req.data['ex'] = ValueError("whatever")
   268         req.data['ex'] = ValueError("whatever")
   295     def list_boxes_for(self, rset):
   295     def list_boxes_for(self, rset):
   296         """returns the list of boxes that can be applied on `rset`"""
   296         """returns the list of boxes that can be applied on `rset`"""
   297         req = rset.req
   297         req = rset.req
   298         for box in self.vreg.possible_objects('boxes', req, rset):
   298         for box in self.vreg.possible_objects('boxes', req, rset):
   299             yield box
   299             yield box
   300             
   300 
   301     def list_startup_views(self):
   301     def list_startup_views(self):
   302         """returns the list of startup views"""
   302         """returns the list of startup views"""
   303         req = self.request()
   303         req = self.request()
   304         for view in self.vreg.possible_views(req, None):
   304         for view in self.vreg.possible_views(req, None):
   305             if view.category == 'startupview':
   305             if view.category == 'startupview':
   306                 yield view.id
   306                 yield view.id
   307             else:
   307             else:
   308                 not_selected(self.vreg, view)
   308                 not_selected(self.vreg, view)
   309                 
   309 
   310     def _test_everything_for(self, rset):
   310     def _test_everything_for(self, rset):
   311         """this method tries to find everything that can be tested
   311         """this method tries to find everything that can be tested
   312         for `rset` and yields a callable test (as needed in generative tests)
   312         for `rset` and yields a callable test (as needed in generative tests)
   313         """
   313         """
   314         propdefs = self.vreg['propertydefs']
   314         propdefs = self.vreg['propertydefs']
   330             yield InnerTest(self._testname(rset, box.id, 'box'), box.dispatch)
   330             yield InnerTest(self._testname(rset, box.id, 'box'), box.dispatch)
   331 
   331 
   332     @staticmethod
   332     @staticmethod
   333     def _testname(rset, objid, objtype):
   333     def _testname(rset, objid, objtype):
   334         return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype)
   334         return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype)
   335             
   335 
   336 
   336 
   337 class AutomaticWebTest(WebTest):
   337 class AutomaticWebTest(WebTest):
   338     """import this if you wan automatic tests to be ran"""
   338     """import this if you wan automatic tests to be ran"""
   339     ## one each
   339     ## one each
   340     def test_one_each_config(self):
   340     def test_one_each_config(self):
   347     def test_ten_each_config(self):
   347     def test_ten_each_config(self):
   348         self.auto_populate(10)
   348         self.auto_populate(10)
   349         for rset in self.iter_automatic_rsets(limit=10):
   349         for rset in self.iter_automatic_rsets(limit=10):
   350             for testargs in self._test_everything_for(rset):
   350             for testargs in self._test_everything_for(rset):
   351                 yield testargs
   351                 yield testargs
   352                 
   352 
   353     ## startup views
   353     ## startup views
   354     def test_startup_views(self):
   354     def test_startup_views(self):
   355         for vid in self.list_startup_views():
   355         for vid in self.list_startup_views():
   356             req = self.request()
   356             req = self.request()
   357             yield self.view, vid, None, req
   357             yield self.view, vid, None, req
   372 def not_selected(vreg, vobject):
   372 def not_selected(vreg, vobject):
   373     try:
   373     try:
   374         vreg._selected[vobject.__class__] -= 1
   374         vreg._selected[vobject.__class__] -= 1
   375     except (KeyError, AttributeError):
   375     except (KeyError, AttributeError):
   376         pass
   376         pass
   377         
   377 
   378 def vreg_instrumentize(testclass):
   378 def vreg_instrumentize(testclass):
   379     from cubicweb.devtools.apptest import TestEnvironment
   379     from cubicweb.devtools.apptest import TestEnvironment
   380     env = testclass._env = TestEnvironment('data', configcls=testclass.configcls,
   380     env = testclass._env = TestEnvironment('data', configcls=testclass.configcls,
   381                                            requestcls=testclass.requestcls)
   381                                            requestcls=testclass.requestcls)
   382     vreg = env.vreg
   382     vreg = env.vreg