Friday, March 11, 2011

file upload unit testing in pylons

controller code:
--------------------------------------------------------------------------
import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from samplebundle.lib.base import BaseController, render

log = logging.getLogger(__name__)

class WebflowcontrolsController(BaseController):
    def upload(self):
        myfile = request.POST['myfile']
        
        return 'Successfully uploaded: %s, size: %i' % \
        (myfile.filename, len(myfile.value))


unit testing code:
--------------------------------------------------------------------------
from samplebundle.tests import *

class TestWebflowcontrolsController(TestController):
    def test_upload(self):
        my_upload_files = [("myfile","myfilename","myfilecontent")]  # field, filename, content
        response = self.app.post(url(controller='webflowcontrols', 
                                    action='upload'),
                                 upload_files=my_upload_files)
        assert 'size: 13' in response

No comments:

Post a Comment