I finally did the following (using a storage with 'filename' and 'file'
attributes) without using webtest which seems to work :

>                 request.function='upload_photo'

        resp = upload_photo()

        photo = Storage()

        photo.filename = os.path.basename(filename)

        photo.file = open(filename)

        set_vars(self.env, _formname = "photo_create",

            file = photo,

            description = desc,

            _formkey = resp['form'].formkey,

            )

        resp = upload_photo()

        print db(db.photo.id>0).select()


Thadeus, do you want a patch to test_runner documentation for this sort of
things (in a Howto section ?) ?

-Mathieu

On Sun, May 16, 2010 at 09:32, Mathieu Clabaut <[email protected]>wrote:

> Thank you for the directions.
> I'll try something like that and keep you informed of my progress...
>
> -Mathieu
>
>
> On Sat, May 15, 2010 at 22:56, Thadeus Burgess <[email protected]>wrote:
>
>> I attempted to solve this by inserting data into the request
>> environment, including the form name, however I did not have any
>> luck..
>>
>> I would use WebTest for file upload testing instead, however you will
>> need to use a mixture of webtest and new_env(), this is because you
>> need to get the unique _formkey variable so that web2py can validate
>> properly.
>>
>> http://pythonpaste.org/webtest/#making-requests
>>
>> I am thinking of some sample pseudocode below.
>>
>> env = new_env(app='test_upload', controller='default')
>>
>> res = env['upload_a_file']()
>>
>> req = {}
>> for hidden in form.custom.end.elements():
>>  try:
>>    if hidden.has_key('_name') and hidden.has_key('_value'):
>>      req[hidden['_name']] = hidden['_value']
>>   except AttributeError:
>>      pass
>>
>> req['name'] = 'ABOUT.txt'
>>
>> Then continue and use the uploaded_files list in webtest and perform
>> an app.post(....) on the function.
>>
>> Now you can verify using another new_env that the record was inserted,
>> and you can manually check the existence of the file and its contents.
>>
>> --
>> Thadeus
>>
>>
>>
>>
>>
>> On Sat, May 15, 2010 at 12:58 PM, Mathieu Clabaut
>> <[email protected]> wrote:
>> > Hello,
>> >  I'd like to  test an upload function in my default controller (I use
>> > Thadeus testrunner from web2py_utils).
>> >  I wonder what object I shall put in my request.post_vars.file where
>> 'file'
>> > is the upload field ?
>> >  Apparently it should have a 'filename' attribute and a read method ?
>> >  Any tip on how to populate the request for such an upload form ?
>> > -Mathieu
>>
>
>

Reply via email to