Debian Codegraph @

tests/forms_tests/widget_tests/test_select.py

raw
1 import datetime
2
3 from django.forms import ChoiceField, Form, MultiWidget, Select, TextInput
4 from django.test import override_settings
5 from django.utils.safestring import mark_safe
6
7 from .test_choicewidget import ChoiceWidgetTest
8
9
10 class SelectTest(ChoiceWidgetTest):
11 widget = Select
12
13 def test_render(self):
14 html = """
15 <select name="beatle">
16 <option value="J" selected>John</option>
17 <option value="P">Paul</option>
18 <option value="G">George</option>
19 <option value="R">Ringo</option>
20 </select>
21 """
22 for choices in (self.beatles, dict(self.beatles)):
23 with self.subTest(choices):
24 self.check_html(self.widget(choices=choices), "beatle", "J", html=html)
25
26 def test_render_none(self):
27 """
28 If the value is None, none of the options are selected.
29 """
30 self.check_html(
31 self.widget(choices=self.beatles),
32 "beatle",
33 None,
34 html=("""<select name="beatle">
35 <option value="J">John</option>
36 <option value="P">Paul</option>
37 <option value="G">George</option>
38 <option value="R">Ringo</option>
39 </select>"""),
40 )
41
42 def test_render_label_value(self):
43 """
44 If the value corresponds to a label (but not to an option value), none
45 of the options are selected.
46 """
47 self.check_html(
48 self.widget(choices=self.beatles),
49 "beatle",
50 "John",
51 html=("""<select name="beatle">
52 <option value="J">John</option>
53 <option value="P">Paul</option>
54 <option value="G">George</option>
55 <option value="R">Ringo</option>
56 </select>"""),
57 )
58
59 def test_render_selected(self):
60 """
61 Only one option can be selected (#8103).
62 """
63 choices = [("0", "0"), ("1", "1"), ("2", "2"), ("3", "3"), ("0", "extra")]
64
65 self.check_html(
66 self.widget(choices=choices),
67 "choices",
68 "0",
69 html=("""<select name="choices">
70 <option value="0" selected>0</option>
71 <option value="1">1</option>
72 <option value="2">2</option>
73 <option value="3">3</option>
74 <option value="0">extra</option>
75 </select>"""),
76 )
77
78 def test_constructor_attrs(self):
79 """
80 Select options shouldn't inherit the parent widget attrs.
81 """
82 widget = Select(
83 attrs={"class": "super", "id": "super"},
84 choices=[(1, 1), (2, 2), (3, 3)],
85 )
86 self.check_html(
87 widget,
88 "num",
89 2,
90 html=("""<select name="num" class="super" id="super">
91 <option value="1">1</option>
92 <option value="2" selected>2</option>
93 <option value="3">3</option>
94 </select>"""),
95 )
96
97 def test_compare_to_str(self):
98 """
99 The value is compared to its str().
100 """
101 self.check_html(
102 self.widget(choices=[("1", "1"), ("2", "2"), ("3", "3")]),
103 "num",
104 2,
105 html=("""<select name="num">
106 <option value="1">1</option>
107 <option value="2" selected>2</option>
108 <option value="3">3</option>
109 </select>"""),
110 )
111 self.check_html(
112 self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
113 "num",
114 "2",
115 html=("""<select name="num">
116 <option value="1">1</option>
117 <option value="2" selected>2</option>
118 <option value="3">3</option>
119 </select>"""),
120 )
121 self.check_html(
122 self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
123 "num",
124 2,
125 html=("""<select name="num">
126 <option value="1">1</option>
127 <option value="2" selected>2</option>
128 <option value="3">3</option>
129 </select>"""),
130 )
131
132 def test_choices_constructor(self):
133 widget = Select(choices=[(1, 1), (2, 2), (3, 3)])
134 self.check_html(
135 widget,
136 "num",
137 2,
138 html=("""<select name="num">
139 <option value="1">1</option>
140 <option value="2" selected>2</option>
141 <option value="3">3</option>
142 </select>"""),
143 )
144
145 def test_choices_constructor_generator(self):
146 """
147 If choices is passed to the constructor and is a generator, it can be
148 iterated over multiple times without getting consumed.
149 """
150
151 def get_choices():
152 for i in range(5):
153 yield (i, i)
154
155 widget = Select(choices=get_choices())
156 self.check_html(
157 widget,
158 "num",
159 2,
160 html=("""<select name="num">
161 <option value="0">0</option>
162 <option value="1">1</option>
163 <option value="2" selected>2</option>
164 <option value="3">3</option>
165 <option value="4">4</option>
166 </select>"""),
167 )
168 self.check_html(
169 widget,
170 "num",
171 3,
172 html=("""<select name="num">
173 <option value="0">0</option>
174 <option value="1">1</option>
175 <option value="2">2</option>
176 <option value="3" selected>3</option>
177 <option value="4">4</option>
178 </select>"""),
179 )
180
181 def test_choices_escaping(self):
182 choices = (("bad", "you & me"), ("good", mark_safe("you &gt; me")))
183 self.check_html(
184 self.widget(choices=choices),
185 "escape",
186 None,
187 html=("""<select name="escape">
188 <option value="bad">you &amp; me</option>
189 <option value="good">you &gt; me</option>
190 </select>"""),
191 )
192
193 def test_choices_unicode(self):
194 self.check_html(
195 self.widget(choices=[("ŠĐĆŽćžšđ", "ŠĐabcĆŽćžšđ"), ("ćžšđ", "abcćžšđ")]),
196 "email",
197 "ŠĐĆŽćžšđ",
198 html=("""
199 <select name="email">
200 <option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111"
201 selected>
202 \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111
203 </option>
204 <option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111
205 </option>
206 </select>
207 """),
208 )
209
210 def test_choices_optgroup(self):
211 """
212 Choices can be nested one level in order to create HTML optgroups.
213 """
214 html = """
215 <select name="nestchoice">
216 <option value="outer1">Outer 1</option>
217 <optgroup label="Group &quot;1&quot;">
218 <option value="inner1">Inner 1</option>
219 <option value="inner2">Inner 2</option>
220 </optgroup>
221 </select>
222 """
223 for widget in self.nested_widgets:
224 with self.subTest(widget):
225 self.check_html(widget, "nestchoice", None, html=html)
226
227 def test_choices_select_outer(self):
228 html = """
229 <select name="nestchoice">
230 <option value="outer1" selected>Outer 1</option>
231 <optgroup label="Group &quot;1&quot;">
232 <option value="inner1">Inner 1</option>
233 <option value="inner2">Inner 2</option>
234 </optgroup>
235 </select>
236 """
237 for widget in self.nested_widgets:
238 with self.subTest(widget):
239 self.check_html(widget, "nestchoice", "outer1", html=html)
240
241 def test_choices_select_inner(self):
242 html = """
243 <select name="nestchoice">
244 <option value="outer1">Outer 1</option>
245 <optgroup label="Group &quot;1&quot;">
246 <option value="inner1" selected>Inner 1</option>
247 <option value="inner2">Inner 2</option>
248 </optgroup>
249 </select>
250 """
251 for widget in self.nested_widgets:
252 with self.subTest(widget):
253 self.check_html(widget, "nestchoice", "inner1", html=html)
254
255 @override_settings(USE_THOUSAND_SEPARATOR=True)
256 def test_doesnt_localize_option_value(self):
257 choices = [
258 (1, "One"),
259 (1000, "One thousand"),
260 (1000000, "One million"),
261 ]
262 html = """
263 <select name="number">
264 <option value="1">One</option>
265 <option value="1000">One thousand</option>
266 <option value="1000000">One million</option>
267 </select>
268 """
269 self.check_html(self.widget(choices=choices), "number", None, html=html)
270
271 choices = [
272 (datetime.time(0, 0), "midnight"),
273 (datetime.time(12, 0), "noon"),
274 ]
275 html = """
276 <select name="time">
277 <option value="00:00:00">midnight</option>
278 <option value="12:00:00">noon</option>
279 </select>
280 """
281 self.check_html(self.widget(choices=choices), "time", None, html=html)
282
283 def _test_optgroups(self, choices):
284 groups = list(
285 self.widget(choices=choices).optgroups(
286 "name",
287 ["vhs"],
288 attrs={"class": "super"},
289 )
290 )
291 audio, video, unknown = groups
292 label, options, index = audio
293 self.assertEqual(label, "Audio")
294 self.assertEqual(
295 options,
296 [
297 {
298 "value": "vinyl",
299 "type": "select",
300 "attrs": {},
301 "index": "0_0",
302 "label": "Vinyl",
303 "template_name": "django/forms/widgets/select_option.html",
304 "name": "name",
305 "selected": False,
306 "wrap_label": True,
307 },
308 {
309 "value": "cd",
310 "type": "select",
311 "attrs": {},
312 "index": "0_1",
313 "label": "CD",
314 "template_name": "django/forms/widgets/select_option.html",
315 "name": "name",
316 "selected": False,
317 "wrap_label": True,
318 },
319 ],
320 )
321 self.assertEqual(index, 0)
322 label, options, index = video
323 self.assertEqual(label, "Video")
324 self.assertEqual(
325 options,
326 [
327 {
328 "value": "vhs",
329 "template_name": "django/forms/widgets/select_option.html",
330 "label": "VHS Tape",
331 "attrs": {"selected": True},
332 "index": "1_0",
333 "name": "name",
334 "selected": True,
335 "type": "select",
336 "wrap_label": True,
337 },
338 {
339 "value": "dvd",
340 "template_name": "django/forms/widgets/select_option.html",
341 "label": "DVD",
342 "attrs": {},
343 "index": "1_1",
344 "name": "name",
345 "selected": False,
346 "type": "select",
347 "wrap_label": True,
348 },
349 ],
350 )
351 self.assertEqual(index, 1)
352 label, options, index = unknown
353 self.assertIsNone(label)
354 self.assertEqual(
355 options,
356 [
357 {
358 "value": "unknown",
359 "selected": False,
360 "template_name": "django/forms/widgets/select_option.html",
361 "label": "Unknown",
362 "attrs": {},
363 "index": "2",
364 "name": "name",
365 "type": "select",
366 "wrap_label": True,
367 }
368 ],
369 )
370 self.assertEqual(index, 2)
371
372 def test_optgroups(self):
373 choices_dict = {
374 "Audio": [
375 ("vinyl", "Vinyl"),
376 ("cd", "CD"),
377 ],
378 "Video": [
379 ("vhs", "VHS Tape"),
380 ("dvd", "DVD"),
381 ],
382 "unknown": "Unknown",
383 }
384 choices_list = list(choices_dict.items())
385 choices_nested_dict = {
386 k: dict(v) if isinstance(v, list) else v for k, v in choices_dict.items()
387 }
388
389 for choices in (choices_dict, choices_list, choices_nested_dict):
390 with self.subTest(choices):
391 self._test_optgroups(choices)
392
393 def test_doesnt_render_required_when_impossible_to_select_empty_field(self):
394 widget = self.widget(choices=[("J", "John"), ("P", "Paul")])
395 self.assertIs(widget.use_required_attribute(initial=None), False)
396
397 def test_doesnt_render_required_when_no_choices_are_available(self):
398 widget = self.widget(choices=[])
399 self.assertIs(widget.use_required_attribute(initial=None), False)
400
401 def test_render_as_subwidget(self):
402 """A RadioSelect as a subwidget of MultiWidget."""
403 choices = (("", "------"),) + self.beatles
404 self.check_html(
405 MultiWidget([self.widget(choices=choices), TextInput()]),
406 "beatle",
407 ["J", "Some text"],
408 html=("""
409 <select name="beatle_0">
410 <option value="">------</option>
411 <option value="J" selected>John</option>
412 <option value="P">Paul</option>
413 <option value="G">George</option>
414 <option value="R">Ringo</option>
415 </select>
416 <input name="beatle_1" type="text" value="Some text">
417 """),
418 )
419
420 def test_fieldset(self):
421 class TestForm(Form):
422 template_name = "forms_tests/use_fieldset.html"
423 field = ChoiceField(widget=self.widget, choices=self.beatles)
424
425 form = TestForm()
426 self.assertIs(self.widget.use_fieldset, False)
427 self.assertHTMLEqual(
428 '<div><label for="id_field">Field:</label>'
429 '<select name="field" id="id_field">'
430 '<option value="J">John</option> '
431 '<option value="P">Paul</option>'
432 '<option value="G">George</option>'
433 '<option value="R">Ringo</option></select></div>',
434 form.render(),
435 )