diff options
| author | Peter Inglesby <peter.inglesby@gmail.com> | 2014-10-27 20:21:59 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-11-04 11:23:58 -0500 |
| commit | 74e1980cf96eb45079bef464fabdcbe0a6db2423 (patch) | |
| tree | 0541e693623c87c9789f1beb37d7f5831426c74b /tests/forms_tests | |
| parent | e0685368c6b122404cf0817d5cb17ab1b211cf6d (diff) | |
Fixed #13181 -- Added support for callable choices to forms.ChoiceField
Thanks vanschelven and expleo for the initial patch.
Diffstat (limited to 'tests/forms_tests')
| -rw-r--r-- | tests/forms_tests/tests/test_fields.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py index 22af2cf23d..3caf902349 100644 --- a/tests/forms_tests/tests/test_fields.py +++ b/tests/forms_tests/tests/test_fields.py @@ -961,6 +961,28 @@ class FieldsTests(SimpleTestCase): self.assertEqual('5', f.clean('5')) self.assertRaisesMessage(ValidationError, "'Select a valid choice. 6 is not one of the available choices.'", f.clean, '6') + def test_choicefield_callable(self): + choices = lambda: [('J', 'John'), ('P', 'Paul')] + f = ChoiceField(choices=choices) + self.assertEqual('J', f.clean('J')) + + def test_choicefield_callable_may_evaluate_to_different_values(self): + choices = [] + + def choices_as_callable(): + return choices + + class ChoiceFieldForm(Form): + choicefield = ChoiceField(choices=choices_as_callable) + + choices = [('J', 'John')] + form = ChoiceFieldForm() + self.assertEqual([('J', 'John')], list(form.fields['choicefield'].choices)) + + choices = [('P', 'Paul')] + form = ChoiceFieldForm() + self.assertEqual([('P', 'Paul')], list(form.fields['choicefield'].choices)) + # TypedChoiceField ############################################################ # TypedChoiceField is just like ChoiceField, except that coerced types will # be returned: |
