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