2015-08-31 10:13:42 +08:00
|
|
|
from django.forms import CheckboxInput
|
|
|
|
|
|
|
|
from .base import WidgetTest
|
|
|
|
|
|
|
|
|
|
|
|
class CheckboxInputTest(WidgetTest):
|
|
|
|
widget = CheckboxInput()
|
|
|
|
|
|
|
|
def test_render_empty(self):
|
|
|
|
self.check_html(self.widget, 'is_cool', '', html='<input type="checkbox" name="is_cool" />')
|
|
|
|
|
|
|
|
def test_render_none(self):
|
|
|
|
self.check_html(self.widget, 'is_cool', None, html='<input type="checkbox" name="is_cool" />')
|
|
|
|
|
|
|
|
def test_render_false(self):
|
|
|
|
self.check_html(self.widget, 'is_cool', False, html='<input type="checkbox" name="is_cool" />')
|
|
|
|
|
|
|
|
def test_render_true(self):
|
|
|
|
self.check_html(
|
|
|
|
self.widget, 'is_cool', True,
|
2016-07-22 13:05:17 +08:00
|
|
|
html='<input checked type="checkbox" name="is_cool" />'
|
2015-08-31 10:13:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_value(self):
|
|
|
|
"""
|
|
|
|
Using any value that's not in ('', None, False, True) will check the
|
|
|
|
checkbox and set the 'value' attribute.
|
|
|
|
"""
|
|
|
|
self.check_html(
|
|
|
|
self.widget, 'is_cool', 'foo',
|
2016-07-22 13:05:17 +08:00
|
|
|
html='<input checked type="checkbox" name="is_cool" value="foo" />',
|
2015-08-31 10:13:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_int(self):
|
|
|
|
"""
|
|
|
|
Integers are handled by value, not as booleans (#17114).
|
|
|
|
"""
|
|
|
|
self.check_html(
|
|
|
|
self.widget, 'is_cool', 0,
|
2016-07-22 13:05:17 +08:00
|
|
|
html='<input checked type="checkbox" name="is_cool" value="0" />',
|
2015-08-31 10:13:42 +08:00
|
|
|
)
|
|
|
|
self.check_html(
|
|
|
|
self.widget, 'is_cool', 1,
|
2016-07-22 13:05:17 +08:00
|
|
|
html='<input checked type="checkbox" name="is_cool" value="1" />',
|
2015-08-31 10:13:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_render_check_test(self):
|
|
|
|
"""
|
|
|
|
You can pass 'check_test' to the constructor. This is a callable that
|
|
|
|
takes the value and returns True if the box should be checked.
|
|
|
|
"""
|
|
|
|
widget = CheckboxInput(check_test=lambda value: value.startswith('hello'))
|
|
|
|
self.check_html(widget, 'greeting', '', html=(
|
|
|
|
'<input type="checkbox" name="greeting" />'
|
|
|
|
))
|
|
|
|
self.check_html(widget, 'greeting', 'hello', html=(
|
2016-07-22 13:05:17 +08:00
|
|
|
'<input checked type="checkbox" name="greeting" value="hello" />'
|
2015-08-31 10:13:42 +08:00
|
|
|
))
|
|
|
|
self.check_html(widget, 'greeting', 'hello there', html=(
|
2016-07-22 13:05:17 +08:00
|
|
|
'<input checked type="checkbox" name="greeting" value="hello there" />'
|
2015-08-31 10:13:42 +08:00
|
|
|
))
|
|
|
|
self.check_html(widget, 'greeting', 'hello & goodbye', html=(
|
2016-07-22 13:05:17 +08:00
|
|
|
'<input checked type="checkbox" name="greeting" value="hello & goodbye" />'
|
2015-08-31 10:13:42 +08:00
|
|
|
))
|
|
|
|
|
|
|
|
def test_render_check_exception(self):
|
|
|
|
"""
|
|
|
|
Calling check_test() shouldn't swallow exceptions (#17888).
|
|
|
|
"""
|
|
|
|
widget = CheckboxInput(
|
|
|
|
check_test=lambda value: value.startswith('hello'),
|
|
|
|
)
|
|
|
|
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
widget.render('greeting', True)
|
|
|
|
|
|
|
|
def test_value_from_datadict(self):
|
|
|
|
"""
|
|
|
|
The CheckboxInput widget will return False if the key is not found in
|
|
|
|
the data dictionary (because HTML form submission doesn't send any
|
|
|
|
result for unchecked checkboxes).
|
|
|
|
"""
|
|
|
|
self.assertFalse(self.widget.value_from_datadict({}, {}, 'testing'))
|
|
|
|
|
|
|
|
def test_value_from_datadict_string_int(self):
|
|
|
|
value = self.widget.value_from_datadict({'testing': '0'}, {}, 'testing')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(value, True)
|