test_select.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import copy
  4. from django.forms import Select
  5. from django.utils.safestring import mark_safe
  6. from .base import WidgetTest
  7. class SelectTest(WidgetTest):
  8. widget = Select
  9. nested_widget = Select(choices=(
  10. ('outer1', 'Outer 1'),
  11. ('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))),
  12. ))
  13. def test_render(self):
  14. self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', 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. def test_render_none(self):
  23. """
  24. If the value is None, none of the options are selected.
  25. """
  26. self.check_html(self.widget(choices=self.beatles), 'beatle', None, html=(
  27. """<select name="beatle">
  28. <option value="J">John</option>
  29. <option value="P">Paul</option>
  30. <option value="G">George</option>
  31. <option value="R">Ringo</option>
  32. </select>"""
  33. ))
  34. def test_render_label_value(self):
  35. """
  36. If the value corresponds to a label (but not to an option value), none
  37. of the options are selected.
  38. """
  39. self.check_html(self.widget(choices=self.beatles), 'beatle', 'John', html=(
  40. """<select name="beatle">
  41. <option value="J">John</option>
  42. <option value="P">Paul</option>
  43. <option value="G">George</option>
  44. <option value="R">Ringo</option>
  45. </select>"""
  46. ))
  47. def test_render_selected(self):
  48. """
  49. Only one option can be selected (#8103).
  50. """
  51. choices = [('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra')]
  52. self.check_html(self.widget(choices=choices), 'choices', '0', html=(
  53. """<select name="choices">
  54. <option value="0" selected>0</option>
  55. <option value="1">1</option>
  56. <option value="2">2</option>
  57. <option value="3">3</option>
  58. <option value="0">extra</option>
  59. </select>"""
  60. ))
  61. def test_constructor_attrs(self):
  62. """
  63. Select options shouldn't inherit the parent widget attrs.
  64. """
  65. widget = Select(
  66. attrs={'class': 'super', 'id': 'super'},
  67. choices=[(1, 1), (2, 2), (3, 3)],
  68. )
  69. self.check_html(widget, 'num', 2, html=(
  70. """<select name="num" class="super" id="super">
  71. <option value="1">1</option>
  72. <option value="2" selected>2</option>
  73. <option value="3">3</option>
  74. </select>"""
  75. ))
  76. def test_compare_to_str(self):
  77. """
  78. The value is compared to its str().
  79. """
  80. self.check_html(
  81. self.widget(choices=[('1', '1'), ('2', '2'), ('3', '3')]),
  82. 'num', 2,
  83. html=(
  84. """<select name="num">
  85. <option value="1">1</option>
  86. <option value="2" selected>2</option>
  87. <option value="3">3</option>
  88. </select>"""
  89. ),
  90. )
  91. self.check_html(
  92. self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
  93. 'num', '2',
  94. html=(
  95. """<select name="num">
  96. <option value="1">1</option>
  97. <option value="2" selected>2</option>
  98. <option value="3">3</option>
  99. </select>"""
  100. ),
  101. )
  102. self.check_html(
  103. self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
  104. 'num', 2,
  105. html=(
  106. """<select name="num">
  107. <option value="1">1</option>
  108. <option value="2" selected>2</option>
  109. <option value="3">3</option>
  110. </select>"""
  111. ),
  112. )
  113. def test_choices_constuctor(self):
  114. widget = Select(choices=[(1, 1), (2, 2), (3, 3)])
  115. self.check_html(widget, 'num', 2, html=(
  116. """<select name="num">
  117. <option value="1">1</option>
  118. <option value="2" selected>2</option>
  119. <option value="3">3</option>
  120. </select>"""
  121. ))
  122. def test_choices_constructor_generator(self):
  123. """
  124. If choices is passed to the constructor and is a generator, it can be
  125. iterated over multiple times without getting consumed.
  126. """
  127. def get_choices():
  128. for i in range(5):
  129. yield (i, i)
  130. widget = Select(choices=get_choices())
  131. self.check_html(widget, 'num', 2, html=(
  132. """<select name="num">
  133. <option value="0">0</option>
  134. <option value="1">1</option>
  135. <option value="2" selected>2</option>
  136. <option value="3">3</option>
  137. <option value="4">4</option>
  138. </select>"""
  139. ))
  140. self.check_html(widget, 'num', 3, html=(
  141. """<select name="num">
  142. <option value="0">0</option>
  143. <option value="1">1</option>
  144. <option value="2">2</option>
  145. <option value="3" selected>3</option>
  146. <option value="4">4</option>
  147. </select>"""
  148. ))
  149. def test_choices_escaping(self):
  150. choices = (('bad', 'you & me'), ('good', mark_safe('you &gt; me')))
  151. self.check_html(self.widget(choices=choices), 'escape', None, html=(
  152. """<select name="escape">
  153. <option value="bad">you &amp; me</option>
  154. <option value="good">you &gt; me</option>
  155. </select>"""
  156. ))
  157. def test_choices_unicode(self):
  158. self.check_html(
  159. self.widget(choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]),
  160. 'email', 'ŠĐĆŽćžšđ',
  161. html=(
  162. """<select name="email">
  163. <option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected>
  164. \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111
  165. </option>
  166. <option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>
  167. </select>"""
  168. ),
  169. )
  170. def test_choices_optgroup(self):
  171. """
  172. Choices can be nested one level in order to create HTML optgroups.
  173. """
  174. self.check_html(self.nested_widget, 'nestchoice', None, html=(
  175. """<select name="nestchoice">
  176. <option value="outer1">Outer 1</option>
  177. <optgroup label="Group &quot;1&quot;">
  178. <option value="inner1">Inner 1</option>
  179. <option value="inner2">Inner 2</option>
  180. </optgroup>
  181. </select>"""
  182. ))
  183. def test_choices_select_outer(self):
  184. self.check_html(self.nested_widget, 'nestchoice', 'outer1', html=(
  185. """<select name="nestchoice">
  186. <option value="outer1" selected>Outer 1</option>
  187. <optgroup label="Group &quot;1&quot;">
  188. <option value="inner1">Inner 1</option>
  189. <option value="inner2">Inner 2</option>
  190. </optgroup>
  191. </select>"""
  192. ))
  193. def test_choices_select_inner(self):
  194. self.check_html(self.nested_widget, 'nestchoice', 'inner1', html=(
  195. """<select name="nestchoice">
  196. <option value="outer1">Outer 1</option>
  197. <optgroup label="Group &quot;1&quot;">
  198. <option value="inner1" selected>Inner 1</option>
  199. <option value="inner2">Inner 2</option>
  200. </optgroup>
  201. </select>"""
  202. ))
  203. def test_options(self):
  204. options = list(self.widget(choices=self.beatles).options(
  205. 'name', ['J'], attrs={'class': 'super'},
  206. ))
  207. self.assertEqual(len(options), 4)
  208. self.assertEqual(options[0]['name'], 'name')
  209. self.assertEqual(options[0]['value'], 'J')
  210. self.assertEqual(options[0]['label'], 'John')
  211. self.assertEqual(options[0]['index'], '0')
  212. self.assertEqual(options[0]['selected'], True)
  213. # Template-related attributes
  214. self.assertEqual(options[1]['name'], 'name')
  215. self.assertEqual(options[1]['value'], 'P')
  216. self.assertEqual(options[1]['label'], 'Paul')
  217. self.assertEqual(options[1]['index'], '1')
  218. self.assertEqual(options[1]['selected'], False)
  219. def test_optgroups(self):
  220. choices = [
  221. ('Audio', [
  222. ('vinyl', 'Vinyl'),
  223. ('cd', 'CD'),
  224. ]),
  225. ('Video', [
  226. ('vhs', 'VHS Tape'),
  227. ('dvd', 'DVD'),
  228. ]),
  229. ('unknown', 'Unknown'),
  230. ]
  231. groups = list(self.widget(choices=choices).optgroups(
  232. 'name', ['vhs'], attrs={'class': 'super'},
  233. ))
  234. self.assertEqual(len(groups), 3)
  235. self.assertEqual(groups[0][0], None)
  236. self.assertEqual(groups[0][2], 0)
  237. self.assertEqual(len(groups[0][1]), 1)
  238. options = groups[0][1]
  239. self.assertEqual(options[0]['name'], 'name')
  240. self.assertEqual(options[0]['value'], 'unknown')
  241. self.assertEqual(options[0]['label'], 'Unknown')
  242. self.assertEqual(options[0]['index'], '0')
  243. self.assertEqual(options[0]['selected'], False)
  244. self.assertEqual(groups[1][0], 'Audio')
  245. self.assertEqual(groups[1][2], 1)
  246. self.assertEqual(len(groups[1][1]), 2)
  247. options = groups[1][1]
  248. self.assertEqual(options[0]['name'], 'name')
  249. self.assertEqual(options[0]['value'], 'vinyl')
  250. self.assertEqual(options[0]['label'], 'Vinyl')
  251. self.assertEqual(options[0]['index'], '1_0')
  252. self.assertEqual(options[1]['index'], '1_1')
  253. self.assertEqual(groups[2][0], 'Video')
  254. self.assertEqual(groups[2][2], 2)
  255. self.assertEqual(len(groups[2][1]), 2)
  256. options = groups[2][1]
  257. self.assertEqual(options[0]['name'], 'name')
  258. self.assertEqual(options[0]['value'], 'vhs')
  259. self.assertEqual(options[0]['label'], 'VHS Tape')
  260. self.assertEqual(options[0]['index'], '2_0')
  261. self.assertEqual(options[0]['selected'], True)
  262. self.assertEqual(options[1]['index'], '2_1')
  263. def test_deepcopy(self):
  264. """
  265. __deepcopy__() should copy all attributes properly (#25085).
  266. """
  267. widget = Select()
  268. obj = copy.deepcopy(widget)
  269. self.assertIsNot(widget, obj)
  270. self.assertEqual(widget.choices, obj.choices)
  271. self.assertIsNot(widget.choices, obj.choices)
  272. self.assertEqual(widget.attrs, obj.attrs)
  273. self.assertIsNot(widget.attrs, obj.attrs)