| 1 |
import copy |
| 2 |
|
| 3 |
from django.forms.widgets import ChoiceWidget |
| 4 |
|
| 5 |
from .base import WidgetTest |
| 6 |
|
| 7 |
|
| 8 |
class ChoiceWidgetTest(WidgetTest): |
| 9 |
widget = ChoiceWidget |
| 10 |
|
| 11 |
@property |
| 12 |
def nested_widgets(self): |
| 13 |
nested_widget = self.widget( |
| 14 |
choices=( |
| 15 |
("outer1", "Outer 1"), |
| 16 |
('Group "1"', (("inner1", "Inner 1"), ("inner2", "Inner 2"))), |
| 17 |
), |
| 18 |
) |
| 19 |
nested_widget_dict = self.widget( |
| 20 |
choices={ |
| 21 |
"outer1": "Outer 1", |
| 22 |
'Group "1"': {"inner1": "Inner 1", "inner2": "Inner 2"}, |
| 23 |
}, |
| 24 |
) |
| 25 |
nested_widget_dict_tuple = self.widget( |
| 26 |
choices={ |
| 27 |
"outer1": "Outer 1", |
| 28 |
'Group "1"': (("inner1", "Inner 1"), ("inner2", "Inner 2")), |
| 29 |
}, |
| 30 |
) |
| 31 |
return (nested_widget, nested_widget_dict, nested_widget_dict_tuple) |
| 32 |
|
| 33 |
def test_deepcopy(self): |
| 34 |
""" |
| 35 |
__deepcopy__() should copy all attributes properly. |
| 36 |
""" |
| 37 |
widget = self.widget() |
| 38 |
obj = copy.deepcopy(widget) |
| 39 |
self.assertIsNot(widget, obj) |
| 40 |
self.assertEqual(widget.choices, obj.choices) |
| 41 |
self.assertIsNot(widget.choices, obj.choices) |
| 42 |
self.assertEqual(widget.attrs, obj.attrs) |
| 43 |
self.assertIsNot(widget.attrs, obj.attrs) |
| 44 |
|
| 45 |
def test_options(self): |
| 46 |
options = list( |
| 47 |
self.widget(choices=self.beatles).options( |
| 48 |
"name", |
| 49 |
["J"], |
| 50 |
attrs={"class": "super"}, |
| 51 |
) |
| 52 |
) |
| 53 |
self.assertEqual(len(options), 4) |
| 54 |
self.assertEqual(options[0]["name"], "name") |
| 55 |
self.assertEqual(options[0]["value"], "J") |
| 56 |
self.assertEqual(options[0]["label"], "John") |
| 57 |
self.assertEqual(options[0]["index"], "0") |
| 58 |
self.assertIs(options[0]["selected"], True) |
| 59 |
# Template-related attributes |
| 60 |
self.assertEqual(options[1]["name"], "name") |
| 61 |
self.assertEqual(options[1]["value"], "P") |
| 62 |
self.assertEqual(options[1]["label"], "Paul") |
| 63 |
self.assertEqual(options[1]["index"], "1") |
| 64 |
self.assertIs(options[1]["selected"], False) |
| 65 |
|
| 66 |
def test_optgroups_integer_choices(self): |
| 67 |
"""The option 'value' is the same type as what's in `choices`.""" |
| 68 |
groups = list( |
| 69 |
self.widget(choices=[[0, "choice text"]]).optgroups("name", ["vhs"]) |
| 70 |
) |
| 71 |
label, options, index = groups[0] |
| 72 |
self.assertEqual(options[0]["value"], 0) |
| 73 |
|
| 74 |
def test_renders_required_when_possible_to_select_empty_field_none(self): |
| 75 |
widget = self.widget(choices=[(None, "select please"), ("P", "Paul")]) |
| 76 |
self.assertIs(widget.use_required_attribute(initial=None), True) |
| 77 |
|
| 78 |
def test_renders_required_when_possible_to_select_empty_field_list(self): |
| 79 |
widget = self.widget(choices=[["", "select please"], ["P", "Paul"]]) |
| 80 |
self.assertIs(widget.use_required_attribute(initial=None), True) |
| 81 |
|
| 82 |
def test_renders_required_when_possible_to_select_empty_field_str(self): |
| 83 |
widget = self.widget(choices=[("", "select please"), ("P", "Paul")]) |
| 84 |
self.assertIs(widget.use_required_attribute(initial=None), True) |