2020-02-12 13:48:49 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2020-10-17 14:17:00 +00:00
|
|
|
from django.db import models
|
2020-02-12 13:48:49 +00:00
|
|
|
from django.forms import ChoiceField, Form
|
2017-02-17 21:04:45 +00:00
|
|
|
from django.test import SimpleTestCase
|
2016-04-09 17:17:34 +00:00
|
|
|
|
|
|
|
from . import FormFieldAssertionsMixin
|
|
|
|
|
|
|
|
|
|
|
|
class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
|
|
|
|
def test_choicefield_1(self):
|
|
|
|
f = ChoiceField(choices=[("1", "One"), ("2", "Two")])
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean("")
|
|
|
|
with self.assertRaisesMessage(ValidationError, "'This field is required.'"):
|
|
|
|
f.clean(None)
|
|
|
|
self.assertEqual("1", f.clean(1))
|
|
|
|
self.assertEqual("1", f.clean("1"))
|
|
|
|
msg = "'Select a valid choice. 3 is not one of the available choices.'"
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
f.clean("3")
|
|
|
|
|
|
|
|
def test_choicefield_2(self):
|
|
|
|
f = ChoiceField(choices=[("1", "One"), ("2", "Two")], required=False)
|
|
|
|
self.assertEqual("", f.clean(""))
|
|
|
|
self.assertEqual("", f.clean(None))
|
|
|
|
self.assertEqual("1", f.clean(1))
|
|
|
|
self.assertEqual("1", f.clean("1"))
|
|
|
|
msg = "'Select a valid choice. 3 is not one of the available choices.'"
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
f.clean("3")
|
|
|
|
|
|
|
|
def test_choicefield_3(self):
|
|
|
|
f = ChoiceField(choices=[("J", "John"), ("P", "Paul")])
|
|
|
|
self.assertEqual("J", f.clean("J"))
|
|
|
|
msg = "'Select a valid choice. John is not one of the available choices.'"
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
f.clean("John")
|
|
|
|
|
|
|
|
def test_choicefield_4(self):
|
|
|
|
f = ChoiceField(
|
|
|
|
choices=[
|
|
|
|
("Numbers", (("1", "One"), ("2", "Two"))),
|
|
|
|
("Letters", (("3", "A"), ("4", "B"))),
|
|
|
|
("5", "Other"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.assertEqual("1", f.clean(1))
|
|
|
|
self.assertEqual("1", f.clean("1"))
|
|
|
|
self.assertEqual("3", f.clean(3))
|
|
|
|
self.assertEqual("3", f.clean("3"))
|
|
|
|
self.assertEqual("5", f.clean(5))
|
|
|
|
self.assertEqual("5", f.clean("5"))
|
|
|
|
msg = "'Select a valid choice. 6 is not one of the available choices.'"
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
f.clean("6")
|
|
|
|
|
2017-06-03 07:50:14 +00:00
|
|
|
def test_choicefield_choices_default(self):
|
|
|
|
f = ChoiceField()
|
|
|
|
self.assertEqual(f.choices, [])
|
|
|
|
|
2016-04-09 17:17:34 +00:00
|
|
|
def test_choicefield_callable(self):
|
|
|
|
def choices():
|
|
|
|
return [("J", "John"), ("P", "Paul")]
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-04-09 17:17:34 +00:00
|
|
|
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))
|
|
|
|
|
|
|
|
def test_choicefield_disabled(self):
|
|
|
|
f = ChoiceField(choices=[("J", "John"), ("P", "Paul")], disabled=True)
|
|
|
|
self.assertWidgetRendersTo(
|
|
|
|
f,
|
2016-11-05 11:27:44 +00:00
|
|
|
'<select id="id_f" name="f" disabled><option value="J">John</option>'
|
2016-04-09 17:17:34 +00:00
|
|
|
'<option value="P">Paul</option></select>',
|
|
|
|
)
|
2020-10-17 14:17:00 +00:00
|
|
|
|
|
|
|
def test_choicefield_enumeration(self):
|
|
|
|
class FirstNames(models.TextChoices):
|
|
|
|
JOHN = "J", "John"
|
|
|
|
PAUL = "P", "Paul"
|
|
|
|
|
2023-03-10 23:34:13 +00:00
|
|
|
f = ChoiceField(choices=FirstNames)
|
|
|
|
self.assertEqual(f.choices, FirstNames.choices)
|
2020-10-17 14:17:00 +00:00
|
|
|
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")
|