From d77657737fcf09a28bc7905be15974977439351a Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 12 Mar 2019 15:46:39 +0100 Subject: [PATCH] Simplified forms_tests.tests.test_formsets. --- tests/forms_tests/tests/test_formsets.py | 263 ++++++++--------------- 1 file changed, 95 insertions(+), 168 deletions(-) diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py index 7d06104791..231436f73e 100644 --- a/tests/forms_tests/tests/test_formsets.py +++ b/tests/forms_tests/tests/test_formsets.py @@ -192,13 +192,9 @@ class FormsFormsetTestCase(SimpleTestCase): dicts to the `initial` argument. By default, an extra blank form is included. """ - initial = [{'choice': 'Calexico', 'votes': 100}] - formset = self.make_choiceformset(initial=initial) - form_output = [] - for form in formset.forms: - form_output.append(form.as_ul()) + formset = self.make_choiceformset(initial=[{'choice': 'Calexico', 'votes': 100}]) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Choice:
  • @@ -239,11 +235,8 @@ class FormsFormsetTestCase(SimpleTestCase): """ ChoiceFormSet = formset_factory(Choice, extra=3) formset = ChoiceFormSet(auto_id=False, prefix='choices') - form_output = [] - for form in formset.forms: - form_output.append(form.as_ul()) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Choice:
  • @@ -277,14 +270,11 @@ class FormsFormsetTestCase(SimpleTestCase): """ ChoiceFormSet = formset_factory(Choice, extra=1, min_num=1) 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. self.assertFalse(formset.forms[0].empty_permitted) self.assertTrue(formset.forms[1].empty_permitted) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Choice:
  • @@ -295,11 +285,8 @@ class FormsFormsetTestCase(SimpleTestCase): """More than 1 empty form can be displayed using min_num.""" ChoiceFormSet = formset_factory(Choice, extra=0, min_num=3) formset = ChoiceFormSet(auto_id=False, prefix='choices') - form_output = [] - for form in formset.forms: - form_output.append(form.as_ul()) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Choice:
  • @@ -434,11 +421,8 @@ class FormsFormsetTestCase(SimpleTestCase): initial = [{'choice': 'Calexico', 'votes': 100}] ChoiceFormSet = formset_factory(Choice, extra=3) formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') - form_output = [] - for form in formset.forms: - form_output.append(form.as_ul()) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Choice:
  • @@ -465,11 +449,8 @@ class FormsFormsetTestCase(SimpleTestCase): ChoiceFormSet = formset_factory(Choice, can_delete=True) initial = [{'choice': 'Calexico', 'votes': 100}, {'choice': 'Fergie', 'votes': 900}] formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') - form_output = [] - for form in formset.forms: - form_output.append(form.as_ul()) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Delete:
  • @@ -546,21 +527,18 @@ class FormsFormsetTestCase(SimpleTestCase): deleted_forms works on a valid formset even if a deleted form would have been invalid. """ - class Person(Form): - name = CharField() - - PeopleForm = formset_factory(form=Person, can_delete=True) - p = PeopleForm({ + FavoriteDrinkFormset = formset_factory(form=FavoriteDrinkForm, can_delete=True) + formset = FavoriteDrinkFormset({ 'form-0-name': '', 'form-0-DELETE': 'on', # no name! 'form-TOTAL_FORMS': 1, 'form-INITIAL_FORMS': 1, 'form-MIN_NUM_FORMS': 0, - 'form-MAX_NUM_FORMS': 1}, - ) - self.assertTrue(p.is_valid()) - self.assertEqual(p._errors, []) - self.assertEqual(len(p.deleted_forms), 1) + 'form-MAX_NUM_FORMS': 1, + }) + self.assertTrue(formset.is_valid()) + self.assertEqual(formset._errors, []) + self.assertEqual(len(formset.deleted_forms), 1) def test_formsets_with_ordering(self): """ @@ -575,11 +553,8 @@ class FormsFormsetTestCase(SimpleTestCase): ChoiceFormSet = formset_factory(Choice, can_order=True) initial = [{'choice': 'Calexico', 'votes': 100}, {'choice': 'Fergie', 'votes': 900}] formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') - form_output = [] - for form in formset.forms: - form_output.append(form.as_ul()) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Order:
  • @@ -605,17 +580,16 @@ class FormsFormsetTestCase(SimpleTestCase): 'choices-2-votes': '500', 'choices-2-ORDER': '0', } - formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertTrue(formset.is_valid()) - form_output = [] - for form in formset.ordered_forms: - form_output.append(form.cleaned_data) - self.assertEqual(form_output, [ - {'votes': 500, 'ORDER': 0, 'choice': 'The Decemberists'}, - {'votes': 100, 'ORDER': 1, 'choice': 'Calexico'}, - {'votes': 900, 'ORDER': 2, 'choice': 'Fergie'}, - ]) + self.assertEqual( + [form.cleaned_data for form in formset.ordered_forms], + [ + {'votes': 500, 'ORDER': 0, 'choice': 'The Decemberists'}, + {'votes': 100, 'ORDER': 1, 'choice': 'Calexico'}, + {'votes': 900, 'ORDER': 2, 'choice': 'Fergie'}, + ], + ) def test_empty_ordered_fields(self): """ @@ -643,15 +617,15 @@ class FormsFormsetTestCase(SimpleTestCase): ChoiceFormSet = formset_factory(Choice, can_order=True) formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertTrue(formset.is_valid()) - form_output = [] - for form in formset.ordered_forms: - form_output.append(form.cleaned_data) - self.assertEqual(form_output, [ - {'votes': 100, 'ORDER': 1, 'choice': 'Calexico'}, - {'votes': 900, 'ORDER': 2, 'choice': 'Fergie'}, - {'votes': 500, 'ORDER': None, 'choice': 'The Decemberists'}, - {'votes': 50, 'ORDER': None, 'choice': 'Basia Bulat'}, - ]) + self.assertEqual( + [form.cleaned_data for form in formset.ordered_forms], + [ + {'votes': 100, 'ORDER': 1, 'choice': 'Calexico'}, + {'votes': 900, 'ORDER': 2, 'choice': 'Fergie'}, + {'votes': 500, 'ORDER': None, 'choice': 'The Decemberists'}, + {'votes': 50, 'ORDER': None, 'choice': 'Basia Bulat'}, + ], + ) def test_ordering_blank_fieldsets(self): """Ordering works with blank fieldsets.""" @@ -664,26 +638,19 @@ class FormsFormsetTestCase(SimpleTestCase): ChoiceFormSet = formset_factory(Choice, can_order=True) formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertTrue(formset.is_valid()) - form_output = [] - for form in formset.ordered_forms: - form_output.append(form.cleaned_data) - self.assertEqual(form_output, []) + self.assertEqual(formset.ordered_forms, []) def test_formset_with_ordering_and_deletion(self): """FormSets with ordering + deletion.""" ChoiceFormSet = formset_factory(Choice, can_order=True, can_delete=True) - initial = [ {'choice': 'Calexico', 'votes': 100}, {'choice': 'Fergie', 'votes': 900}, {'choice': 'The Decemberists', 'votes': 500}, ] formset = ChoiceFormSet(initial=initial, auto_id=False, prefix='choices') - form_output = [] - for form in formset.forms: - form_output.append(form.as_ul()) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(form.as_ul() for form in formset.forms), """
  • Choice:
  • Votes:
  • Order:
  • @@ -726,13 +693,13 @@ class FormsFormsetTestCase(SimpleTestCase): } formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertTrue(formset.is_valid()) - form_output = [] - 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': 100, 'DELETE': False, 'ORDER': 1, 'choice': 'Calexico'}, - ]) + self.assertEqual( + [form.cleaned_data for form in formset.ordered_forms], + [ + {'votes': 500, 'DELETE': False, 'ORDER': 0, 'choice': 'The Decemberists'}, + {'votes': 100, 'DELETE': False, 'ORDER': 1, 'choice': 'Calexico'}, + ], + ) self.assertEqual( [form.cleaned_data for form in formset.deleted_forms], [{'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 would have been invalid. """ - class Person(Form): - name = CharField() - - PeopleForm = formset_factory(form=Person, can_delete=True, can_order=True) - p = PeopleForm({ + FavoriteDrinkFormset = formset_factory(form=FavoriteDrinkForm, can_delete=True, can_order=True) + formset = FavoriteDrinkFormset({ 'form-0-name': '', 'form-0-DELETE': 'on', # no name! 'form-TOTAL_FORMS': 1, @@ -755,8 +719,8 @@ class FormsFormsetTestCase(SimpleTestCase): 'form-MIN_NUM_FORMS': 0, 'form-MAX_NUM_FORMS': 1 }) - self.assertTrue(p.is_valid()) - self.assertEqual(p.ordered_forms, []) + self.assertTrue(formset.is_valid()) + self.assertEqual(formset.ordered_forms, []) def test_clean_hook(self): """ @@ -779,14 +743,7 @@ class FormsFormsetTestCase(SimpleTestCase): for error in formset.non_form_errors(): self.assertEqual(str(error), 'You may only specify a drink once.') # The valid case still works. - data = { - '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', - } + data['drinks-1-name'] = 'Bloody Mary' formset = FavoriteDrinksFormSet(data, prefix='drinks') self.assertTrue(formset.is_valid()) 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. LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3) formset = LimitedFavoriteDrinkFormSet() - form_output = [] - for form in formset.forms: - form_output.append(str(form)) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(str(form) for form in formset.forms), """ @@ -812,19 +766,13 @@ class FormsFormsetTestCase(SimpleTestCase): # If max_num is 0 then no form is rendered at all. LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3, max_num=0) formset = LimitedFavoriteDrinkFormSet() - form_output = [] - for form in formset.forms: - form_output.append(str(form)) - self.assertEqual('\n'.join(form_output), "") + self.assertEqual(formset.forms, []) def test_limited_max_forms_two(self): LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=5, max_num=2) formset = LimitedFavoriteDrinkFormSet() - form_output = [] - for form in formset.forms: - form_output.append(str(form)) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(str(form) for form in formset.forms), """ @@ -835,30 +783,20 @@ class FormsFormsetTestCase(SimpleTestCase): """max_num has no effect when extra is less than max_num.""" LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=2) formset = LimitedFavoriteDrinkFormSet() - form_output = [] - for form in formset.forms: - form_output.append(str(form)) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(str(form) for form in formset.forms), """ """ ) 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 # number of forms only controlled by the value of the initial and extra # parameters. - initial = [ - {'name': 'Fernet and Coke'}, - ] LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1) - formset = LimitedFavoriteDrinkFormSet(initial=initial) - form_output = [] - for form in formset.forms: - form_output.append(str(form)) + formset = LimitedFavoriteDrinkFormSet(initial=[{'name': 'Fernet and Coke'}]) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(str(form) for form in formset.forms), """ @@ -872,10 +810,7 @@ class FormsFormsetTestCase(SimpleTestCase): """ LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=0) formset = LimitedFavoriteDrinkFormSet() - form_output = [] - for form in formset.forms: - form_output.append(str(form)) - self.assertEqual('\n'.join(form_output), "") + self.assertEqual(formset.forms, []) def test_max_num_zero_with_initial(self): # initial trumps max_num @@ -885,11 +820,8 @@ class FormsFormsetTestCase(SimpleTestCase): ] LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=0) formset = LimitedFavoriteDrinkFormSet(initial=initial) - form_output = [] - for form in formset.forms: - form_output.append(str(form)) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(str(form) for form in formset.forms), """ @@ -908,11 +840,8 @@ class FormsFormsetTestCase(SimpleTestCase): ] LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=1, max_num=2) formset = LimitedFavoriteDrinkFormSet(initial=initial) - form_output = [] - for form in formset.forms: - form_output.append(str(form)) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(str(form) for form in formset.forms), """ @@ -926,16 +855,10 @@ class FormsFormsetTestCase(SimpleTestCase): One form from initial and extra=3 with max_num=2 results in the one initial form and one extra. """ - initial = [ - {'name': 'Gin Tonic'}, - ] LimitedFavoriteDrinkFormSet = formset_factory(FavoriteDrinkForm, extra=3, max_num=2) - formset = LimitedFavoriteDrinkFormSet(initial=initial) - form_output = [] - for form in formset.forms: - form_output.append(str(form)) + formset = LimitedFavoriteDrinkFormSet(initial=[{'name': 'Gin Tonic'}]) self.assertHTMLEqual( - '\n'.join(form_output), + '\n'.join(str(form) for form in formset.forms), """ @@ -1123,6 +1046,10 @@ class FormsFormsetTestCase(SimpleTestCase): raise ValidationError("This is a non-form error") ChoiceFormSet = formset_factory(Choice, formset=BaseCustomFormSet) + data = { + 'choices-TOTAL_FORMS': '1', + 'choices-INITIAL_FORMS': '0', + } formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertIsInstance(formset.non_form_errors(), ErrorList) 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__()) -data = { - 'choices-TOTAL_FORMS': '1', # the number of forms rendered - 'choices-INITIAL_FORMS': '0', # the number of forms with initial data - 'choices-MIN_NUM_FORMS': '0', # min number of forms - 'choices-MAX_NUM_FORMS': '0', # max number of forms - 'choices-0-choice': 'Calexico', - 'choices-0-votes': '100', -} +class FormsetAsTagTests(SimpleTestCase): + def setUp(self): + data = { + 'choices-TOTAL_FORMS': '1', + 'choices-INITIAL_FORMS': '0', + 'choices-MIN_NUM_FORMS': '0', + '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 = ( + '' + '' + '' + '' + ) - -class FormsetAsFooTests(SimpleTestCase): def test_as_table(self): - formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertHTMLEqual( - formset.as_table(), - """ - - - -Choice: -Votes:""" + self.formset.as_table(), + self.management_form_html + ( + 'Choice:' + '' + 'Votes:' + '' + ) ) def test_as_p(self): - formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertHTMLEqual( - formset.as_p(), - """ - - - -

    Choice:

    -

    Votes:

    """ + self.formset.as_p(), + self.management_form_html + ( + '

    Choice:

    ' + '

    Votes:

    ' + ) ) def test_as_ul(self): - formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertHTMLEqual( - formset.as_ul(), - """ - - - -
  • Choice:
  • -
  • Votes:
  • """ + self.formset.as_ul(), + self.management_form_html + ( + '
  • Choice:
  • ' + '
  • Votes:
  • ' + ) )