Permalink
Browse files

Added tests for django.forms.formsets.all_valid().

  • Loading branch information...
timgraham committed Jan 12, 2018
1 parent 1f0813c commit 4bcec02368b7e5466f64dc17286689b16613c94b
Showing with 41 additions and 1 deletion.
  1. +41 −1 tests/forms_tests/tests/test_formsets.py
@@ -6,7 +6,7 @@
BaseForm, CharField, DateField, FileField, Form, IntegerField,
SplitDateTimeField, ValidationError, formsets,
)
-from django.forms.formsets import BaseFormSet, formset_factory
+from django.forms.formsets import BaseFormSet, all_valid, formset_factory
from django.forms.utils import ErrorList
from django.test import SimpleTestCase
@@ -1320,3 +1320,43 @@ def test_empty_formset_is_multipart(self):
class FileForm(Form):
file = FileField()
self.assertTrue(formset_factory(FileForm, extra=0)().is_multipart())
+
+
+class AllValidTests(SimpleTestCase):
+
+ def test_valid(self):
+ data = {
+ 'choices-TOTAL_FORMS': '2',
+ 'choices-INITIAL_FORMS': '0',
+ 'choices-MIN_NUM_FORMS': '0',
+ 'choices-0-choice': 'Zero',
+ 'choices-0-votes': '0',
+ 'choices-1-choice': 'One',
+ 'choices-1-votes': '1',
+ }
+ ChoiceFormSet = formset_factory(Choice)
+ formset1 = ChoiceFormSet(data, auto_id=False, prefix='choices')
+ formset2 = ChoiceFormSet(data, auto_id=False, prefix='choices')
+ self.assertIs(all_valid((formset1, formset2)), True)
+ expected_errors = [{}, {}]
+ self.assertEqual(formset1._errors, expected_errors)
+ self.assertEqual(formset2._errors, expected_errors)
+
+ def test_invalid(self):
+ """all_valid() validates all forms, even when some are invalid."""
+ data = {
+ 'choices-TOTAL_FORMS': '2',
+ 'choices-INITIAL_FORMS': '0',
+ 'choices-MIN_NUM_FORMS': '0',
+ 'choices-0-choice': 'Zero',
+ 'choices-0-votes': '',
+ 'choices-1-choice': 'One',
+ 'choices-1-votes': '',
+ }
+ ChoiceFormSet = formset_factory(Choice)
+ formset1 = ChoiceFormSet(data, auto_id=False, prefix='choices')
+ formset2 = ChoiceFormSet(data, auto_id=False, prefix='choices')
+ self.assertIs(all_valid((formset1, formset2)), False)
+ expected_errors = [{'votes': ['This field is required.']}, {'votes': ['This field is required.']}]
+ self.assertEqual(formset1._errors, expected_errors)
+ self.assertEqual(formset2._errors, expected_errors)

0 comments on commit 4bcec02

Please sign in to comment.