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='',
)
def test_render_multiple(self):
self.check_html(
self.widget,
"email",
["test@example.com", "foo@example.com"],
html=(
'\n'
''
),
)
def test_render_attrs(self):
self.check_html(
self.widget,
"email",
["test@example.com"],
attrs={"class": "fun"},
html=(
''
),
)
def test_render_attrs_multiple(self):
self.check_html(
self.widget,
"email",
["test@example.com", "foo@example.com"],
attrs={"class": "fun"},
html=(
'\n'
''
),
)
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=(
''
),
)
self.check_html(
widget,
"email",
["foo@example.com", "test@example.com"],
html=(
'\n'
''
),
)
self.check_html(
widget,
"email",
["foo@example.com"],
attrs={"class": "special"},
html=(
''
),
)
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=(
'\n'
'\n'
''
),
)
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(
''
'',
form.render(),
)