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