Fixed #32110 -- Doc'd and tested enumerations for ChoiceField.choices.

This commit is contained in:
Claude Paroz 2020-10-17 16:17:00 +02:00 committed by Mariusz Felisiak
parent af87574a3c
commit 7f85498eef
2 changed files with 19 additions and 6 deletions

View file

@ -1,4 +1,5 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.forms import ChoiceField, Form
from django.test import SimpleTestCase
@ -87,3 +88,14 @@ class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
'<select id="id_f" name="f" disabled><option value="J">John</option>'
'<option value="P">Paul</option></select>'
)
def test_choicefield_enumeration(self):
class FirstNames(models.TextChoices):
JOHN = 'J', 'John'
PAUL = 'P', 'Paul'
f = ChoiceField(choices=FirstNames.choices)
self.assertEqual(f.clean('J'), 'J')
msg = "'Select a valid choice. 3 is not one of the available choices.'"
with self.assertRaisesMessage(ValidationError, msg):
f.clean('3')