Creating TestUnits for file upload form

28 views
Skip to first unread message

Bojan MIletic

unread,
Jun 19, 2012, 8:29:19 AM6/19/12
to pylons...@googlegroups.com
Hello

I'm relatively new to Pyramid, so I'm struggling with writing tests for my picture upload form. 
How can I add picture to the dummy request?

Here is the function I would like to test.

@view_config(route_name='profile_pic')
def profilePictureUpload(request):
if 'form.submitted' in request.params:
log.info("recieved picture upload request")
form = Form(request, schema=PictureUpload)

if request.method == 'POST' and form.validate():
#TODO replace hardcoded value with value from config
 upload_directory = 'PATH'

upload = request.POST.get('profile')
image_type = imghdr.what(upload.filename, upload.value)
log.info(image_type)
saved_file = str(upload_directory) + str(upload.filename)

perm_file = open(saved_file, 'wb')
shutil.copyfileobj(upload.file, perm_file)
upload.file.close()
perm_file.close()
else:
print form.errors
redirect_url = route_url('profile', request)
return HTTPFound(location=redirect_url)

Reply all
Reply to author
Forward
0 new messages