test_multiplehiddeninput.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from django.forms import Form, MultipleChoiceField, MultipleHiddenInput
  2. from django.utils.datastructures import MultiValueDict
  3. from .base import WidgetTest
  4. class MultipleHiddenInputTest(WidgetTest):
  5. widget = MultipleHiddenInput()
  6. def test_render_single(self):
  7. self.check_html(
  8. self.widget,
  9. "email",
  10. ["test@example.com"],
  11. html='<input type="hidden" name="email" value="test@example.com">',
  12. )
  13. def test_render_multiple(self):
  14. self.check_html(
  15. self.widget,
  16. "email",
  17. ["test@example.com", "foo@example.com"],
  18. html=(
  19. '<input type="hidden" name="email" value="test@example.com">\n'
  20. '<input type="hidden" name="email" value="foo@example.com">'
  21. ),
  22. )
  23. def test_render_attrs(self):
  24. self.check_html(
  25. self.widget,
  26. "email",
  27. ["test@example.com"],
  28. attrs={"class": "fun"},
  29. html=(
  30. '<input type="hidden" name="email" value="test@example.com" '
  31. 'class="fun">'
  32. ),
  33. )
  34. def test_render_attrs_multiple(self):
  35. self.check_html(
  36. self.widget,
  37. "email",
  38. ["test@example.com", "foo@example.com"],
  39. attrs={"class": "fun"},
  40. html=(
  41. '<input type="hidden" name="email" value="test@example.com" '
  42. 'class="fun">\n'
  43. '<input type="hidden" name="email" value="foo@example.com" class="fun">'
  44. ),
  45. )
  46. def test_render_attrs_constructor(self):
  47. widget = MultipleHiddenInput(attrs={"class": "fun"})
  48. self.check_html(widget, "email", [], "")
  49. self.check_html(
  50. widget,
  51. "email",
  52. ["foo@example.com"],
  53. html=(
  54. '<input type="hidden" class="fun" value="foo@example.com" name="email">'
  55. ),
  56. )
  57. self.check_html(
  58. widget,
  59. "email",
  60. ["foo@example.com", "test@example.com"],
  61. html=(
  62. '<input type="hidden" class="fun" value="foo@example.com" '
  63. 'name="email">\n'
  64. '<input type="hidden" class="fun" value="test@example.com" '
  65. 'name="email">'
  66. ),
  67. )
  68. self.check_html(
  69. widget,
  70. "email",
  71. ["foo@example.com"],
  72. attrs={"class": "special"},
  73. html=(
  74. '<input type="hidden" class="special" value="foo@example.com" '
  75. 'name="email">'
  76. ),
  77. )
  78. def test_render_empty(self):
  79. self.check_html(self.widget, "email", [], "")
  80. def test_render_none(self):
  81. self.check_html(self.widget, "email", None, "")
  82. def test_render_increment_id(self):
  83. """
  84. Each input should get a separate ID.
  85. """
  86. self.check_html(
  87. self.widget,
  88. "letters",
  89. ["a", "b", "c"],
  90. attrs={"id": "hideme"},
  91. html=(
  92. '<input type="hidden" name="letters" value="a" id="hideme_0">\n'
  93. '<input type="hidden" name="letters" value="b" id="hideme_1">\n'
  94. '<input type="hidden" name="letters" value="c" id="hideme_2">'
  95. ),
  96. )
  97. def test_fieldset(self):
  98. class TestForm(Form):
  99. template_name = "forms_tests/use_fieldset.html"
  100. composers = MultipleChoiceField(
  101. choices=[("J", "John Lennon"), ("P", "Paul McCartney")],
  102. widget=MultipleHiddenInput,
  103. )
  104. form = TestForm(MultiValueDict({"composers": ["J", "P"]}))
  105. self.assertIs(self.widget.use_fieldset, False)
  106. self.assertHTMLEqual(
  107. '<input type="hidden" name="composers" value="J" id="id_composers_0">'
  108. '<input type="hidden" name="composers" value="P" id="id_composers_1">',
  109. form.render(),
  110. )