Ticket #1762: tgtest.patch

File tgtest.patch, 11.0 kB (added by kskuhlman, 2 years ago)

Almost complete..

  • turbogears/identity/tests/test_identity.py

    old new  
    155155 
    156156 
    157157 
    158 class TestIdentity(testutil.TGWebTest): 
     158class TestIdentity(testutil.TGTest): 
    159159 
    160160    def setUp(self): 
    161161        # identity requires visit and a failure_url 
     
    169169        self._original_config = original_config 
    170170        config.configure_loggers(test_config) 
    171171        config.update(test_config['global']) 
    172         testutil.mount(IdentityRoot()) 
    173         testutil.TGWebTest.setUp(self) 
     172        self.root = IdentityRoot 
     173        testutil.TGTest.setUp(self) 
    174174        self.init_model() 
    175175 
    176176    def tearDown(self): 
    177         testutil.TGWebTest.tearDown(self) 
     177        testutil.TGTest.tearDown(self) 
    178178        config.update(self._original_config) 
    179179 
    180180    def init_model(self): 
  • turbogears/tests/test_controllers.py

    old new  
    242242        return "redirected OK" 
    243243 
    244244 
    245 class TestRoot(testutil.TGWebTest): 
     245class TestRoot(testutil.TGTest): 
    246246 
    247247    def setUp(self): 
    248248        testutil.mount(MyRoot(), '/') 
    249249        testutil.mount(SubApp(), '/subthing') 
     250        self.app = testutil.make_app() 
    250251        super(TestRoot, self).setUp() 
    251252 
    252253 
     
    565566        assert 'handling_key' in response 
    566567 
    567568 
    568 class TestURLs(testutil.TGWebTest): 
     569class TestURLs(testutil.TGTest): 
    569570 
    570571    def setUp(self): 
    571572        testutil.mount(MyRoot(), '/') 
    572573        testutil.mount(SubApp(), '/subthing') 
    573574        testutil.mount(SubApp(), '/subthing/subsubthing') 
     575        self.app = testutil.make_app() 
    574576        super(TestURLs, self).setUp() 
    575577 
    576578    def tearDown(self): 
  • turbogears/tests/test_view.py

    old new  
    33import cherrypy 
    44import unittest 
    55 
    6 class TestView(testutil.TGWebTest): 
     6class TestView(unittest.TestCase): 
    77 
     8    def setUp(self): 
     9        # The server needs to be started so that the template engines get loaded. 
     10        testutil.start_server() 
     11 
    812    def test_cycle(self): 
    913        oe = view.base.cycle(('odd','even')) 
    1014        assert str(oe) == str(None) 
  • turbogears/tests/test_paginate.py

    old new  
    186186    return result 
    187187 
    188188 
    189 class TestSpy(testutil.TGWebTest): 
     189class TestPagination(testutil.TGTest): 
     190    """Base class for all Paginate TestCases""" 
     191 
     192    def setUp(self): 
     193        self.root = self.MyRoot 
     194        super(TestPagination, self).setUp() 
     195 
     196    def request(self, url, status=200): 
     197        response = self.app.get(url, status=status) 
     198        self.body = response.body 
     199        if "fail: " in self.body: 
     200            print self.body 
     201            assert False, "Spy alert! Check body output for details..." 
     202 
     203 
     204class TestSpy(TestPagination): 
    190205    """Never trust a spy""" 
    191206 
    192207    class MyRoot(RootController): 
     
    219234            spy = Spy(foobar=10) 
    220235            return dict(data=data, spy=spy) 
    221236 
    222  
    223     def setUp(self): 
    224         super(TestSpy, self).setUp(self.MyRoot) 
    225  
    226  
    227237    def test_spy(self): 
    228238        response = self.app.get('/spy') 
    229239        Spy.assert_ok(response.body, 'current_page', 1) 
     
    251261        Spy.assert_ok(response.body, 'var_name', "'data'", raw=True) 
    252262 
    253263 
    254 class TestPagination(testutil.TGWebTest): 
    255     """Base class for all Paginate TestCases""" 
    256  
    257     def setUp(self): 
    258         super(TestPagination, self).setUp(self.MyRoot) 
    259  
    260     def request(self, url, status=200): 
    261         response = self.app.get(url, status=status) 
    262         self.body = response.body 
    263         if "fail: " in self.body: 
    264             print self.body 
    265             assert False, "Spy alert! Check body output for details..." 
    266  
    267  
    268264class TestBasicPagination(TestPagination): 
    269265 
    270266    class MyRoot(RootController): 
  • turbogears/qstemplates/quickstart/+package+/tests/test_model.py_tmpl

    old new  
    1717    from ${package}.model import YourModelClass, User, ... 
    1818""" 
    1919 
    20 from turbogears import testutil, database 
     20from turbogears.testutil import DBTest 
    2121 
    2222# from ${package}.model import YourModelClass, User 
    2323 
    24 # class TestUser(testutil.DBTest): 
    25 #     def get_model(self): 
    26 #         return User 
     24# class TestUser(DBTest): 
     25#     model = User 
    2726# 
    2827#     def test_creation(self): 
    2928#         """Object creation should set the name.""" 
  • turbogears/qstemplates/quickstart/+package+/tests/test_controllers.py_tmpl

    old new  
    44from ${package}.controllers import Root 
    55import cherrypy 
    66 
    7 testutil.mount(root = Root()) 
     7class TestPages(testutil.TGTest): 
    88 
    9 class TestPages(unittest.TestCase): 
     9    root = Root 
    1010 
    11     def setUp(self): 
    12         testutil.start_server() 
    13  
    14     def tearDown(self): 
    15         """Tests for apps using identity need to stop CP/TG after each test to 
    16         stop the VisitManager thread. 
    17         See http://trac.turbogears.org/turbogears/ticket/1217 for details. 
    18         """ 
    19         testutil.stop_server() 
    20  
    2111    def test_method(self): 
    2212        "the index method should return a string called now" 
    2313        import types 
    24         response = testutil.go("/") 
     14        response = self.app.get("/") 
    2515        assert type(response.raw["now"]) == types.StringType 
    2616 
    2717    def test_indextitle(self): 
    2818        "The indexpage should have the right title" 
    29         response = testutil.go("/") 
     19        response = self.app.get("/") 
    3020        assert "<title>welcome to turbogears</title>" in response.body.lower() 
    3121 
    3222#if $identity != "none" 
    3323    def test_logintitle(self): 
    3424        "login page should have the right title" 
    35         response = testutil.go("/login") 
    36         assert "<title>login</title>" in response.body.lower() 
     25        response = self.app.get("/login") 
     26        assert "<title>Login</title>" in response, response 
    3727#end if 
  • turbogears/qstemplates/quickstart/setup.py_tmpl

    old new  
    2727 
    2828    install_requires=[ 
    2929        "TurboGears >= ${turbogearsversion}", 
     30        "WebTest", 
    3031#if $sqlobject == 'True' 
    3132        "$sqlobjectversion" 
    3233#elif $sqlalchemy == 'True' 
  • turbogears/testutil.py

    old new  
    157157    return TestApp(wsgiapp) 
    158158 
    159159 
    160 class TGWebTest(unittest.TestCase): 
     160class TGTest(unittest.TestCase): 
    161161    """A WebTest enabled unit testing class. 
    162162 
    163     This allows testers to subclass us and use self.app to make WebTest calls. 
     163    To use, subclass & set root to your controller object, or set app to a  
     164    webtest.TestApp instance.   
    164165 
     166    In your tests, use self.app to make WebTest calls. 
    165167    """ 
    166168 
    167     def setUp(self, controller=None): 
    168         """Set up the WebTest by starting the server." 
     169    root = None 
     170    app = None 
    169171 
     172    def setUp(self): 
     173        """Set up the WebTest by starting the server. 
     174 
    170175        You should override this and make sure you have properly 
    171176        mounted a root for your server before calling super, 
    172177        or simply pass a root controller to super. 
    173178        Otherwise the Cherrypy filters for TurboGears will not be used. 
    174  
    175179        """ 
    176         self.app = make_app(controller) 
     180        assert self.root or self.app, "Either self.root or self.app must be set" 
     181        if not self.app:  
     182            self.app = make_app(self.root)  
    177183        start_server() 
    178184 
    179185    def tearDown(self): 
    180186        """Tear down the WebTest by stopping the server.""" 
    181187        stop_server(tg_only = True) 
    182         del self.app 
    183188 
    184189    def login_user(self, user): 
    185190        """Log a specified user object into the system.""" 
     
    281286    return output, response 
    282287 
    283288 
    284 class DBTest(unittest.TestCase): 
     289class AbstractDBTest(unittest.TestCase): 
     290    """A database enabled unit testing class. 
    285291 
     292    Creates and destroys your database before and after each unit test.  You must set the  
     293    model attribute in order for this class to function correctly. 
     294    """ 
    286295    model = None 
    287296 
     297    def setUp(self): 
     298        raise NotImplementedError() 
     299 
     300    def tearDown(self): 
     301        raise NotImplementedError() 
     302 
     303class DBTestSO(AbstractDBTest): 
    288304    def _get_soClasses(self): 
    289305        try: 
    290306            return [self.model.__dict__[x] for x in self.model.soClasses] 
     
    295311        if not self.model: 
    296312            self.model = get_model() 
    297313            if not self.model: 
    298                 raise "Unable to run database tests without a model" 
    299  
     314                raise Exception("Unable to run database tests without a model") 
    300315        for item in self._get_soClasses(): 
    301316            if isinstance(item, types.TypeType) and issubclass(item, 
    302317                sqlobject.SQLObject) and item != sqlobject.SQLObject \ 
     
    311326                and item != InheritableSQLObject: 
    312327                item.dropTable(ifExists=True, cascade=True) 
    313328 
     329class DBTestSA(AbstractDBTest): 
     330    def setUp(self): 
     331        database.get_engine() 
     332        database.metadata.create_all() 
     333 
     334    def tearDown(self): 
     335        database.metadata.drop_all() 
     336 
     337 
     338#Determine which class to use for "DBTest".  Setup & teardown should behave  
     339#simularly regardless of which ORM you choose. 
     340if config.get("sqlobject.dburi"): 
     341    DBTest = DBTestSO 
     342elif config.get("sqlalchemy.dburi"): 
     343    DBTest = DBTestSA 
     344else: 
     345    raise Exception("Unable to find sqlalchemy or sqlobject dburi") 
     346 
     347 
    314348def unmount(): 
    315349    """Remove an application from the object traversal tree.""" 
    316350    # There's no clean way to remove a subtree under CP2, so the only use case 
  • turbogears/command/quickstart.py

    old new  
    240240            sqlalchemyversion = str(get_requirement('sqlalchemy')) 
    241241            cmd_args.append("sqlalchemyversion=%s" % sqlalchemyversion) 
    242242        if self.elixir: 
    243             elixirversion = str(get_requirement('future', 'elixir')) 
     243            elixirversion = str(get_requirement('sqlalchemy')) 
    244244            cmd_args.append("elixirversion=%s" % elixirversion) 
    245245 
    246246        command.run(cmd_args)