1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
from django.core.exceptions import ValidationError
from django.db import models
from django.forms import ChoiceField, Form
from django.test import SimpleTestCase
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")
def test_choicefield_choices_default(self):
f = ChoiceField()
self.assertEqual(f.choices, [])
def test_choicefield_callable(self):
def choices():
return [("J", "John"), ("P", "Paul")]
f = ChoiceField(choices=choices)
self.assertEqual("J", f.clean("J"))
def test_choicefield_callable_mapping(self):
def choices():
return {"J": "John", "P": "Paul"}
f = ChoiceField(choices=choices)
self.assertEqual("J", f.clean("J"))
def test_choicefield_callable_grouped_mapping(self):
def choices():
return {
"Numbers": {"1": "One", "2": "Two"},
"Letters": {"3": "A", "4": "B"},
}
f = ChoiceField(choices=choices)
for i in ("1", "2", "3", "4"):
with self.subTest(i):
self.assertEqual(i, f.clean(i))
def test_choicefield_mapping(self):
f = ChoiceField(choices={"J": "John", "P": "Paul"})
self.assertEqual("J", f.clean("J"))
def test_choicefield_grouped_mapping(self):
f = ChoiceField(
choices={
"Numbers": (("1", "One"), ("2", "Two")),
"Letters": (("3", "A"), ("4", "B")),
}
)
for i in ("1", "2", "3", "4"):
with self.subTest(i):
self.assertEqual(i, f.clean(i))
def test_choicefield_grouped_mapping_inner_dict(self):
f = ChoiceField(
choices={
"Numbers": {"1": "One", "2": "Two"},
"Letters": {"3": "A", "4": "B"},
}
)
for i in ("1", "2", "3", "4"):
with self.subTest(i):
self.assertEqual(i, f.clean(i))
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(choices, list(form.fields["choicefield"].choices))
self.assertEqual(choices, list(form.fields["choicefield"].widget.choices))
choices = [("P", "Paul")]
form = ChoiceFieldForm()
self.assertEqual(choices, list(form.fields["choicefield"].choices))
self.assertEqual(choices, list(form.fields["choicefield"].widget.choices))
def test_choicefield_disabled(self):
f = ChoiceField(choices=[("J", "John"), ("P", "Paul")], disabled=True)
self.assertWidgetRendersTo(
f,
'<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)
self.assertEqual(f.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")
|