Debian Codegraph @

tests/forms_tests/widget_tests/test_selectmultiple.py

raw
1 from django.forms import ChoiceField, Form, SelectMultiple
2
3 from .base import WidgetTest
4
5
6 class SelectMultipleTest(WidgetTest):
7 widget = SelectMultiple
8 numeric_choices = (("0", "0"), ("1", "1"), ("2", "2"), ("3", "3"), ("0", "extra"))
9
10 def test_format_value(self):
11 widget = self.widget(choices=self.numeric_choices)
12 self.assertEqual(widget.format_value(None), [])
13 self.assertEqual(widget.format_value(""), [""])
14 self.assertEqual(widget.format_value([3, 0, 1]), ["3", "0", "1"])
15
16 def test_render_selected(self):
17 self.check_html(
18 self.widget(choices=self.beatles),
19 "beatles",
20 ["J"],
21 html=("""<select multiple name="beatles">
22 <option value="J" selected>John</option>
23 <option value="P">Paul</option>
24 <option value="G">George</option>
25 <option value="R">Ringo</option>
26 </select>"""),
27 )
28
29 def test_render_multiple_selected(self):
30 self.check_html(
31 self.widget(choices=self.beatles),
32 "beatles",
33 ["J", "P"],
34 html=("""<select multiple name="beatles">
35 <option value="J" selected>John</option>
36 <option value="P" selected>Paul</option>
37 <option value="G">George</option>
38 <option value="R">Ringo</option>
39 </select>"""),
40 )
41
42 def test_render_none(self):
43 """
44 If the value is None, none of the options are selected, even if the
45 choices have an empty option.
46 """
47 self.check_html(
48 self.widget(choices=(("", "Unknown"),) + self.beatles),
49 "beatles",
50 None,
51 html=("""<select multiple name="beatles">
52 <option value="">Unknown</option>
53 <option value="J">John</option>
54 <option value="P">Paul</option>
55 <option value="G">George</option>
56 <option value="R">Ringo</option>
57 </select>"""),
58 )
59
60 def test_render_value_label(self):
61 """
62 If the value corresponds to a label (but not to an option value), none
63 of the options are selected.
64 """
65 self.check_html(
66 self.widget(choices=self.beatles),
67 "beatles",
68 ["John"],
69 html=("""<select multiple name="beatles">
70 <option value="J">John</option>
71 <option value="P">Paul</option>
72 <option value="G">George</option>
73 <option value="R">Ringo</option>
74 </select>"""),
75 )
76
77 def test_multiple_options_same_value(self):
78 """
79 Multiple options with the same value can be selected (#8103).
80 """
81 self.check_html(
82 self.widget(choices=self.numeric_choices),
83 "choices",
84 ["0"],
85 html=("""<select multiple name="choices">
86 <option value="0" selected>0</option>
87 <option value="1">1</option>
88 <option value="2">2</option>
89 <option value="3">3</option>
90 <option value="0" selected>extra</option>
91 </select>"""),
92 )
93
94 def test_multiple_values_invalid(self):
95 """
96 If multiple values are given, but some of them are not valid, the valid
97 ones are selected.
98 """
99 self.check_html(
100 self.widget(choices=self.beatles),
101 "beatles",
102 ["J", "G", "foo"],
103 html=("""<select multiple name="beatles">
104 <option value="J" selected>John</option>
105 <option value="P">Paul</option>
106 <option value="G" selected>George</option>
107 <option value="R">Ringo</option>
108 </select>"""),
109 )
110
111 def test_compare_string(self):
112 choices = [("1", "1"), ("2", "2"), ("3", "3")]
113
114 self.check_html(
115 self.widget(choices=choices),
116 "nums",
117 [2],
118 html=("""<select multiple name="nums">
119 <option value="1">1</option>
120 <option value="2" selected>2</option>
121 <option value="3">3</option>
122 </select>"""),
123 )
124
125 self.check_html(
126 self.widget(choices=choices),
127 "nums",
128 ["2"],
129 html=("""<select multiple name="nums">
130 <option value="1">1</option>
131 <option value="2" selected>2</option>
132 <option value="3">3</option>
133 </select>"""),
134 )
135
136 self.check_html(
137 self.widget(choices=choices),
138 "nums",
139 [2],
140 html=("""<select multiple name="nums">
141 <option value="1">1</option>
142 <option value="2" selected>2</option>
143 <option value="3">3</option>
144 </select>"""),
145 )
146
147 def test_optgroup_select_multiple(self):
148 widget = SelectMultiple(
149 choices=(
150 ("outer1", "Outer 1"),
151 ('Group "1"', (("inner1", "Inner 1"), ("inner2", "Inner 2"))),
152 )
153 )
154 self.check_html(
155 widget,
156 "nestchoice",
157 ["outer1", "inner2"],
158 html=("""<select multiple name="nestchoice">
159 <option value="outer1" selected>Outer 1</option>
160 <optgroup label="Group &quot;1&quot;">
161 <option value="inner1">Inner 1</option>
162 <option value="inner2" selected>Inner 2</option>
163 </optgroup>
164 </select>"""),
165 )
166
167 def test_value_omitted_from_data(self):
168 widget = self.widget(choices=self.beatles)
169 self.assertIs(widget.value_omitted_from_data({}, {}, "field"), False)
170 self.assertIs(
171 widget.value_omitted_from_data({"field": "value"}, {}, "field"), False
172 )
173
174 def test_fieldset(self):
175 class TestForm(Form):
176 template_name = "forms_tests/use_fieldset.html"
177 field = ChoiceField(
178 widget=self.widget, choices=self.beatles, required=False
179 )
180
181 form = TestForm()
182 self.assertIs(self.widget.use_fieldset, False)
183 self.assertHTMLEqual(
184 '<div><label for="id_field">Field:</label>'
185 '<select multiple name="field" id="id_field">'
186 '<option value="J">John</option> <option value="P">Paul</option>'
187 '<option value="G">George</option><option value="R">Ringo'
188 "</option></select></div>",
189 form.render(),
190 )