Ticket #1762: TurboGears-1.0-WebTest.patch

File TurboGears-1.0-WebTest.patch, 2.6 kB (added by lmacken, 8 months ago)
  • testutil.py

    old new  
    2323from turbogears.identity import current_provider 
    2424from turbogears.util import get_model 
    2525 
     26from webtest import TestApp 
     27 
    2628cwd = os.getcwd() 
    2729 
    2830# For clean tests, remove all compiled Kid templates 
     
    197199                item.dropTable(ifExists=True) 
    198200 
    199201 
     202class TGWebTest(unittest.TestCase): 
     203    """ A WebTest enabled unit testing class. 
     204 
     205    This allows testers to subclass us and use self.app to make WebTest calls. 
     206    """ 
     207    def setUp(self): 
     208        from cherrypy._cpwsgi import wsgiApp 
     209        start_cp() 
     210        startup.startTurboGears() 
     211        self.app = TestApp(wsgiApp) 
     212 
     213    def tearDown(self): 
     214        startup.stopTurboGears() 
     215        del self.app 
     216 
     217    def login_user(self, user): 
     218        """ Log a specified user object into the system """ 
     219        self.app.post(config.get('identity.failure_url'), { 
     220                'login'     : 'Login', 
     221                'user_name' : user.user_name, 
     222                'password'  : user.password 
     223        }) 
     224 
     225 
     226class DBWebTest(TGWebTest, DBTest): 
     227    """ A database driven WebTest enabled unit testing class. 
     228 
     229    This class will automatically setup and tear down your database, along 
     230    with TurboGears and WebTest before and after each unit test. 
     231    """ 
     232    def setUp(self): 
     233        DBTest.setUp(self) 
     234        TGWebTest.setUp(self) 
     235 
     236    def tearDown(self): 
     237        TGWebTest.tearDown(self) 
     238        DBTest.tearDown(self) 
     239 
     240 
    200241def reset_cp(): 
    201242    cherrypy.root = None 
    202243 
     
    305346 
    306347__all__ = ["call", "create_request", "createRequest", "DBTest", 
    307348    "attach_identity", "set_identity_user", 
    308     "capture_log", "print_log", "get_log", "sqlalchemy_cleanup"] 
     349    "capture_log", "print_log", "get_log", "sqlalchemy_cleanup", 
     350    "TGWebTest", "DBWebTest"] 
  • view/base.py

    old new  
    126126        format = config.get("%s.outputformat" % enginename, "html") 
    127127    args, kw = adapt_call(engine.render, args= [], 
    128128                kw = dict(info=info, format=format, fragment=fragment, template=template, mapping=mapping), start=1) 
     129 
     130    if hasattr(cherrypy.request, 'wsgi_environ'): 
     131        if 'paste.testing' in cherrypy.request.wsgi_environ: 
     132            cherrypy.request.wsgi_environ['paste.testing_variables']['namespace'] = info 
     133 
    129134    return engine.render(**kw) 
    130135 
    131136def transform(info, template):