diff options
| author | Claude Paroz <claude@2xlibre.net> | 2013-11-18 18:24:56 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2013-11-18 18:24:56 +0100 |
| commit | a0f3eeccf3d5a90f9914bfe20b15df05673ea59d (patch) | |
| tree | da91c208646cdef2a8f1dd535ab5bcd07d566a52 /tests | |
| parent | 4a00f132e0cc5246f7e7bd04b6d84a9d9ea4a0c1 (diff) | |
Fixed #21397 -- Re-added flexibility to TypedChoiceField coercion
Thanks Elec for the report and Simon Charette for the review.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/forms_tests/tests/test_fields.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index a7e124a50c..fe11370bde 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -956,6 +956,22 @@ class FieldsTests(SimpleTestCase): f = TypedChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True) self.assertFalse(f._has_changed(None, '')) + def test_typedchoicefield_special_coerce(self): + """ + Test a coerce function which results in a value not present in choices. + Refs #21397. + """ + def coerce_func(val): + return Decimal('1.%s' % val) + + f = TypedChoiceField(choices=[(1, "1"), (2, "2")], coerce=coerce_func, required=True) + self.assertEqual(Decimal('1.2'), f.clean('2')) + self.assertRaisesMessage(ValidationError, + "'This field is required.'", f.clean, '') + self.assertRaisesMessage(ValidationError, + "'Select a valid choice. 3 is not one of the available choices.'", + f.clean, '3') + # NullBooleanField ############################################################ def test_nullbooleanfield_1(self): @@ -1110,6 +1126,23 @@ class FieldsTests(SimpleTestCase): f = TypedMultipleChoiceField(choices=[(1, "+1"), (-1, "-1")], coerce=int, required=True) self.assertFalse(f._has_changed(None, '')) + def test_typedmultiplechoicefield_special_coerce(self): + """ + Test a coerce function which results in a value not present in choices. + Refs #21397. + """ + def coerce_func(val): + return Decimal('1.%s' % val) + + f = TypedMultipleChoiceField( + choices=[(1, "1"), (2, "2")], coerce=coerce_func, required=True) + self.assertEqual([Decimal('1.2')], f.clean(['2'])) + self.assertRaisesMessage(ValidationError, + "'This field is required.'", f.clean, []) + self.assertRaisesMessage(ValidationError, + "'Select a valid choice. 3 is not one of the available choices.'", + f.clean, ['3']) + # ComboField ################################################################## def test_combofield_1(self): |
