Code

Ticket #3334: form_tests.diff

File form_tests.diff, 1.8 KB (added by Deryck Hodge <deryck@…>, 7 years ago)

A patch to tests/regressiontests/forms/tests.py to test instances of dynamically created forms return the right fields.

Line 
1Index: tests/regressiontests/forms/tests.py
2===================================================================
3--- tests/regressiontests/forms/tests.py        (revision 4357)
4+++ tests/regressiontests/forms/tests.py        (working copy)
5@@ -2063,6 +2063,8 @@
6 >>> f.clean_data
7 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
8 
9+# Dynamic construction ########################################################
10+
11 It's possible to construct a Form dynamically by adding to the self.fields
12 dictionary in __init__(). Don't forget to call Form.__init__() within the
13 subclass' __init__().
14@@ -2078,6 +2080,25 @@
15 <tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
16 <tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>
17 
18+Instances of a dynamic form should not persist fields from one
19+form instance to the next
20+>>> class MyForm(Form):
21+...     def __init__(self, data=None, auto_id=False, field_list=[]):
22+...         Form.__init__(self, data, auto_id)
23+...         for field in field_list:
24+...             self.fields[field[0]] = field[1]
25+...
26+>>> field_list = ( ('field1', CharField()), ('field2', CharField()) )
27+>>> my_form = MyForm(field_list=field_list)
28+>>> print my_form
29+<tr><th>Field1:</th><td><input type="text" name="field1" /></td></tr>
30+<tr><th>Field2:</th><td><input type="text" name="field2" /></td></tr>
31+>>> field_list = ( ('field3', CharField()), ('field4', CharField()) )
32+>>> my_form = MyForm(field_list=field_list)
33+>>> print my_form
34+<tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr>
35+<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr>
36+
37 HiddenInput widgets are displayed differently in the as_table(), as_ul()
38 and as_p() output of a Form -- their verbose names are not displayed, and a
39 separate row is not displayed. They're displayed in the last row of the