mirror of
https://github.com/django/django.git
synced 2024-11-21 19:09:18 +01:00
Fixed #34655 -- Increased radioselect's test coverage.
This commit is contained in:
parent
89c8c2e831
commit
f9c5958b8f
1
AUTHORS
1
AUTHORS
@ -431,6 +431,7 @@ answer newbie questions, and generally made Django that much better:
|
||||
Jacob Kaplan-Moss <jacob@jacobian.org>
|
||||
Jacob Rief <jacob.rief@gmail.com>
|
||||
Jacob Walls <http://www.jacobtylerwalls.com/>
|
||||
Jakub Bagiński <https://github.com/Jacob1507>
|
||||
Jakub Paczkowski <jakub@paczkowski.eu>
|
||||
Jakub Wilk <jwilk@jwilk.net>
|
||||
Jakub Wiśniowski <restless.being@gmail.com>
|
||||
|
71
tests/forms_tests/widget_tests/test_choicewidget.py
Normal file
71
tests/forms_tests/widget_tests/test_choicewidget.py
Normal file
@ -0,0 +1,71 @@
|
||||
import copy
|
||||
|
||||
from django.forms.widgets import ChoiceWidget
|
||||
|
||||
from .base import WidgetTest
|
||||
|
||||
|
||||
class ChoiceWidgetTest(WidgetTest):
|
||||
widget = ChoiceWidget
|
||||
|
||||
@property
|
||||
def nested_widget(self):
|
||||
return self.widget(
|
||||
choices=(
|
||||
("outer1", "Outer 1"),
|
||||
('Group "1"', (("inner1", "Inner 1"), ("inner2", "Inner 2"))),
|
||||
)
|
||||
)
|
||||
|
||||
def test_deepcopy(self):
|
||||
"""
|
||||
__deepcopy__() should copy all attributes properly.
|
||||
"""
|
||||
widget = self.widget()
|
||||
obj = copy.deepcopy(widget)
|
||||
self.assertIsNot(widget, obj)
|
||||
self.assertEqual(widget.choices, obj.choices)
|
||||
self.assertIsNot(widget.choices, obj.choices)
|
||||
self.assertEqual(widget.attrs, obj.attrs)
|
||||
self.assertIsNot(widget.attrs, obj.attrs)
|
||||
|
||||
def test_options(self):
|
||||
options = list(
|
||||
self.widget(choices=self.beatles).options(
|
||||
"name",
|
||||
["J"],
|
||||
attrs={"class": "super"},
|
||||
)
|
||||
)
|
||||
self.assertEqual(len(options), 4)
|
||||
self.assertEqual(options[0]["name"], "name")
|
||||
self.assertEqual(options[0]["value"], "J")
|
||||
self.assertEqual(options[0]["label"], "John")
|
||||
self.assertEqual(options[0]["index"], "0")
|
||||
self.assertIs(options[0]["selected"], True)
|
||||
# Template-related attributes
|
||||
self.assertEqual(options[1]["name"], "name")
|
||||
self.assertEqual(options[1]["value"], "P")
|
||||
self.assertEqual(options[1]["label"], "Paul")
|
||||
self.assertEqual(options[1]["index"], "1")
|
||||
self.assertIs(options[1]["selected"], False)
|
||||
|
||||
def test_optgroups_integer_choices(self):
|
||||
"""The option 'value' is the same type as what's in `choices`."""
|
||||
groups = list(
|
||||
self.widget(choices=[[0, "choice text"]]).optgroups("name", ["vhs"])
|
||||
)
|
||||
label, options, index = groups[0]
|
||||
self.assertEqual(options[0]["value"], 0)
|
||||
|
||||
def test_renders_required_when_possible_to_select_empty_field_none(self):
|
||||
widget = self.widget(choices=[(None, "select please"), ("P", "Paul")])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), True)
|
||||
|
||||
def test_renders_required_when_possible_to_select_empty_field_list(self):
|
||||
widget = self.widget(choices=[["", "select please"], ["P", "Paul"]])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), True)
|
||||
|
||||
def test_renders_required_when_possible_to_select_empty_field_str(self):
|
||||
widget = self.widget(choices=[("", "select please"), ("P", "Paul")])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), True)
|
@ -1,32 +1,39 @@
|
||||
import datetime
|
||||
|
||||
from django.forms import ChoiceField, Form, MultiWidget, RadioSelect
|
||||
from django.forms import ChoiceField, Form, MultiWidget, RadioSelect, TextInput
|
||||
from django.test import override_settings
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from .base import WidgetTest
|
||||
from .test_choicewidget import ChoiceWidgetTest
|
||||
|
||||
BLANK_CHOICE_DASH = (("", "------"),)
|
||||
|
||||
|
||||
class RadioSelectTest(WidgetTest):
|
||||
class RadioSelectTest(ChoiceWidgetTest):
|
||||
widget = RadioSelect
|
||||
|
||||
def test_render(self):
|
||||
choices = (("", "------"),) + self.beatles
|
||||
self.check_html(
|
||||
self.widget(choices=choices),
|
||||
"beatle",
|
||||
"J",
|
||||
html="""
|
||||
<div>
|
||||
<div><label><input type="radio" name="beatle" value=""> ------</label></div>
|
||||
<div><label>
|
||||
<input checked type="radio" name="beatle" value="J"> John</label></div>
|
||||
<div><label><input type="radio" name="beatle" value="P"> Paul</label></div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle" value="G"> George</label></div>
|
||||
<div><label><input type="radio" name="beatle" value="R"> Ringo</label></div>
|
||||
</div>
|
||||
""",
|
||||
)
|
||||
choices = BLANK_CHOICE_DASH + self.beatles
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="">------</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input checked type="radio" name="beatle" value="J">John</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="P">Paul</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="G">George</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="R">Ringo</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(self.widget(choices=choices), "beatle", "J", html=html)
|
||||
|
||||
def test_nested_choices(self):
|
||||
nested_choices = (
|
||||
@ -70,28 +77,287 @@ class RadioSelectTest(WidgetTest):
|
||||
html=html,
|
||||
)
|
||||
|
||||
def test_render_none(self):
|
||||
"""
|
||||
If value is None, none of the options are selected.
|
||||
"""
|
||||
choices = BLANK_CHOICE_DASH + self.beatles
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input checked type="radio" name="beatle" value="">------</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="J">John</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="P">Paul</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="G">George</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="R">Ringo</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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 = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="J">John</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="P">Paul</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="G">George</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="beatle" value="R">Ringo</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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 = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input checked type="radio" name="choices" value="0">0</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="choices" value="1">1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="choices" value="2">2</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="choices" value="3">3</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="choices" value="0">extra</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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 = RadioSelect(attrs={"id": "foo"}, choices=self.beatles)
|
||||
widget = self.widget(attrs={"id": "foo"}, choices=self.beatles)
|
||||
html = """
|
||||
<div id="foo">
|
||||
<div>
|
||||
<label for="foo_0">
|
||||
<input checked type="radio" id="foo_0" value="J" name="beatle"> John</label>
|
||||
</div>
|
||||
<div><label for="foo_1">
|
||||
<input type="radio" id="foo_1" value="P" name="beatle"> Paul</label></div>
|
||||
<div><label for="foo_2">
|
||||
<input type="radio" id="foo_2" value="G" name="beatle"> George</label></div>
|
||||
<div><label for="foo_3">
|
||||
<input type="radio" id="foo_3" value="R" name="beatle"> Ringo</label></div>
|
||||
<div>
|
||||
<label for="foo_0">
|
||||
<input checked type="radio" id="foo_0" value="J" name="beatle">John</label>
|
||||
</div>
|
||||
<div><label for="foo_1">
|
||||
<input type="radio" id="foo_1" value="P" name="beatle">Paul</label>
|
||||
</div>
|
||||
<div><label for="foo_2">
|
||||
<input type="radio" id="foo_2" value="G" name="beatle">George</label>
|
||||
</div>
|
||||
<div><label for="foo_3">
|
||||
<input type="radio" id="foo_3" value="R" name="beatle">Ringo</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(widget, "beatle", "J", html=html)
|
||||
|
||||
def test_compare_to_str(self):
|
||||
"""
|
||||
The value is compared to its str()
|
||||
"""
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="num" value="1">1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="num" value="2">2</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input checked type="radio" name="num" value="3">3</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(
|
||||
self.widget(choices=[("1", "1"), ("2", "2"), ("3", "3")]),
|
||||
"num",
|
||||
3,
|
||||
html=html,
|
||||
)
|
||||
self.check_html(
|
||||
self.widget(choices=[(1, 1), (2, 2), (3, 3)]), "num", "3", html=html
|
||||
)
|
||||
self.check_html(
|
||||
self.widget(choices=[(1, 1), (2, 2), (3, 3)]), "num", 3, html=html
|
||||
)
|
||||
|
||||
def test_choices_constructor(self):
|
||||
widget = self.widget(choices=[(1, 1), (2, 2), (3, 3)])
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="num" value="1">1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="num" value="2">2</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input checked type="radio" name="num" value="3">3</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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 = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="num" value="0">0</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="num" value="1">1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="num" value="2">2</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input checked type="radio" name="num" value="3">3</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
widget = self.widget(choices=get_choices())
|
||||
self.check_html(widget, "num", 3, html=html)
|
||||
|
||||
def test_choices_escaping(self):
|
||||
choices = (("bad", "you & me"), ("good", mark_safe("you > me")))
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="escape" value="bad">you & me</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="escape" value="good">you > me</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(self.widget(choices=choices), "escape", None, html=html)
|
||||
|
||||
def test_choices_unicode(self):
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label>
|
||||
<input checked type="radio" name="email"
|
||||
value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111">
|
||||
\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>
|
||||
<input type="radio" name="email" value="\u0107\u017e\u0161\u0111">
|
||||
abc\u0107\u017e\u0161\u0111</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
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 = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="nestchoice" value="outer1">Outer 1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>Group "1"</label>
|
||||
<div>
|
||||
<label>
|
||||
<input type="radio" name="nestchoice" value="inner1">Inner 1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>
|
||||
<input type="radio" name="nestchoice" value="inner2">Inner 2</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(self.nested_widget, "nestchoice", None, html=html)
|
||||
|
||||
def test_choices_select_outer(self):
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label>
|
||||
<input checked type="radio" name="nestchoice" value="outer1">Outer 1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>Group "1"</label>
|
||||
<div>
|
||||
<label>
|
||||
<input type="radio" name="nestchoice" value="inner1">Inner 1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>
|
||||
<input type="radio" name="nestchoice" value="inner2">Inner 2</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(self.nested_widget, "nestchoice", "outer1", html=html)
|
||||
|
||||
def test_choices_select_inner(self):
|
||||
html = """
|
||||
<div>
|
||||
<div>
|
||||
<label><input type="radio" name="nestchoice" value="outer1">Outer 1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>Group "1"</label>
|
||||
<div>
|
||||
<label>
|
||||
<input type="radio" name="nestchoice" value="inner1">Inner 1</label>
|
||||
</div>
|
||||
<div>
|
||||
<label>
|
||||
<input checked type="radio" name="nestchoice" value="inner2">Inner 2
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(self.nested_widget, "nestchoice", "inner2", html=html)
|
||||
|
||||
def test_render_attrs(self):
|
||||
"""
|
||||
Attributes provided at render-time are passed to the constituent
|
||||
@ -99,16 +365,19 @@ class RadioSelectTest(WidgetTest):
|
||||
"""
|
||||
html = """
|
||||
<div id="bar">
|
||||
<div>
|
||||
<label for="bar_0">
|
||||
<input checked type="radio" id="bar_0" value="J" name="beatle"> John</label>
|
||||
</div>
|
||||
<div><label for="bar_1">
|
||||
<input type="radio" id="bar_1" value="P" name="beatle"> Paul</label></div>
|
||||
<div><label for="bar_2">
|
||||
<input type="radio" id="bar_2" value="G" name="beatle"> George</label></div>
|
||||
<div><label for="bar_3">
|
||||
<input type="radio" id="bar_3" value="R" name="beatle"> Ringo</label></div>
|
||||
<div>
|
||||
<label for="bar_0">
|
||||
<input checked type="radio" id="bar_0" value="J" name="beatle">John</label>
|
||||
</div>
|
||||
<div><label for="bar_1">
|
||||
<input type="radio" id="bar_1" value="P" name="beatle">Paul</label>
|
||||
</div>
|
||||
<div><label for="bar_2">
|
||||
<input type="radio" id="bar_2" value="G" name="beatle">George</label>
|
||||
</div>
|
||||
<div><label for="bar_3">
|
||||
<input type="radio" id="bar_3" value="R" name="beatle">Ringo</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(
|
||||
@ -126,15 +395,18 @@ class RadioSelectTest(WidgetTest):
|
||||
"""
|
||||
html = """
|
||||
<div class="bar">
|
||||
<div><label>
|
||||
<input checked type="radio" class="bar" value="J" name="beatle"> John</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input type="radio" class="bar" value="P" name="beatle"> Paul</label></div>
|
||||
<div><label>
|
||||
<input type="radio" class="bar" value="G" name="beatle"> George</label></div>
|
||||
<div><label>
|
||||
<input type="radio" class="bar" value="R" name="beatle"> Ringo</label></div>
|
||||
<div><label>
|
||||
<input checked type="radio" class="bar" value="J" name="beatle">John</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input type="radio" class="bar" value="P" name="beatle">Paul</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input type="radio" class="bar" value="G" name="beatle">George</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input type="radio" class="bar" value="R" name="beatle">Ringo</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(
|
||||
@ -154,11 +426,13 @@ class RadioSelectTest(WidgetTest):
|
||||
]
|
||||
html = """
|
||||
<div>
|
||||
<div><label><input type="radio" name="number" value="1"> One</label></div>
|
||||
<div><label>
|
||||
<input type="radio" name="number" value="1000"> One thousand</label></div>
|
||||
<div><label>
|
||||
<input type="radio" name="number" value="1000000"> One million</label></div>
|
||||
<div><label><input type="radio" name="number" value="1">One</label></div>
|
||||
<div>
|
||||
<label><input type="radio" name="number" value="1000">One thousand</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="number" value="1000000">One million</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(self.widget(choices=choices), "number", None, html=html)
|
||||
@ -169,35 +443,44 @@ class RadioSelectTest(WidgetTest):
|
||||
]
|
||||
html = """
|
||||
<div>
|
||||
<div><label>
|
||||
<input type="radio" name="time" value="00:00:00"> midnight</label></div>
|
||||
<div><label>
|
||||
<input type="radio" name="time" value="12:00:00"> noon</label></div>
|
||||
<div>
|
||||
<label><input type="radio" name="time" value="00:00:00">midnight</label>
|
||||
</div>
|
||||
<div>
|
||||
<label><input type="radio" name="time" value="12:00:00">noon</label>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
self.check_html(self.widget(choices=choices), "time", None, html=html)
|
||||
|
||||
def test_render_as_subwidget(self):
|
||||
"""A RadioSelect as a subwidget of MultiWidget."""
|
||||
choices = (("", "------"),) + self.beatles
|
||||
choices = BLANK_CHOICE_DASH + self.beatles
|
||||
html = """
|
||||
<div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value="">------</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input checked type="radio" name="beatle_0" value="J">John</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value="P">Paul</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value="G">George</label>
|
||||
</div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value="R">Ringo</label>
|
||||
</div>
|
||||
</div>
|
||||
<input name="beatle_1" type="text" value="Some text">
|
||||
"""
|
||||
self.check_html(
|
||||
MultiWidget([self.widget(choices=choices)]),
|
||||
MultiWidget([self.widget(choices=choices), TextInput()]),
|
||||
"beatle",
|
||||
["J"],
|
||||
html="""
|
||||
<div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value=""> ------</label></div>
|
||||
<div><label>
|
||||
<input checked type="radio" name="beatle_0" value="J"> John</label></div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value="P"> Paul</label></div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value="G"> George</label></div>
|
||||
<div><label>
|
||||
<input type="radio" name="beatle_0" value="R"> Ringo</label></div>
|
||||
</div>
|
||||
""",
|
||||
["J", "Some text"],
|
||||
html=html,
|
||||
)
|
||||
|
||||
def test_fieldset(self):
|
||||
|
@ -1,21 +1,14 @@
|
||||
import copy
|
||||
import datetime
|
||||
|
||||
from django.forms import ChoiceField, Form, Select
|
||||
from django.forms import ChoiceField, Form, MultiWidget, Select, TextInput
|
||||
from django.test import override_settings
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from .base import WidgetTest
|
||||
from .test_choicewidget import ChoiceWidgetTest
|
||||
|
||||
|
||||
class SelectTest(WidgetTest):
|
||||
class SelectTest(ChoiceWidgetTest):
|
||||
widget = Select
|
||||
nested_widget = Select(
|
||||
choices=(
|
||||
("outer1", "Outer 1"),
|
||||
('Group "1"', (("inner1", "Inner 1"), ("inner2", "Inner 2"))),
|
||||
)
|
||||
)
|
||||
|
||||
def test_render(self):
|
||||
self.check_html(
|
||||
@ -319,27 +312,6 @@ class SelectTest(WidgetTest):
|
||||
"""
|
||||
self.check_html(self.widget(choices=choices), "time", None, html=html)
|
||||
|
||||
def test_options(self):
|
||||
options = list(
|
||||
self.widget(choices=self.beatles).options(
|
||||
"name",
|
||||
["J"],
|
||||
attrs={"class": "super"},
|
||||
)
|
||||
)
|
||||
self.assertEqual(len(options), 4)
|
||||
self.assertEqual(options[0]["name"], "name")
|
||||
self.assertEqual(options[0]["value"], "J")
|
||||
self.assertEqual(options[0]["label"], "John")
|
||||
self.assertEqual(options[0]["index"], "0")
|
||||
self.assertIs(options[0]["selected"], True)
|
||||
# Template-related attributes
|
||||
self.assertEqual(options[1]["name"], "name")
|
||||
self.assertEqual(options[1]["value"], "P")
|
||||
self.assertEqual(options[1]["label"], "Paul")
|
||||
self.assertEqual(options[1]["index"], "1")
|
||||
self.assertIs(options[1]["selected"], False)
|
||||
|
||||
def test_optgroups(self):
|
||||
choices = [
|
||||
(
|
||||
@ -446,46 +418,35 @@ class SelectTest(WidgetTest):
|
||||
)
|
||||
self.assertEqual(index, 2)
|
||||
|
||||
def test_optgroups_integer_choices(self):
|
||||
"""The option 'value' is the same type as what's in `choices`."""
|
||||
groups = list(
|
||||
self.widget(choices=[[0, "choice text"]]).optgroups("name", ["vhs"])
|
||||
)
|
||||
label, options, index = groups[0]
|
||||
self.assertEqual(options[0]["value"], 0)
|
||||
|
||||
def test_deepcopy(self):
|
||||
"""
|
||||
__deepcopy__() should copy all attributes properly (#25085).
|
||||
"""
|
||||
widget = Select()
|
||||
obj = copy.deepcopy(widget)
|
||||
self.assertIsNot(widget, obj)
|
||||
self.assertEqual(widget.choices, obj.choices)
|
||||
self.assertIsNot(widget.choices, obj.choices)
|
||||
self.assertEqual(widget.attrs, obj.attrs)
|
||||
self.assertIsNot(widget.attrs, obj.attrs)
|
||||
|
||||
def test_doesnt_render_required_when_impossible_to_select_empty_field(self):
|
||||
widget = self.widget(choices=[("J", "John"), ("P", "Paul")])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), False)
|
||||
|
||||
def test_renders_required_when_possible_to_select_empty_field_str(self):
|
||||
widget = self.widget(choices=[("", "select please"), ("P", "Paul")])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), True)
|
||||
|
||||
def test_renders_required_when_possible_to_select_empty_field_list(self):
|
||||
widget = self.widget(choices=[["", "select please"], ["P", "Paul"]])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), True)
|
||||
|
||||
def test_renders_required_when_possible_to_select_empty_field_none(self):
|
||||
widget = self.widget(choices=[(None, "select please"), ("P", "Paul")])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), True)
|
||||
|
||||
def test_doesnt_render_required_when_no_choices_are_available(self):
|
||||
widget = self.widget(choices=[])
|
||||
self.assertIs(widget.use_required_attribute(initial=None), False)
|
||||
|
||||
def test_render_as_subwidget(self):
|
||||
"""A RadioSelect as a subwidget of MultiWidget."""
|
||||
choices = (("", "------"),) + self.beatles
|
||||
self.check_html(
|
||||
MultiWidget([self.widget(choices=choices), TextInput()]),
|
||||
"beatle",
|
||||
["J", "Some text"],
|
||||
html=(
|
||||
"""
|
||||
<select name="beatle_0">
|
||||
<option value="">------</option>
|
||||
<option value="J" selected>John</option>
|
||||
<option value="P">Paul</option>
|
||||
<option value="G">George</option>
|
||||
<option value="R">Ringo</option>
|
||||
</select>
|
||||
<input name="beatle_1" type="text" value="Some text">
|
||||
"""
|
||||
),
|
||||
)
|
||||
|
||||
def test_fieldset(self):
|
||||
class TestForm(Form):
|
||||
template_name = "forms_tests/use_fieldset.html"
|
||||
|
Loading…
Reference in New Issue
Block a user