| 1 |
import datetime |
| 2 |
|
| 3 |
from django.forms import ChoiceField, Form, MultiWidget, RadioSelect, 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 |
BLANK_CHOICE_DASH = (("", "------"),) |
| 10 |
|
| 11 |
|
| 12 |
class RadioSelectTest(ChoiceWidgetTest): |
| 13 |
widget = RadioSelect |
| 14 |
|
| 15 |
def test_render(self): |
| 16 |
html = """ |
| 17 |
<div> |
| 18 |
<div> |
| 19 |
<label><input type="radio" name="beatle" value="">------</label> |
| 20 |
</div> |
| 21 |
<div> |
| 22 |
<label><input checked type="radio" name="beatle" value="J">John</label> |
| 23 |
</div> |
| 24 |
<div> |
| 25 |
<label><input type="radio" name="beatle" value="P">Paul</label> |
| 26 |
</div> |
| 27 |
<div> |
| 28 |
<label><input type="radio" name="beatle" value="G">George</label> |
| 29 |
</div> |
| 30 |
<div> |
| 31 |
<label><input type="radio" name="beatle" value="R">Ringo</label> |
| 32 |
</div> |
| 33 |
</div> |
| 34 |
""" |
| 35 |
beatles_with_blank = BLANK_CHOICE_DASH + self.beatles |
| 36 |
for choices in (beatles_with_blank, dict(beatles_with_blank)): |
| 37 |
with self.subTest(choices): |
| 38 |
self.check_html(self.widget(choices=choices), "beatle", "J", html=html) |
| 39 |
|
| 40 |
def test_nested_choices(self): |
| 41 |
nested_choices = ( |
| 42 |
("unknown", "Unknown"), |
| 43 |
("Audio", (("vinyl", "Vinyl"), ("cd", "CD"))), |
| 44 |
("Video", (("vhs", "VHS"), ("dvd", "DVD"))), |
| 45 |
) |
| 46 |
html = """ |
| 47 |
<div id="media"> |
| 48 |
<div> |
| 49 |
<label for="media_0"> |
| 50 |
<input type="radio" name="nestchoice" value="unknown" id="media_0"> Unknown |
| 51 |
</label></div> |
| 52 |
<div> |
| 53 |
<label>Audio</label> |
| 54 |
<div> |
| 55 |
<label for="media_1_0"> |
| 56 |
<input type="radio" name="nestchoice" value="vinyl" id="media_1_0"> Vinyl |
| 57 |
</label></div> |
| 58 |
<div> <label for="media_1_1"> |
| 59 |
<input type="radio" name="nestchoice" value="cd" id="media_1_1"> CD |
| 60 |
</label></div> |
| 61 |
</div><div> |
| 62 |
<label>Video</label> |
| 63 |
<div> |
| 64 |
<label for="media_2_0"> |
| 65 |
<input type="radio" name="nestchoice" value="vhs" id="media_2_0"> VHS |
| 66 |
</label></div> |
| 67 |
<div> |
| 68 |
<label for="media_2_1"> |
| 69 |
<input type="radio" name="nestchoice" value="dvd" id="media_2_1" checked> DVD |
| 70 |
</label></div> |
| 71 |
</div> |
| 72 |
</div> |
| 73 |
""" |
| 74 |
self.check_html( |
| 75 |
self.widget(choices=nested_choices), |
| 76 |
"nestchoice", |
| 77 |
"dvd", |
| 78 |
attrs={"id": "media"}, |
| 79 |
html=html, |
| 80 |
) |
| 81 |
|
| 82 |
def test_render_none(self): |
| 83 |
""" |
| 84 |
If value is None, none of the options are selected. |
| 85 |
""" |
| 86 |
choices = BLANK_CHOICE_DASH + self.beatles |
| 87 |
html = """ |
| 88 |
<div> |
| 89 |
<div> |
| 90 |
<label><input checked type="radio" name="beatle" value="">------</label> |
| 91 |
</div> |
| 92 |
<div> |
| 93 |
<label><input type="radio" name="beatle" value="J">John</label> |
| 94 |
</div> |
| 95 |
<div> |
| 96 |
<label><input type="radio" name="beatle" value="P">Paul</label> |
| 97 |
</div> |
| 98 |
<div> |
| 99 |
<label><input type="radio" name="beatle" value="G">George</label> |
| 100 |
</div> |
| 101 |
<div> |
| 102 |
<label><input type="radio" name="beatle" value="R">Ringo</label> |
| 103 |
</div> |
| 104 |
</div> |
| 105 |
""" |
| 106 |
self.check_html(self.widget(choices=choices), "beatle", None, html=html) |
| 107 |
|
| 108 |
def test_render_label_value(self): |
| 109 |
""" |
| 110 |
If the value corresponds to a label (but not to an option value), none |
| 111 |
of the options are selected. |
| 112 |
""" |
| 113 |
html = """ |
| 114 |
<div> |
| 115 |
<div> |
| 116 |
<label><input type="radio" name="beatle" value="J">John</label> |
| 117 |
</div> |
| 118 |
<div> |
| 119 |
<label><input type="radio" name="beatle" value="P">Paul</label> |
| 120 |
</div> |
| 121 |
<div> |
| 122 |
<label><input type="radio" name="beatle" value="G">George</label> |
| 123 |
</div> |
| 124 |
<div> |
| 125 |
<label><input type="radio" name="beatle" value="R">Ringo</label> |
| 126 |
</div> |
| 127 |
</div> |
| 128 |
""" |
| 129 |
self.check_html(self.widget(choices=self.beatles), "beatle", "Ringo", html=html) |
| 130 |
|
| 131 |
def test_render_selected(self): |
| 132 |
""" |
| 133 |
Only one option can be selected. |
| 134 |
""" |
| 135 |
choices = [("0", "0"), ("1", "1"), ("2", "2"), ("3", "3"), ("0", "extra")] |
| 136 |
html = """ |
| 137 |
<div> |
| 138 |
<div> |
| 139 |
<label><input checked type="radio" name="choices" value="0">0</label> |
| 140 |
</div> |
| 141 |
<div> |
| 142 |
<label><input type="radio" name="choices" value="1">1</label> |
| 143 |
</div> |
| 144 |
<div> |
| 145 |
<label><input type="radio" name="choices" value="2">2</label> |
| 146 |
</div> |
| 147 |
<div> |
| 148 |
<label><input type="radio" name="choices" value="3">3</label> |
| 149 |
</div> |
| 150 |
<div> |
| 151 |
<label><input type="radio" name="choices" value="0">extra</label> |
| 152 |
</div> |
| 153 |
</div> |
| 154 |
""" |
| 155 |
self.check_html(self.widget(choices=choices), "choices", "0", html=html) |
| 156 |
|
| 157 |
def test_constructor_attrs(self): |
| 158 |
""" |
| 159 |
Attributes provided at instantiation are passed to the constituent |
| 160 |
inputs. |
| 161 |
""" |
| 162 |
widget = self.widget(attrs={"id": "foo"}, choices=self.beatles) |
| 163 |
html = """ |
| 164 |
<div id="foo"> |
| 165 |
<div> |
| 166 |
<label for="foo_0"> |
| 167 |
<input checked type="radio" id="foo_0" value="J" name="beatle">John</label> |
| 168 |
</div> |
| 169 |
<div><label for="foo_1"> |
| 170 |
<input type="radio" id="foo_1" value="P" name="beatle">Paul</label> |
| 171 |
</div> |
| 172 |
<div><label for="foo_2"> |
| 173 |
<input type="radio" id="foo_2" value="G" name="beatle">George</label> |
| 174 |
</div> |
| 175 |
<div><label for="foo_3"> |
| 176 |
<input type="radio" id="foo_3" value="R" name="beatle">Ringo</label> |
| 177 |
</div> |
| 178 |
</div> |
| 179 |
""" |
| 180 |
self.check_html(widget, "beatle", "J", html=html) |
| 181 |
|
| 182 |
def test_compare_to_str(self): |
| 183 |
""" |
| 184 |
The value is compared to its str(). |
| 185 |
""" |
| 186 |
html = """ |
| 187 |
<div> |
| 188 |
<div> |
| 189 |
<label><input type="radio" name="num" value="1">1</label> |
| 190 |
</div> |
| 191 |
<div> |
| 192 |
<label><input type="radio" name="num" value="2">2</label> |
| 193 |
</div> |
| 194 |
<div> |
| 195 |
<label><input checked type="radio" name="num" value="3">3</label> |
| 196 |
</div> |
| 197 |
</div> |
| 198 |
""" |
| 199 |
self.check_html( |
| 200 |
self.widget(choices=[("1", "1"), ("2", "2"), ("3", "3")]), |
| 201 |
"num", |
| 202 |
3, |
| 203 |
html=html, |
| 204 |
) |
| 205 |
self.check_html( |
| 206 |
self.widget(choices=[(1, 1), (2, 2), (3, 3)]), "num", "3", html=html |
| 207 |
) |
| 208 |
self.check_html( |
| 209 |
self.widget(choices=[(1, 1), (2, 2), (3, 3)]), "num", 3, html=html |
| 210 |
) |
| 211 |
|
| 212 |
def test_choices_constructor(self): |
| 213 |
widget = self.widget(choices=[(1, 1), (2, 2), (3, 3)]) |
| 214 |
html = """ |
| 215 |
<div> |
| 216 |
<div> |
| 217 |
<label><input type="radio" name="num" value="1">1</label> |
| 218 |
</div> |
| 219 |
<div> |
| 220 |
<label><input type="radio" name="num" value="2">2</label> |
| 221 |
</div> |
| 222 |
<div> |
| 223 |
<label><input checked type="radio" name="num" value="3">3</label> |
| 224 |
</div> |
| 225 |
</div> |
| 226 |
""" |
| 227 |
self.check_html(widget, "num", 3, html=html) |
| 228 |
|
| 229 |
def test_choices_constructor_generator(self): |
| 230 |
""" |
| 231 |
If choices is passed to the constructor and is a generator, it can be |
| 232 |
iterated over multiple times without getting consumed. |
| 233 |
""" |
| 234 |
|
| 235 |
def get_choices(): |
| 236 |
for i in range(4): |
| 237 |
yield (i, i) |
| 238 |
|
| 239 |
html = """ |
| 240 |
<div> |
| 241 |
<div> |
| 242 |
<label><input type="radio" name="num" value="0">0</label> |
| 243 |
</div> |
| 244 |
<div> |
| 245 |
<label><input type="radio" name="num" value="1">1</label> |
| 246 |
</div> |
| 247 |
<div> |
| 248 |
<label><input type="radio" name="num" value="2">2</label> |
| 249 |
</div> |
| 250 |
<div> |
| 251 |
<label><input checked type="radio" name="num" value="3">3</label> |
| 252 |
</div> |
| 253 |
</div> |
| 254 |
""" |
| 255 |
widget = self.widget(choices=get_choices()) |
| 256 |
self.check_html(widget, "num", 3, html=html) |
| 257 |
|
| 258 |
def test_choices_escaping(self): |
| 259 |
choices = (("bad", "you & me"), ("good", mark_safe("you > me"))) |
| 260 |
html = """ |
| 261 |
<div> |
| 262 |
<div> |
| 263 |
<label><input type="radio" name="escape" value="bad">you & me</label> |
| 264 |
</div> |
| 265 |
<div> |
| 266 |
<label><input type="radio" name="escape" value="good">you > me</label> |
| 267 |
</div> |
| 268 |
</div> |
| 269 |
""" |
| 270 |
self.check_html(self.widget(choices=choices), "escape", None, html=html) |
| 271 |
|
| 272 |
def test_choices_unicode(self): |
| 273 |
html = """ |
| 274 |
<div> |
| 275 |
<div> |
| 276 |
<label> |
| 277 |
<input checked type="radio" name="email" |
| 278 |
value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111"> |
| 279 |
\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label> |
| 280 |
</div> |
| 281 |
<div> |
| 282 |
<label> |
| 283 |
<input type="radio" name="email" value="\u0107\u017e\u0161\u0111"> |
| 284 |
abc\u0107\u017e\u0161\u0111</label> |
| 285 |
</div> |
| 286 |
</div> |
| 287 |
""" |
| 288 |
self.check_html( |
| 289 |
self.widget(choices=[("ŠĐĆŽćžšđ", "ŠĐabcĆŽćžšđ"), ("ćžšđ", "abcćžšđ")]), |
| 290 |
"email", |
| 291 |
"ŠĐĆŽćžšđ", |
| 292 |
html=html, |
| 293 |
) |
| 294 |
|
| 295 |
def test_choices_optgroup(self): |
| 296 |
""" |
| 297 |
Choices can be nested one level in order to create HTML optgroups. |
| 298 |
""" |
| 299 |
html = """ |
| 300 |
<div> |
| 301 |
<div> |
| 302 |
<label><input type="radio" name="nestchoice" value="outer1">Outer 1</label> |
| 303 |
</div> |
| 304 |
<div> |
| 305 |
<label>Group "1"</label> |
| 306 |
<div> |
| 307 |
<label> |
| 308 |
<input type="radio" name="nestchoice" value="inner1">Inner 1</label> |
| 309 |
</div> |
| 310 |
<div> |
| 311 |
<label> |
| 312 |
<input type="radio" name="nestchoice" value="inner2">Inner 2</label> |
| 313 |
</div> |
| 314 |
</div> |
| 315 |
</div> |
| 316 |
""" |
| 317 |
for widget in self.nested_widgets: |
| 318 |
with self.subTest(widget): |
| 319 |
self.check_html(widget, "nestchoice", None, html=html) |
| 320 |
|
| 321 |
def test_choices_select_outer(self): |
| 322 |
html = """ |
| 323 |
<div> |
| 324 |
<div> |
| 325 |
<label> |
| 326 |
<input checked type="radio" name="nestchoice" value="outer1">Outer 1</label> |
| 327 |
</div> |
| 328 |
<div> |
| 329 |
<label>Group "1"</label> |
| 330 |
<div> |
| 331 |
<label> |
| 332 |
<input type="radio" name="nestchoice" value="inner1">Inner 1</label> |
| 333 |
</div> |
| 334 |
<div> |
| 335 |
<label> |
| 336 |
<input type="radio" name="nestchoice" value="inner2">Inner 2</label> |
| 337 |
</div> |
| 338 |
</div> |
| 339 |
</div> |
| 340 |
""" |
| 341 |
for widget in self.nested_widgets: |
| 342 |
with self.subTest(widget): |
| 343 |
self.check_html(widget, "nestchoice", "outer1", html=html) |
| 344 |
|
| 345 |
def test_choices_select_inner(self): |
| 346 |
html = """ |
| 347 |
<div> |
| 348 |
<div> |
| 349 |
<label><input type="radio" name="nestchoice" value="outer1">Outer 1</label> |
| 350 |
</div> |
| 351 |
<div> |
| 352 |
<label>Group "1"</label> |
| 353 |
<div> |
| 354 |
<label> |
| 355 |
<input type="radio" name="nestchoice" value="inner1">Inner 1</label> |
| 356 |
</div> |
| 357 |
<div> |
| 358 |
<label> |
| 359 |
<input checked type="radio" name="nestchoice" value="inner2">Inner 2 |
| 360 |
</label> |
| 361 |
</div> |
| 362 |
</div> |
| 363 |
</div> |
| 364 |
""" |
| 365 |
for widget in self.nested_widgets: |
| 366 |
with self.subTest(widget): |
| 367 |
self.check_html(widget, "nestchoice", "inner2", html=html) |
| 368 |
|
| 369 |
def test_render_attrs(self): |
| 370 |
""" |
| 371 |
Attributes provided at render-time are passed to the constituent |
| 372 |
inputs. |
| 373 |
""" |
| 374 |
html = """ |
| 375 |
<div id="bar"> |
| 376 |
<div> |
| 377 |
<label for="bar_0"> |
| 378 |
<input checked type="radio" id="bar_0" value="J" name="beatle">John</label> |
| 379 |
</div> |
| 380 |
<div><label for="bar_1"> |
| 381 |
<input type="radio" id="bar_1" value="P" name="beatle">Paul</label> |
| 382 |
</div> |
| 383 |
<div><label for="bar_2"> |
| 384 |
<input type="radio" id="bar_2" value="G" name="beatle">George</label> |
| 385 |
</div> |
| 386 |
<div><label for="bar_3"> |
| 387 |
<input type="radio" id="bar_3" value="R" name="beatle">Ringo</label> |
| 388 |
</div> |
| 389 |
</div> |
| 390 |
""" |
| 391 |
self.check_html( |
| 392 |
self.widget(choices=self.beatles), |
| 393 |
"beatle", |
| 394 |
"J", |
| 395 |
attrs={"id": "bar"}, |
| 396 |
html=html, |
| 397 |
) |
| 398 |
|
| 399 |
def test_class_attrs(self): |
| 400 |
""" |
| 401 |
The <div> in the multiple_input.html widget template include the class |
| 402 |
attribute. |
| 403 |
""" |
| 404 |
html = """ |
| 405 |
<div class="bar"> |
| 406 |
<div><label> |
| 407 |
<input checked type="radio" class="bar" value="J" name="beatle">John</label> |
| 408 |
</div> |
| 409 |
<div><label> |
| 410 |
<input type="radio" class="bar" value="P" name="beatle">Paul</label> |
| 411 |
</div> |
| 412 |
<div><label> |
| 413 |
<input type="radio" class="bar" value="G" name="beatle">George</label> |
| 414 |
</div> |
| 415 |
<div><label> |
| 416 |
<input type="radio" class="bar" value="R" name="beatle">Ringo</label> |
| 417 |
</div> |
| 418 |
</div> |
| 419 |
""" |
| 420 |
self.check_html( |
| 421 |
self.widget(choices=self.beatles), |
| 422 |
"beatle", |
| 423 |
"J", |
| 424 |
attrs={"class": "bar"}, |
| 425 |
html=html, |
| 426 |
) |
| 427 |
|
| 428 |
@override_settings(USE_THOUSAND_SEPARATOR=True) |
| 429 |
def test_doesnt_localize_input_value(self): |
| 430 |
choices = [ |
| 431 |
(1, "One"), |
| 432 |
(1000, "One thousand"), |
| 433 |
(1000000, "One million"), |
| 434 |
] |
| 435 |
html = """ |
| 436 |
<div> |
| 437 |
<div><label><input type="radio" name="number" value="1">One</label></div> |
| 438 |
<div> |
| 439 |
<label><input type="radio" name="number" value="1000">One thousand</label> |
| 440 |
</div> |
| 441 |
<div> |
| 442 |
<label><input type="radio" name="number" value="1000000">One million</label> |
| 443 |
</div> |
| 444 |
</div> |
| 445 |
""" |
| 446 |
self.check_html(self.widget(choices=choices), "number", None, html=html) |
| 447 |
|
| 448 |
choices = [ |
| 449 |
(datetime.time(0, 0), "midnight"), |
| 450 |
(datetime.time(12, 0), "noon"), |
| 451 |
] |
| 452 |
html = """ |
| 453 |
<div> |
| 454 |
<div> |
| 455 |
<label><input type="radio" name="time" value="00:00:00">midnight</label> |
| 456 |
</div> |
| 457 |
<div> |
| 458 |
<label><input type="radio" name="time" value="12:00:00">noon</label> |
| 459 |
</div> |
| 460 |
</div> |
| 461 |
""" |
| 462 |
self.check_html(self.widget(choices=choices), "time", None, html=html) |
| 463 |
|
| 464 |
def test_render_as_subwidget(self): |
| 465 |
"""A RadioSelect as a subwidget of MultiWidget.""" |
| 466 |
choices = BLANK_CHOICE_DASH + self.beatles |
| 467 |
html = """ |
| 468 |
<div> |
| 469 |
<div><label> |
| 470 |
<input type="radio" name="beatle_0" value="">------</label> |
| 471 |
</div> |
| 472 |
<div><label> |
| 473 |
<input checked type="radio" name="beatle_0" value="J">John</label> |
| 474 |
</div> |
| 475 |
<div><label> |
| 476 |
<input type="radio" name="beatle_0" value="P">Paul</label> |
| 477 |
</div> |
| 478 |
<div><label> |
| 479 |
<input type="radio" name="beatle_0" value="G">George</label> |
| 480 |
</div> |
| 481 |
<div><label> |
| 482 |
<input type="radio" name="beatle_0" value="R">Ringo</label> |
| 483 |
</div> |
| 484 |
</div> |
| 485 |
<input name="beatle_1" type="text" value="Some text"> |
| 486 |
""" |
| 487 |
self.check_html( |
| 488 |
MultiWidget([self.widget(choices=choices), TextInput()]), |
| 489 |
"beatle", |
| 490 |
["J", "Some text"], |
| 491 |
html=html, |
| 492 |
) |
| 493 |
|
| 494 |
def test_fieldset(self): |
| 495 |
class TestForm(Form): |
| 496 |
template_name = "forms_tests/use_fieldset.html" |
| 497 |
field = ChoiceField( |
| 498 |
widget=self.widget, choices=self.beatles, required=False |
| 499 |
) |
| 500 |
|
| 501 |
form = TestForm() |
| 502 |
self.assertIs(self.widget.use_fieldset, True) |
| 503 |
self.assertHTMLEqual( |
| 504 |
'<div><fieldset><legend>Field:</legend><div id="id_field">' |
| 505 |
'<div><label for="id_field_0">' |
| 506 |
'<input type="radio" name="field" value="J" id="id_field_0"> John' |
| 507 |
'</label></div><div><label for="id_field_1">' |
| 508 |
'<input type="radio" name="field" value="P" id="id_field_1">Paul' |
| 509 |
'</label></div><div><label for="id_field_2"><input type="radio" ' |
| 510 |
'name="field" value="G" id="id_field_2"> George</label></div>' |
| 511 |
'<div><label for="id_field_3"><input type="radio" name="field" ' |
| 512 |
'value="R" id="id_field_3">Ringo</label></div></div></fieldset>' |
| 513 |
"</div>", |
| 514 |
form.render(), |
| 515 |
) |