mirror of https://github.com/django/django.git
Simplified forms_tests.tests.test_formsets.
This commit is contained in:
parent
9fd90c4088
commit
d77657737f
|
@ -192,13 +192,9 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
dicts to the `initial` argument. By default, an extra blank form is
|
dicts to the `initial` argument. By default, an extra blank form is
|
||||||
included.
|
included.
|
||||||
"""
|
"""
|
||||||
initial = [{'choice': 'Calexico', 'votes': 100}]
|
formset = self.make_choiceformset(initial=[{'choice': 'Calexico', 'votes': 100}])
|
||||||
formset = self.make_choiceformset(initial=initial)
|
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
||||||
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
||||||
|
@ -239,11 +235,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
"""
|
"""
|
||||||
ChoiceFormSet = formset_factory(Choice, extra=3)
|
ChoiceFormSet = formset_factory(Choice, extra=3)
|
||||||
formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes"></li>
|
<li>Votes: <input type="number" name="choices-0-votes"></li>
|
||||||
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
||||||
|
@ -277,14 +270,11 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
"""
|
"""
|
||||||
ChoiceFormSet = formset_factory(Choice, extra=1, min_num=1)
|
ChoiceFormSet = formset_factory(Choice, extra=1, min_num=1)
|
||||||
formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
# Min_num forms are required; extra forms can be empty.
|
# Min_num forms are required; extra forms can be empty.
|
||||||
self.assertFalse(formset.forms[0].empty_permitted)
|
self.assertFalse(formset.forms[0].empty_permitted)
|
||||||
self.assertTrue(formset.forms[1].empty_permitted)
|
self.assertTrue(formset.forms[1].empty_permitted)
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes"></li>
|
<li>Votes: <input type="number" name="choices-0-votes"></li>
|
||||||
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
||||||
|
@ -295,11 +285,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
"""More than 1 empty form can be displayed using min_num."""
|
"""More than 1 empty form can be displayed using min_num."""
|
||||||
ChoiceFormSet = formset_factory(Choice, extra=0, min_num=3)
|
ChoiceFormSet = formset_factory(Choice, extra=0, min_num=3)
|
||||||
formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(auto_id=False, prefix='choices')
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes"></li>
|
<li>Votes: <input type="number" name="choices-0-votes"></li>
|
||||||
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
||||||
|
@ -434,11 +421,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
initial = [{'choice': 'Calexico', 'votes': 100}]
|
initial = [{'choice': 'Calexico', 'votes': 100}]
|
||||||
ChoiceFormSet = formset_factory(Choice, extra=3)
|
ChoiceFormSet = formset_factory(Choice, extra=3)
|
||||||
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
||||||
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
<li>Choice: <input type="text" name="choices-1-choice"></li>
|
||||||
|
@ -465,11 +449,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
ChoiceFormSet = formset_factory(Choice, can_delete=True)
|
ChoiceFormSet = formset_factory(Choice, can_delete=True)
|
||||||
initial = [{'choice': 'Calexico', 'votes': 100}, {'choice': 'Fergie', 'votes': 900}]
|
initial = [{'choice': 'Calexico', 'votes': 100}, {'choice': 'Fergie', 'votes': 900}]
|
||||||
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
||||||
<li>Delete: <input type="checkbox" name="choices-0-DELETE"></li>
|
<li>Delete: <input type="checkbox" name="choices-0-DELETE"></li>
|
||||||
|
@ -546,21 +527,18 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
deleted_forms works on a valid formset even if a deleted form would
|
deleted_forms works on a valid formset even if a deleted form would
|
||||||
have been invalid.
|
have been invalid.
|
||||||
"""
|
"""
|
||||||
class Person(Form):
|
FavoriteDrinkFormset = formset_factory(form=FavoriteDrinkForm, can_delete=True)
|
||||||
name = CharField()
|
formset = FavoriteDrinkFormset({
|
||||||
|
|
||||||
PeopleForm = formset_factory(form=Person, can_delete=True)
|
|
||||||
p = PeopleForm({
|
|
||||||
'form-0-name': '',
|
'form-0-name': '',
|
||||||
'form-0-DELETE': 'on', # no name!
|
'form-0-DELETE': 'on', # no name!
|
||||||
'form-TOTAL_FORMS': 1,
|
'form-TOTAL_FORMS': 1,
|
||||||
'form-INITIAL_FORMS': 1,
|
'form-INITIAL_FORMS': 1,
|
||||||
'form-MIN_NUM_FORMS': 0,
|
'form-MIN_NUM_FORMS': 0,
|
||||||
'form-MAX_NUM_FORMS': 1},
|
'form-MAX_NUM_FORMS': 1,
|
||||||
)
|
})
|
||||||
self.assertTrue(p.is_valid())
|
self.assertTrue(formset.is_valid())
|
||||||
self.assertEqual(p._errors, [])
|
self.assertEqual(formset._errors, [])
|
||||||
self.assertEqual(len(p.deleted_forms), 1)
|
self.assertEqual(len(formset.deleted_forms), 1)
|
||||||
|
|
||||||
def test_formsets_with_ordering(self):
|
def test_formsets_with_ordering(self):
|
||||||
"""
|
"""
|
||||||
|
@ -575,11 +553,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
ChoiceFormSet = formset_factory(Choice, can_order=True)
|
ChoiceFormSet = formset_factory(Choice, can_order=True)
|
||||||
initial = [{'choice': 'Calexico', 'votes': 100}, {'choice': 'Fergie', 'votes': 900}]
|
initial = [{'choice': 'Calexico', 'votes': 100}, {'choice': 'Fergie', 'votes': 900}]
|
||||||
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
||||||
<li>Order: <input type="number" name="choices-0-ORDER" value="1"></li>
|
<li>Order: <input type="number" name="choices-0-ORDER" value="1"></li>
|
||||||
|
@ -605,17 +580,16 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
'choices-2-votes': '500',
|
'choices-2-votes': '500',
|
||||||
'choices-2-ORDER': '0',
|
'choices-2-ORDER': '0',
|
||||||
}
|
}
|
||||||
|
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
||||||
self.assertTrue(formset.is_valid())
|
self.assertTrue(formset.is_valid())
|
||||||
form_output = []
|
self.assertEqual(
|
||||||
for form in formset.ordered_forms:
|
[form.cleaned_data for form in formset.ordered_forms],
|
||||||
form_output.append(form.cleaned_data)
|
[
|
||||||
self.assertEqual(form_output, [
|
{'votes': 500, 'ORDER': 0, 'choice': 'The Decemberists'},
|
||||||
{'votes': 500, 'ORDER': 0, 'choice': 'The Decemberists'},
|
{'votes': 100, 'ORDER': 1, 'choice': 'Calexico'},
|
||||||
{'votes': 100, 'ORDER': 1, 'choice': 'Calexico'},
|
{'votes': 900, 'ORDER': 2, 'choice': 'Fergie'},
|
||||||
{'votes': 900, 'ORDER': 2, 'choice': 'Fergie'},
|
],
|
||||||
])
|
)
|
||||||
|
|
||||||
def test_empty_ordered_fields(self):
|
def test_empty_ordered_fields(self):
|
||||||
"""
|
"""
|
||||||
|
@ -643,15 +617,15 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
ChoiceFormSet = formset_factory(Choice, can_order=True)
|
ChoiceFormSet = formset_factory(Choice, can_order=True)
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
||||||
self.assertTrue(formset.is_valid())
|
self.assertTrue(formset.is_valid())
|
||||||
form_output = []
|
self.assertEqual(
|
||||||
for form in formset.ordered_forms:
|
[form.cleaned_data for form in formset.ordered_forms],
|
||||||
form_output.append(form.cleaned_data)
|
[
|
||||||
self.assertEqual(form_output, [
|
{'votes': 100, 'ORDER': 1, 'choice': 'Calexico'},
|
||||||
{'votes': 100, 'ORDER': 1, 'choice': 'Calexico'},
|
{'votes': 900, 'ORDER': 2, 'choice': 'Fergie'},
|
||||||
{'votes': 900, 'ORDER': 2, 'choice': 'Fergie'},
|
{'votes': 500, 'ORDER': None, 'choice': 'The Decemberists'},
|
||||||
{'votes': 500, 'ORDER': None, 'choice': 'The Decemberists'},
|
{'votes': 50, 'ORDER': None, 'choice': 'Basia Bulat'},
|
||||||
{'votes': 50, 'ORDER': None, 'choice': 'Basia Bulat'},
|
],
|
||||||
])
|
)
|
||||||
|
|
||||||
def test_ordering_blank_fieldsets(self):
|
def test_ordering_blank_fieldsets(self):
|
||||||
"""Ordering works with blank fieldsets."""
|
"""Ordering works with blank fieldsets."""
|
||||||
|
@ -664,26 +638,19 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
ChoiceFormSet = formset_factory(Choice, can_order=True)
|
ChoiceFormSet = formset_factory(Choice, can_order=True)
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
||||||
self.assertTrue(formset.is_valid())
|
self.assertTrue(formset.is_valid())
|
||||||
form_output = []
|
self.assertEqual(formset.ordered_forms, [])
|
||||||
for form in formset.ordered_forms:
|
|
||||||
form_output.append(form.cleaned_data)
|
|
||||||
self.assertEqual(form_output, [])
|
|
||||||
|
|
||||||
def test_formset_with_ordering_and_deletion(self):
|
def test_formset_with_ordering_and_deletion(self):
|
||||||
"""FormSets with ordering + deletion."""
|
"""FormSets with ordering + deletion."""
|
||||||
ChoiceFormSet = formset_factory(Choice, can_order=True, can_delete=True)
|
ChoiceFormSet = formset_factory(Choice, can_order=True, can_delete=True)
|
||||||
|
|
||||||
initial = [
|
initial = [
|
||||||
{'choice': 'Calexico', 'votes': 100},
|
{'choice': 'Calexico', 'votes': 100},
|
||||||
{'choice': 'Fergie', 'votes': 900},
|
{'choice': 'Fergie', 'votes': 900},
|
||||||
{'choice': 'The Decemberists', 'votes': 500},
|
{'choice': 'The Decemberists', 'votes': 500},
|
||||||
]
|
]
|
||||||
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices')
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(form.as_ul())
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(form.as_ul() for form in formset.forms),
|
||||||
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
"""<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
||||||
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>
|
||||||
<li>Order: <input type="number" name="choices-0-ORDER" value="1"></li>
|
<li>Order: <input type="number" name="choices-0-ORDER" value="1"></li>
|
||||||
|
@ -726,13 +693,13 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
}
|
}
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
||||||
self.assertTrue(formset.is_valid())
|
self.assertTrue(formset.is_valid())
|
||||||
form_output = []
|
self.assertEqual(
|
||||||
for form in formset.ordered_forms:
|
[form.cleaned_data for form in formset.ordered_forms],
|
||||||
form_output.append(form.cleaned_data)
|
[
|
||||||
self.assertEqual(form_output, [
|
{'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': 'The Decemberists'},
|
||||||
{'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': 'The Decemberists'},
|
{'votes': 100, 'DELETE': False, 'ORDER': 1, 'choice': 'Calexico'},
|
||||||
{'votes': 100, 'DELETE': False, 'ORDER': 1, 'choice': 'Calexico'},
|
],
|
||||||
])
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
[form.cleaned_data for form in formset.deleted_forms],
|
[form.cleaned_data for form in formset.deleted_forms],
|
||||||
[{'votes': 900, 'DELETE': True, 'ORDER': 2, 'choice': 'Fergie'}]
|
[{'votes': 900, 'DELETE': True, 'ORDER': 2, 'choice': 'Fergie'}]
|
||||||
|
@ -743,11 +710,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
Can get ordered_forms from a valid formset even if a deleted form
|
Can get ordered_forms from a valid formset even if a deleted form
|
||||||
would have been invalid.
|
would have been invalid.
|
||||||
"""
|
"""
|
||||||
class Person(Form):
|
FavoriteDrinkFormset = formset_factory(form=FavoriteDrinkForm, can_delete=True, can_order=True)
|
||||||
name = CharField()
|
formset = FavoriteDrinkFormset({
|
||||||
|
|
||||||
PeopleForm = formset_factory(form=Person, can_delete=True, can_order=True)
|
|
||||||
p = PeopleForm({
|
|
||||||
'form-0-name': '',
|
'form-0-name': '',
|
||||||
'form-0-DELETE': 'on', # no name!
|
'form-0-DELETE': 'on', # no name!
|
||||||
'form-TOTAL_FORMS': 1,
|
'form-TOTAL_FORMS': 1,
|
||||||
|
@ -755,8 +719,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
'form-MIN_NUM_FORMS': 0,
|
'form-MIN_NUM_FORMS': 0,
|
||||||
'form-MAX_NUM_FORMS': 1
|
'form-MAX_NUM_FORMS': 1
|
||||||
})
|
})
|
||||||
self.assertTrue(p.is_valid())
|
self.assertTrue(formset.is_valid())
|
||||||
self.assertEqual(p.ordered_forms, [])
|
self.assertEqual(formset.ordered_forms, [])
|
||||||
|
|
||||||
def test_clean_hook(self):
|
def test_clean_hook(self):
|
||||||
"""
|
"""
|
||||||
|
@ -779,14 +743,7 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
for error in formset.non_form_errors():
|
for error in formset.non_form_errors():
|
||||||
self.assertEqual(str(error), 'You may only specify a drink once.')
|
self.assertEqual(str(error), 'You may only specify a drink once.')
|
||||||
# The valid case still works.
|
# The valid case still works.
|
||||||
data = {
|
data['drinks-1-name'] = 'Bloody Mary'
|
||||||
'drinks-TOTAL_FORMS': '2', # the number of forms rendered
|
|
||||||
'drinks-INITIAL_FORMS': '0', # the number of forms with initial data
|
|
||||||
'drinks-MIN_NUM_FORMS': '0', # min number of forms
|
|
||||||
'drinks-MAX_NUM_FORMS': '0', # max number of forms
|
|
||||||
'drinks-0-name': 'Gin and Tonic',
|
|
||||||
'drinks-1-name': 'Bloody Mary',
|
|
||||||
}
|
|
||||||
formset = FavoriteDrinksFormSet(data, prefix='drinks')
|
formset = FavoriteDrinksFormSet(data, prefix='drinks')
|
||||||
self.assertTrue(formset.is_valid())
|
self.assertTrue(formset.is_valid())
|
||||||
self.assertEqual(formset.non_form_errors(), [])
|
self.assertEqual(formset.non_form_errors(), [])
|
||||||
|
@ -797,11 +754,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
# number of forms only controlled by the value of the extra parameter.
|
# number of forms only controlled by the value of the extra parameter.
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3)
|
||||||
formset = LimitedFavoriteDrinkFormSet()
|
formset = LimitedFavoriteDrinkFormSet()
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(str(form) for form in formset.forms),
|
||||||
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
||||||
<td><input type="text" name="form-0-name" id="id_form-0-name"></td></tr>
|
<td><input type="text" name="form-0-name" id="id_form-0-name"></td></tr>
|
||||||
<tr><th><label for="id_form-1-name">Name:</label></th>
|
<tr><th><label for="id_form-1-name">Name:</label></th>
|
||||||
|
@ -812,19 +766,13 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
# If max_num is 0 then no form is rendered at all.
|
# If max_num is 0 then no form is rendered at all.
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3, max_num=0)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3, max_num=0)
|
||||||
formset = LimitedFavoriteDrinkFormSet()
|
formset = LimitedFavoriteDrinkFormSet()
|
||||||
form_output = []
|
self.assertEqual(formset.forms, [])
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertEqual('\n'.join(form_output), "")
|
|
||||||
|
|
||||||
def test_limited_max_forms_two(self):
|
def test_limited_max_forms_two(self):
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=5, max_num=2)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=5, max_num=2)
|
||||||
formset = LimitedFavoriteDrinkFormSet()
|
formset = LimitedFavoriteDrinkFormSet()
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(str(form) for form in formset.forms),
|
||||||
"""<tr><th><label for="id_form-0-name">Name:</label></th><td>
|
"""<tr><th><label for="id_form-0-name">Name:</label></th><td>
|
||||||
<input type="text" name="form-0-name" id="id_form-0-name"></td></tr>
|
<input type="text" name="form-0-name" id="id_form-0-name"></td></tr>
|
||||||
<tr><th><label for="id_form-1-name">Name:</label></th>
|
<tr><th><label for="id_form-1-name">Name:</label></th>
|
||||||
|
@ -835,30 +783,20 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
"""max_num has no effect when extra is less than max_num."""
|
"""max_num has no effect when extra is less than max_num."""
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=2)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=2)
|
||||||
formset = LimitedFavoriteDrinkFormSet()
|
formset = LimitedFavoriteDrinkFormSet()
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(str(form) for form in formset.forms),
|
||||||
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
||||||
<td><input type="text" name="form-0-name" id="id_form-0-name"></td></tr>"""
|
<td><input type="text" name="form-0-name" id="id_form-0-name"></td></tr>"""
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_max_num_with_initial_data(self):
|
def test_max_num_with_initial_data(self):
|
||||||
"""max_num with initial data."""
|
|
||||||
# When not passed, max_num will take a high default value, leaving the
|
# When not passed, max_num will take a high default value, leaving the
|
||||||
# number of forms only controlled by the value of the initial and extra
|
# number of forms only controlled by the value of the initial and extra
|
||||||
# parameters.
|
# parameters.
|
||||||
initial = [
|
|
||||||
{'name': 'Fernet and Coke'},
|
|
||||||
]
|
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1)
|
||||||
formset = LimitedFavoriteDrinkFormSet(initial=initial)
|
formset = LimitedFavoriteDrinkFormSet(initial=[{'name': 'Fernet and Coke'}])
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(str(form) for form in formset.forms),
|
||||||
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
||||||
<td><input type="text" name="form-0-name" value="Fernet and Coke" id="id_form-0-name"></td></tr>
|
<td><input type="text" name="form-0-name" value="Fernet and Coke" id="id_form-0-name"></td></tr>
|
||||||
<tr><th><label for="id_form-1-name">Name:</label></th>
|
<tr><th><label for="id_form-1-name">Name:</label></th>
|
||||||
|
@ -872,10 +810,7 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
"""
|
"""
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=0)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=0)
|
||||||
formset = LimitedFavoriteDrinkFormSet()
|
formset = LimitedFavoriteDrinkFormSet()
|
||||||
form_output = []
|
self.assertEqual(formset.forms, [])
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertEqual('\n'.join(form_output), "")
|
|
||||||
|
|
||||||
def test_max_num_zero_with_initial(self):
|
def test_max_num_zero_with_initial(self):
|
||||||
# initial trumps max_num
|
# initial trumps max_num
|
||||||
|
@ -885,11 +820,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
]
|
]
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=0)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=0)
|
||||||
formset = LimitedFavoriteDrinkFormSet(initial=initial)
|
formset = LimitedFavoriteDrinkFormSet(initial=initial)
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(str(form) for form in formset.forms),
|
||||||
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
||||||
<td><input id="id_form-0-name" name="form-0-name" type="text" value="Fernet and Coke"></td></tr>
|
<td><input id="id_form-0-name" name="form-0-name" type="text" value="Fernet and Coke"></td></tr>
|
||||||
<tr><th><label for="id_form-1-name">Name:</label></th>
|
<tr><th><label for="id_form-1-name">Name:</label></th>
|
||||||
|
@ -908,11 +840,8 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
]
|
]
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=2)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=2)
|
||||||
formset = LimitedFavoriteDrinkFormSet(initial=initial)
|
formset = LimitedFavoriteDrinkFormSet(initial=initial)
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(str(form) for form in formset.forms),
|
||||||
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
||||||
<td><input id="id_form-0-name" name="form-0-name" type="text" value="Gin Tonic"></td></tr>
|
<td><input id="id_form-0-name" name="form-0-name" type="text" value="Gin Tonic"></td></tr>
|
||||||
<tr><th><label for="id_form-1-name">Name:</label></th>
|
<tr><th><label for="id_form-1-name">Name:</label></th>
|
||||||
|
@ -926,16 +855,10 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
One form from initial and extra=3 with max_num=2 results in the one
|
One form from initial and extra=3 with max_num=2 results in the one
|
||||||
initial form and one extra.
|
initial form and one extra.
|
||||||
"""
|
"""
|
||||||
initial = [
|
|
||||||
{'name': 'Gin Tonic'},
|
|
||||||
]
|
|
||||||
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3, max_num=2)
|
LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3, max_num=2)
|
||||||
formset = LimitedFavoriteDrinkFormSet(initial=initial)
|
formset = LimitedFavoriteDrinkFormSet(initial=[{'name': 'Gin Tonic'}])
|
||||||
form_output = []
|
|
||||||
for form in formset.forms:
|
|
||||||
form_output.append(str(form))
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
'\n'.join(form_output),
|
'\n'.join(str(form) for form in formset.forms),
|
||||||
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
"""<tr><th><label for="id_form-0-name">Name:</label></th>
|
||||||
<td><input type="text" name="form-0-name" value="Gin Tonic" id="id_form-0-name"></td></tr>
|
<td><input type="text" name="form-0-name" value="Gin Tonic" id="id_form-0-name"></td></tr>
|
||||||
<tr><th><label for="id_form-1-name">Name:</label></th>
|
<tr><th><label for="id_form-1-name">Name:</label></th>
|
||||||
|
@ -1123,6 +1046,10 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
raise ValidationError("This is a non-form error")
|
raise ValidationError("This is a non-form error")
|
||||||
|
|
||||||
ChoiceFormSet = formset_factory(Choice, formset=BaseCustomFormSet)
|
ChoiceFormSet = formset_factory(Choice, formset=BaseCustomFormSet)
|
||||||
|
data = {
|
||||||
|
'choices-TOTAL_FORMS': '1',
|
||||||
|
'choices-INITIAL_FORMS': '0',
|
||||||
|
}
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
||||||
self.assertIsInstance(formset.non_form_errors(), ErrorList)
|
self.assertIsInstance(formset.non_form_errors(), ErrorList)
|
||||||
self.assertEqual(list(formset.non_form_errors()), ['This is a non-form error'])
|
self.assertEqual(list(formset.non_form_errors()), ['This is a non-form error'])
|
||||||
|
@ -1180,51 +1107,51 @@ class FormsFormsetTestCase(SimpleTestCase):
|
||||||
self.assertEqual(str(formset), formset.__html__())
|
self.assertEqual(str(formset), formset.__html__())
|
||||||
|
|
||||||
|
|
||||||
data = {
|
class FormsetAsTagTests(SimpleTestCase):
|
||||||
'choices-TOTAL_FORMS': '1', # the number of forms rendered
|
def setUp(self):
|
||||||
'choices-INITIAL_FORMS': '0', # the number of forms with initial data
|
data = {
|
||||||
'choices-MIN_NUM_FORMS': '0', # min number of forms
|
'choices-TOTAL_FORMS': '1',
|
||||||
'choices-MAX_NUM_FORMS': '0', # max number of forms
|
'choices-INITIAL_FORMS': '0',
|
||||||
'choices-0-choice': 'Calexico',
|
'choices-MIN_NUM_FORMS': '0',
|
||||||
'choices-0-votes': '100',
|
'choices-MAX_NUM_FORMS': '0',
|
||||||
}
|
'choices-0-choice': 'Calexico',
|
||||||
|
'choices-0-votes': '100',
|
||||||
|
}
|
||||||
|
self.formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
||||||
|
self.management_form_html = (
|
||||||
|
'<input type="hidden" name="choices-TOTAL_FORMS" value="1">'
|
||||||
|
'<input type="hidden" name="choices-INITIAL_FORMS" value="0">'
|
||||||
|
'<input type="hidden" name="choices-MIN_NUM_FORMS" value="0">'
|
||||||
|
'<input type="hidden" name="choices-MAX_NUM_FORMS" value="0">'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class FormsetAsFooTests(SimpleTestCase):
|
|
||||||
def test_as_table(self):
|
def test_as_table(self):
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
formset.as_table(),
|
self.formset.as_table(),
|
||||||
"""<input type="hidden" name="choices-TOTAL_FORMS" value="1">
|
self.management_form_html + (
|
||||||
<input type="hidden" name="choices-INITIAL_FORMS" value="0">
|
'<tr><th>Choice:</th><td>'
|
||||||
<input type="hidden" name="choices-MIN_NUM_FORMS" value="0">
|
'<input type="text" name="choices-0-choice" value="Calexico"></td></tr>'
|
||||||
<input type="hidden" name="choices-MAX_NUM_FORMS" value="0">
|
'<tr><th>Votes:</th><td>'
|
||||||
<tr><th>Choice:</th><td><input type="text" name="choices-0-choice" value="Calexico"></td></tr>
|
'<input type="number" name="choices-0-votes" value="100"></td></tr>'
|
||||||
<tr><th>Votes:</th><td><input type="number" name="choices-0-votes" value="100"></td></tr>"""
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_as_p(self):
|
def test_as_p(self):
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
formset.as_p(),
|
self.formset.as_p(),
|
||||||
"""<input type="hidden" name="choices-TOTAL_FORMS" value="1">
|
self.management_form_html + (
|
||||||
<input type="hidden" name="choices-INITIAL_FORMS" value="0">
|
'<p>Choice: <input type="text" name="choices-0-choice" value="Calexico"></p>'
|
||||||
<input type="hidden" name="choices-MIN_NUM_FORMS" value="0">
|
'<p>Votes: <input type="number" name="choices-0-votes" value="100"></p>'
|
||||||
<input type="hidden" name="choices-MAX_NUM_FORMS" value="0">
|
)
|
||||||
<p>Choice: <input type="text" name="choices-0-choice" value="Calexico"></p>
|
|
||||||
<p>Votes: <input type="number" name="choices-0-votes" value="100"></p>"""
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_as_ul(self):
|
def test_as_ul(self):
|
||||||
formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
|
|
||||||
self.assertHTMLEqual(
|
self.assertHTMLEqual(
|
||||||
formset.as_ul(),
|
self.formset.as_ul(),
|
||||||
"""<input type="hidden" name="choices-TOTAL_FORMS" value="1">
|
self.management_form_html + (
|
||||||
<input type="hidden" name="choices-INITIAL_FORMS" value="0">
|
'<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>'
|
||||||
<input type="hidden" name="choices-MIN_NUM_FORMS" value="0">
|
'<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>'
|
||||||
<input type="hidden" name="choices-MAX_NUM_FORMS" value="0">
|
)
|
||||||
<li>Choice: <input type="text" name="choices-0-choice" value="Calexico"></li>
|
|
||||||
<li>Votes: <input type="number" name="choices-0-votes" value="100"></li>"""
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue