test_radioselect.py 16 KB

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