2017-04-22 00:14:40 +08:00
|
|
|
import datetime
|
|
|
|
|
2015-08-31 10:13:42 +08:00
|
|
|
from django.forms import RadioSelect
|
2017-04-22 00:14:40 +08:00
|
|
|
from django.test import override_settings
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
from .base import WidgetTest
|
|
|
|
|
|
|
|
|
|
|
|
class RadioSelectTest(WidgetTest):
|
2016-04-29 06:48:52 +08:00
|
|
|
widget = RadioSelect
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
def test_render(self):
|
2017-03-09 22:42:47 +08:00
|
|
|
choices = (('', '------'),) + self.beatles
|
|
|
|
self.check_html(self.widget(choices=choices), 'beatle', 'J', html=(
|
2015-08-31 10:13:42 +08:00
|
|
|
"""<ul>
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label><input type="radio" name="beatle" value=""> ------</label></li>
|
|
|
|
<li><label><input checked type="radio" name="beatle" value="J"> John</label></li>
|
|
|
|
<li><label><input type="radio" name="beatle" value="P"> Paul</label></li>
|
|
|
|
<li><label><input type="radio" name="beatle" value="G"> George</label></li>
|
|
|
|
<li><label><input type="radio" name="beatle" value="R"> Ringo</label></li>
|
2015-08-31 10:13:42 +08:00
|
|
|
</ul>"""
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_nested_choices(self):
|
|
|
|
nested_choices = (
|
|
|
|
('unknown', 'Unknown'),
|
|
|
|
('Audio', (('vinyl', 'Vinyl'), ('cd', 'CD'))),
|
|
|
|
('Video', (('vhs', 'VHS'), ('dvd', 'DVD'))),
|
|
|
|
)
|
|
|
|
html = """
|
|
|
|
<ul id="media">
|
|
|
|
<li>
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="media_0"><input id="media_0" name="nestchoice" type="radio" value="unknown"> Unknown</label>
|
2015-08-31 10:13:42 +08:00
|
|
|
</li>
|
|
|
|
<li>Audio<ul id="media_1">
|
|
|
|
<li>
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="media_1_0"><input id="media_1_0" name="nestchoice" type="radio" value="vinyl"> Vinyl</label>
|
2015-08-31 10:13:42 +08:00
|
|
|
</li>
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label for="media_1_1"><input id="media_1_1" name="nestchoice" type="radio" value="cd"> CD</label></li>
|
2015-08-31 10:13:42 +08:00
|
|
|
</ul></li>
|
|
|
|
<li>Video<ul id="media_2">
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label for="media_2_0"><input id="media_2_0" name="nestchoice" type="radio" value="vhs"> VHS</label></li>
|
2015-08-31 10:13:42 +08:00
|
|
|
<li>
|
|
|
|
<label for="media_2_1">
|
2018-01-21 15:09:10 +08:00
|
|
|
<input checked id="media_2_1" name="nestchoice" type="radio" value="dvd"> DVD
|
2015-08-31 10:13:42 +08:00
|
|
|
</label>
|
|
|
|
</li>
|
|
|
|
</ul></li>
|
|
|
|
</ul>
|
|
|
|
"""
|
|
|
|
self.check_html(
|
2016-04-29 06:48:52 +08:00
|
|
|
self.widget(choices=nested_choices), 'nestchoice', 'dvd',
|
2015-08-31 10:13:42 +08:00
|
|
|
attrs={'id': 'media'}, html=html,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_constructor_attrs(self):
|
|
|
|
"""
|
|
|
|
Attributes provided at instantiation are passed to the constituent
|
|
|
|
inputs.
|
|
|
|
"""
|
2016-04-29 06:48:52 +08:00
|
|
|
widget = RadioSelect(attrs={'id': 'foo'}, choices=self.beatles)
|
2015-08-31 10:13:42 +08:00
|
|
|
html = """
|
|
|
|
<ul id="foo">
|
|
|
|
<li>
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="foo_0"><input checked type="radio" id="foo_0" value="J" name="beatle"> John</label>
|
2015-08-31 10:13:42 +08:00
|
|
|
</li>
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label for="foo_1"><input type="radio" id="foo_1" value="P" name="beatle"> Paul</label></li>
|
|
|
|
<li><label for="foo_2"><input type="radio" id="foo_2" value="G" name="beatle"> George</label></li>
|
|
|
|
<li><label for="foo_3"><input type="radio" id="foo_3" value="R" name="beatle"> Ringo</label></li>
|
2015-08-31 10:13:42 +08:00
|
|
|
</ul>
|
|
|
|
"""
|
2016-04-29 06:48:52 +08:00
|
|
|
self.check_html(widget, 'beatle', 'J', html=html)
|
2015-08-31 10:13:42 +08:00
|
|
|
|
|
|
|
def test_render_attrs(self):
|
|
|
|
"""
|
|
|
|
Attributes provided at render-time are passed to the constituent
|
|
|
|
inputs.
|
|
|
|
"""
|
|
|
|
html = """
|
|
|
|
<ul id="bar">
|
|
|
|
<li>
|
2018-01-21 15:09:10 +08:00
|
|
|
<label for="bar_0"><input checked type="radio" id="bar_0" value="J" name="beatle"> John</label>
|
2015-08-31 10:13:42 +08:00
|
|
|
</li>
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle"> Paul</label></li>
|
|
|
|
<li><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle"> George</label></li>
|
|
|
|
<li><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle"> Ringo</label></li>
|
2015-08-31 10:13:42 +08:00
|
|
|
</ul>
|
|
|
|
"""
|
2016-04-29 06:48:52 +08:00
|
|
|
self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', attrs={'id': 'bar'}, html=html)
|
2017-04-16 01:55:33 +08:00
|
|
|
|
|
|
|
def test_class_attrs(self):
|
|
|
|
"""
|
|
|
|
The <ul> in the multiple_input.html widget template include the class
|
|
|
|
attribute.
|
|
|
|
"""
|
|
|
|
html = """
|
|
|
|
<ul class="bar">
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label><input checked type="radio" class="bar" value="J" name="beatle"> John</label></li>
|
|
|
|
<li><label><input type="radio" class="bar" value="P" name="beatle"> Paul</label></li>
|
|
|
|
<li><label><input type="radio" class="bar" value="G" name="beatle"> George</label></li>
|
|
|
|
<li><label><input type="radio" class="bar" value="R" name="beatle"> Ringo</label></li>
|
2017-04-16 01:55:33 +08:00
|
|
|
</ul>
|
|
|
|
"""
|
|
|
|
self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', attrs={'class': 'bar'}, html=html)
|
2017-04-22 00:14:40 +08:00
|
|
|
|
|
|
|
@override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
|
|
|
|
def test_doesnt_localize_input_value(self):
|
|
|
|
choices = [
|
|
|
|
(1, 'One'),
|
|
|
|
(1000, 'One thousand'),
|
|
|
|
(1000000, 'One million'),
|
|
|
|
]
|
|
|
|
html = """
|
|
|
|
<ul>
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label><input type="radio" name="number" value="1"> One</label></li>
|
|
|
|
<li><label><input type="radio" name="number" value="1000"> One thousand</label></li>
|
|
|
|
<li><label><input type="radio" name="number" value="1000000"> One million</label></li>
|
2017-04-22 00:14:40 +08:00
|
|
|
</ul>
|
|
|
|
"""
|
|
|
|
self.check_html(self.widget(choices=choices), 'number', None, html=html)
|
|
|
|
|
|
|
|
choices = [
|
|
|
|
(datetime.time(0, 0), 'midnight'),
|
|
|
|
(datetime.time(12, 0), 'noon'),
|
|
|
|
]
|
|
|
|
html = """
|
|
|
|
<ul>
|
2018-01-21 15:09:10 +08:00
|
|
|
<li><label><input type="radio" name="time" value="00:00:00"> midnight</label></li>
|
|
|
|
<li><label><input type="radio" name="time" value="12:00:00"> noon</label></li>
|
2017-04-22 00:14:40 +08:00
|
|
|
</ul>
|
|
|
|
"""
|
|
|
|
self.check_html(self.widget(choices=choices), 'time', None, html=html)
|