import datetime
from django.forms import ChoiceField, Form, MultiWidget, RadioSelect, TextInput
from django.test import override_settings
from django.utils.safestring import mark_safe
from .test_choicewidget import ChoiceWidgetTest
BLANK_CHOICE_DASH = (("", "------"),)
class RadioSelectTest(ChoiceWidgetTest):
widget = RadioSelect
def test_render(self):
html = """
"""
beatles_with_blank = BLANK_CHOICE_DASH + self.beatles
for choices in (beatles_with_blank, dict(beatles_with_blank)):
with self.subTest(choices):
self.check_html(self.widget(choices=choices), "beatle", "J", html=html)
def test_nested_choices(self):
nested_choices = (
("unknown", "Unknown"),
("Audio", (("vinyl", "Vinyl"), ("cd", "CD"))),
("Video", (("vhs", "VHS"), ("dvd", "DVD"))),
)
html = """
"""
self.check_html(
self.widget(choices=nested_choices),
"nestchoice",
"dvd",
attrs={"id": "media"},
html=html,
)
def test_render_none(self):
"""
If value is None, none of the options are selected.
"""
choices = BLANK_CHOICE_DASH + self.beatles
html = """
"""
self.check_html(self.widget(choices=choices), "beatle", None, html=html)
def test_render_label_value(self):
"""
If the value corresponds to a label (but not to an option value), none
of the options are selected.
"""
html = """
"""
self.check_html(self.widget(choices=self.beatles), "beatle", "Ringo", html=html)
def test_render_selected(self):
"""
Only one option can be selected.
"""
choices = [("0", "0"), ("1", "1"), ("2", "2"), ("3", "3"), ("0", "extra")]
html = """
"""
self.check_html(self.widget(choices=choices), "choices", "0", html=html)
def test_constructor_attrs(self):
"""
Attributes provided at instantiation are passed to the constituent
inputs.
"""
widget = self.widget(attrs={"id": "foo"}, choices=self.beatles)
html = """
"""
self.check_html(widget, "beatle", "J", html=html)
def test_compare_to_str(self):
"""
The value is compared to its str().
"""
html = """
"""
self.check_html(widget, "num", 3, html=html)
def test_choices_constructor_generator(self):
"""
If choices is passed to the constructor and is a generator, it can be
iterated over multiple times without getting consumed.
"""
def get_choices():
for i in range(4):
yield (i, i)
html = """
"""
self.check_html(self.widget(choices=choices), "escape", None, html=html)
def test_choices_unicode(self):
html = """
"""
self.check_html(
self.widget(choices=[("ŠĐĆŽćžšđ", "ŠĐabcĆŽćžšđ"), ("ćžšđ", "abcćžšđ")]),
"email",
"ŠĐĆŽćžšđ",
html=html,
)
def test_choices_optgroup(self):
"""
Choices can be nested one level in order to create HTML optgroups.
"""
html = """
"""
for widget in self.nested_widgets:
with self.subTest(widget):
self.check_html(widget, "nestchoice", None, html=html)
def test_choices_select_outer(self):
html = """
"""
for widget in self.nested_widgets:
with self.subTest(widget):
self.check_html(widget, "nestchoice", "outer1", html=html)
def test_choices_select_inner(self):
html = """
"""
for widget in self.nested_widgets:
with self.subTest(widget):
self.check_html(widget, "nestchoice", "inner2", html=html)
def test_render_attrs(self):
"""
Attributes provided at render-time are passed to the constituent
inputs.
"""
html = """