If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!


  • 0
    matclab  14 years ago
    I finally use the following helper :
    
    def form_postvars(tablename, fields, request, action="create", 
            record_id=None):
        """
        Creates the appropriate request vars for forms
        """
    
        vars = {}
        for field_name in fields:
            vars[field_name] = fields[field_name]
        if action == "create":
            vars["_formname"] = tablename + "_" + action
        elif action == "update":
            vars["_formname"] = tablename + "_" + str(record_id)
            vars["id"] = record_id
        elif action == "delete":
            vars["_formname"] = tablename + "_" + str(record_id)
            vars["id"] = record_id
            vars["delete_this_record"] = True
        elif action:
            vars["_formname"] = action
    
        request['vars'].update(vars)
        request['post_vars'].update(vars)
    
    
    and the following test class: (note that without pickling at init and unpickling at setup, the session and request was not cleaned up before each test.)
    
    #!/usr/bin/python
    
    #python web2py.py -S postcard -M -R testRunner.py
    
    import unittest
    from gluon.globals import Request, Session, Storage, Response
    from gluon.contrib.test_helpers import form_postvars
    from gluon.html import BEAUTIFY
    import cPickle as pickle
    
    
    
    class DefaultController(unittest.TestCase):
        def __init__(self, p):
            global auth, session, request
            unittest.TestCase.__init__(self, p)
            self.session = pickle.dumps(session)
            request.application = 'appname'
            request.controller = 'default'
            self.request = pickle.dumps(request)
    
    
        def setUp(self):
            global response, session, request, auth
            session = pickle.loads(self.session)
            request = pickle.loads(self.request)
            auth = Auth(globals(), db)
            auth.define_tables()
       def _testRedirect(self, callable, url="/index"):
            try:
                resp = callable()
                self.fail("%s should raise an exception\n%s" % (
                    callable.__name__,
                    resp.errors))
            except HTTP, e:
                self.assertTrue(e.headers['Location'] == url,
                        "Wrong redirection url for unlogged user on %s() : %s" %
                        (callable.__name__, e.headers['Location']))
            else:
                self.fail("%s should raise an HTTP exception\n%s" % (
                    callable.__name__,
                    e))
    
        def emptyUserDB(self):
            db(db.auth_user.id>0).delete()
            db.commit()
    
        def testRegisterSuccess(self):
            self.emptyUserDB()
            #Register
            request.function='register'
            resp = auth.register()
            form_postvars("auth_user", {
                "email": "essai@gmail.com",
                "first_name": "e_first",
                "last_name": "e_lats",
                "password" : "blob",
                "password_two": "blob",
                "_formkey": resp.formkey,
                },
                request, action="register",
                record_id=None, )     
            self._testRedirect(auth.register)
            self.assertTrue(auth.is_logged_in())
            self.assertEquals(auth.user.first_name, 'e_first')
    
    
    

Commented on:

A unit testing solution

Hosting graciously provided by:
Python Anywhere