librelist archives

« back to archive

SlectFields in unit tests

SlectFields in unit tests

From:
Max Countryman
Date:
2011-12-03 @ 18:47
Hi all,

I've got a question about selecting SelectField options in unit tests? 
While typical input data is easily set via the data dict, I don't see any 
example for handling other things of input fields (e.g. SelectField). Can 
someone point me in the right direction?

As a quick example assume I have a form where there's an input field for 
address and a SelectField for state. I want to select the various options 
in this SelectField via a POST request executed in my unit tests, so 
something like: app.post('/some_view', data=data=dict(address='123 Main 
St', city=('NYC', 'NYC'))) # this doesn't quite do it though

Thanks!

Re: [flask] SlectFields in unit tests

From:
Simon Sapin
Date:
2011-12-03 @ 19:41
Le 03/12/2011 19:47, Max Countryman a écrit :
> Hi all,
>
> I've got a question about selecting SelectField options in unit tests? 
While typical input data is easily set via the data dict, I don't see any 
example for handling other things of input fields (e.g. SelectField). Can 
someone point me in the right direction?
>
> As a quick example assume I have a form where there's an input field for
address and a SelectField for state. I want to select the various options 
in this SelectField via a POST request executed in my unit tests, so 
something like: app.post('/some_view', data=data=dict(address='123 Main 
St', city=('NYC', 'NYC'))) # this doesn't quite do it though
>
> Thanks!

Hi,

It does not matter to the test client what the HTML looks like. For the 
form data in client.post(), only pass values the same way you get it in 
request.form on the server side.

You probably want something like this:

     app.post('/some_view', data=dict(address='123 Main St', city='NYC'))


Regards,
-- 
Simon Sapin

Re: [flask] SlectFields in unit tests

From:
Max Countryman
Date:
2011-12-03 @ 20:58
Hm, but that won't work for fields that need to be validated, right? For 
example, say you have a select field for credit card expiry dates. The 
date is validated, in order to make sure it's at least equal to or greater
than the current date. If I can't control what's selected, the default 
date will always be wrong (January, 2011). This prevents me from being 
able to POST to the view and evaluating the response. Suggestions?

On Dec 3, 2011, at 2:41 PM, Simon Sapin wrote:

> Le 03/12/2011 19:47, Max Countryman a écrit :
>> Hi all,
>> 
>> I've got a question about selecting SelectField options in unit tests? 
While typical input data is easily set via the data dict, I don't see any 
example for handling other things of input fields (e.g. SelectField). Can 
someone point me in the right direction?
>> 
>> As a quick example assume I have a form where there's an input field 
for address and a SelectField for state. I want to select the various 
options in this SelectField via a POST request executed in my unit tests, 
so something like: app.post('/some_view', data=data=dict(address='123 Main
St', city=('NYC', 'NYC'))) # this doesn't quite do it though
>> 
>> Thanks!
> 
> Hi,
> 
> It does not matter to the test client what the HTML looks like. For the 
> form data in client.post(), only pass values the same way you get it in 
> request.form on the server side.
> 
> You probably want something like this:
> 
>     app.post('/some_view', data=dict(address='123 Main St', city='NYC'))
> 
> 
> Regards,
> -- 
> Simon Sapin

Re: [flask] SlectFields in unit tests

From:
Adam Patterson
Date:
2011-12-03 @ 22:12
You just set them to whatever values you want.

app.post('/some_view', data=dict(exp_month='01', exp_year='2011')) --
should fail
app.post('/some_view', data=dict(exp_month='08', exp_year='2012')) --
should pass

The form validation will happen in the view after the POST.


On Sun, Dec 4, 2011 at 3:58 AM, Max Countryman <maxc@me.com> wrote:
> Hm, but that won't work for fields that need to be validated, right? For
example, say you have a select field for credit card expiry dates. The 
date is validated, in order to make sure it's at least equal to or greater
than the current date. If I can't control what's selected, the default 
date will always be wrong (January, 2011). This prevents me from being 
able to POST to the view and evaluating the response. Suggestions?
>
> On Dec 3, 2011, at 2:41 PM, Simon Sapin wrote:
>
>> Le 03/12/2011 19:47, Max Countryman a écrit :
>>> Hi all,
>>>
>>> I've got a question about selecting SelectField options in unit tests?
While typical input data is easily set via the data dict, I don't see any 
example for handling other things of input fields (e.g. SelectField). Can 
someone point me in the right direction?
>>>
>>> As a quick example assume I have a form where there's an input field 
for address and a SelectField for state. I want to select the various 
options in this SelectField via a POST request executed in my unit tests, 
so something like: app.post('/some_view', data=data=dict(address='123 Main
St', city=('NYC', 'NYC'))) # this doesn't quite do it though
>>>
>>> Thanks!
>>
>> Hi,
>>
>> It does not matter to the test client what the HTML looks like. For the
>> form data in client.post(), only pass values the same way you get it in
>> request.form on the server side.
>>
>> You probably want something like this:
>>
>>     app.post('/some_view', data=dict(address='123 Main St', city='NYC'))
>>
>>
>> Regards,
>> --
>> Simon Sapin
>

Re: [flask] SlectFields in unit tests

From:
Max Countryman
Date:
2011-12-04 @ 20:16
It seems I might be having an issue with the client-side JavaScript…so 
slightly off-topic, but in terms of unit testing with Flask, how should I 
handle instances where there's JavaScript on the page that is, in effect, 
hijacking the POST, processing, and returning a token to the backend? (If 
you're wondering if I'm describing Stripe's API, you're correct.) It seems
that the test client isn't actually executing this JavaScript (as far as I
can tell, although I'm not positive).

Suggestions appreciated! Thanks,


Max