summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2018-01-12 09:52:46 -0500
committerTim Graham <timograham@gmail.com>2018-01-12 12:40:21 -0500
commit4bcec02368b7e5466f64dc17286689b16613c94b (patch)
tree22415bdfda1f65e1ac5c45a62f4d4d5560f7b80b
parent1f0813ca7e8a15d10fcdaad1a5d66b7eec28dedb (diff)
Added tests for django.forms.formsets.all_valid().
-rw-r--r--tests/forms_tests/tests/test_formsets.py42
1 files changed, 41 insertions, 1 deletions
diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py
index 2346a826c2..6e8ca01261 100644
--- a/tests/forms_tests/tests/test_formsets.py
+++ b/tests/forms_tests/tests/test_formsets.py
@@ -6,7 +6,7 @@ from django.forms import (
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 @@ class TestEmptyFormSet(SimpleTestCase):
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)