summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-12-05 04:47:19 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-12-05 04:47:19 +0000
commit4a1f2129d09658e705fbe0660275c6efccf1474a (patch)
tree196aec05eddaacbe7b4257d20ae3b4264f035b2c /tests
parentee48da24050a4f447f6ba153430c59b93a1522b3 (diff)
Fixed #12398 -- Added a TypedMultipleChoiceField. Thanks to Tai Lee.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14829 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/forms/tests/fields.py44
1 files changed, 43 insertions, 1 deletions
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
index bc549a9848..93ca5c1762 100644
--- a/tests/regressiontests/forms/tests/fields.py
+++ b/tests/regressiontests/forms/tests/fields.py
@@ -750,7 +750,49 @@ class FieldsTests(TestCase):
self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['6'])
self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 6 is not one of the available choices.']", f.clean, ['1','6'])
- # ComboField ##################################################################
+ # TypedMultipleChoiceField ############################################################
+ # TypedMultipleChoiceField is just like MultipleChoiceField, except that coerced types
+ # will be returned:
+
+ def test_typedmultiplechoicefield_1(self):
+ f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
+ self.assertEqual([1], f.clean(['1']))
+ self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['2'])
+
+ def test_typedmultiplechoicefield_2(self):
+ # Different coercion, same validation.
+ f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=float)
+ self.assertEqual([1.0], f.clean(['1']))
+
+ def test_typedmultiplechoicefield_3(self):
+ # This can also cause weirdness: be careful (bool(-1) == True, remember)
+ f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=bool)
+ self.assertEqual([True], f.clean(['-1']))
+
+ def test_typedmultiplechoicefield_4(self):
+ f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int)
+ self.assertEqual([1, -1], f.clean(['1','-1']))
+ self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. 2 is not one of the available choices.']", f.clean, ['1','2'])
+
+ def test_typedmultiplechoicefield_5(self):
+ # Even more weirdness: if you have a valid choice but your coercion function
+ # can't coerce, you'll still get a validation error. Don't do this!
+ f = TypedMultipleChoiceField(choices=[('A', 'A'), ('B', 'B')], coerce=int)
+ self.assertRaisesErrorWithMessage(ValidationError, "[u'Select a valid choice. B is not one of the available choices.']", f.clean, ['B'])
+ # Required fields require values
+ self.assertRaisesErrorWithMessage(ValidationError, "[u'This field is required.']", f.clean, [])
+
+ def test_typedmultiplechoicefield_6(self):
+ # Non-required fields aren't required
+ f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False)
+ self.assertEqual([], f.clean([]))
+
+ def test_typedmultiplechoicefield_7(self):
+ # If you want cleaning an empty value to return a different type, tell the field
+ f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=False, empty_value=None)
+ self.assertEqual(None, f.clean([]))
+
+ # ComboField ##################################################################
def test_combofield_1(self):
f = ComboField(fields=[CharField(max_length=20), EmailField()])