Ticket #9061: after.patch

File after.patch, 2.5 KB (added by Daniel Ward, 4 years ago)

Tests introduced to ensure all is well following introduction of potential solution

  • tests/forms_tests/tests/test_formsets.py

    diff --git tests/forms_tests/tests/test_formsets.py tests/forms_tests/tests/test_formsets.py
    index 0bd2811..a13cab5 100644
    class FormsFormsetTestCase(SimpleTestCase): 
    461461        self.assertTrue(p.is_valid())
    462462        self.assertEqual(len(p.deleted_forms), 1)
    463463
     464    def test_delete_fields_are_not_added_for_extra_formset_forms(self):
     465
     466        """
     467        When a formset is built with can_delete=True and extra>=1, the extra
     468        forms should not be seen to hold DELETE fields.
     469        """
     470
     471        class GenericForm(Form):
     472            foo = CharField()
     473
     474        GenericFormFormset = formset_factory(
     475            form=GenericForm,
     476            can_delete=True,
     477            can_delete_extra=False,
     478            extra=2)
     479
     480        # Instantiate formset and check DELETE fields do not exist
     481
     482        formset = GenericFormFormset()
     483
     484        self.assertEqual(len(formset), 2)
     485
     486        self.assertNotIn('DELETE', formset.forms[0].fields)
     487        self.assertNotIn('DELETE', formset.forms[1].fields)
     488
     489        # Instantiate formset with initial data, check DELETE's do not exist
     490
     491        formset = GenericFormFormset(initial=[{'foo': 'bar'}])
     492
     493        self.assertEqual(len(formset), 3)
     494
     495        self.assertIn('DELETE', formset.forms[0].fields)
     496        self.assertNotIn('DELETE', formset.forms[1].fields)
     497        self.assertNotIn('DELETE', formset.forms[2].fields)
     498
     499        # Ensure validation behaviour of formsets works correctly
     500
     501        formset = GenericFormFormset(data={
     502            'form-0-foo': 'bar',
     503            'form-0-DELETE': 'on',
     504            'form-1-foo': 'baz',
     505            'form-2-foo': '',
     506            'form-TOTAL_FORMS': '3',
     507            'form-INITIAL_FORMS': '1',
     508        }, initial=[{'foo': 'bar'}])
     509
     510        # Check correct cleaned data from the formset
     511
     512        self.assertEqual(formset.cleaned_data, [
     513            {'foo': u'bar', u'DELETE': True},
     514            {'foo': u'baz'},
     515            {},
     516        ])
     517
     518        # Ensure deletion flag is correctly detected by formset method
     519
     520        self.assertTrue(formset._should_delete_form(formset.forms[0]))
     521        self.assertFalse(formset._should_delete_form(formset.forms[1]))
     522        self.assertFalse(formset._should_delete_form(formset.forms[2]))
     523
    464524    def test_formsets_with_ordering(self):
    465525        # FormSets with ordering ######################################################
    466526        # We can also add ordering ability to a FormSet with an argument to
Back to Top