Python/Django unittesting and formsets

In recent months I have been getting to grips with Python/Django. It all started back at the Health 2.0 Developer Challenge when I worked with Alan Viars of Videntity to build the Rainbowbutton.com. We built the application using Python/Django and ran it on Amazon’s cloud.

Now I am working on writing a series of Unit Tests for an application. It has been going pretty well but now I am hitting a road block and wonder if anyone else has had similar experience (and hopefully solutions).

I am tring to test a web page that uses multiple sections to the page and also uses formsets.

I get the error:

u'ManagementForm data is missing or has been tampered with'
when I try to post to the page using the UnitTest framework.

I have read various solutions on StackOverflow.com but I am still hitting that wall.

I have looked through the source of the page and extracted field names (there are hundreds in the whole form – you know what government forms are like!) 

The test I am trying to perform is to submit an incomplete form and test for an error message. However, the unittest bombs with the ManagementForm Data error.

Here is the data I am submitting (HIPAA disclaimer – all data is fictitious):

post_parameters = {‘medication_assistance’:”on”,
                           ‘food_stamps’:”on”,
                           ‘ida’:”on”,
                           ‘tanf_gc’:”on”,
                           ‘coinhabitants-0-last_name’:”First”,
                           ‘coinhabitants-0-middle_name’:””,
                           ‘coinhabitants-0-first_name’:”Arthur”,
                           ‘coinhabitants-0-applying_for_this_person’:”YES”,
                           ‘coinhabitants-0-applying_for_this_person’:”YES”,
                           ‘coinhabitants-0-gender’:”MALE”,
                           ‘coinhabitants-0-date_of_birth_month’:”1″,
                           ‘coinhabitants-0-date_of_birth_day’:”1″,
                           ‘coinhabitants-0-date_of_birth_year’:”1940″,
                           ‘coinhabitants-0-age’:”72″,
                           ‘coinhabitants-0-ssn’:”123-45-1234″,
                           ‘coinhabitants-0-us_citizen’:”YES”,
                           ‘coinhabitants-0-relation_to_you’:”self”,
                           ‘coinhabitants-0-eat_together’:”YES”,
                           ‘coinhabitants-0-TOTAL_FORMS’: u’1′,
                           ‘coinhabitants-0-INITIAL_FORMS’: u’0′,
                           ‘coinhabitants-0-MAX_NUM_FORMS’:u’2′

        } 

Every Django formset comes with a management form that needs to be included in the post. The official docs explain it pretty well. To use it within your unit test, you either need to write it out yourself. (The link I provided shows an example), or call formset.management_form which outputs the data.
[Provided by Bartek]

What is not clear to me is how to include the management_form data. 

Can anyone shed any light on what might need to be in the post parameters?
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s