test_checkboxinput.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from django.forms import CheckboxInput
  2. from .base import WidgetTest
  3. class CheckboxInputTest(WidgetTest):
  4. widget = CheckboxInput()
  5. def test_render_empty(self):
  6. self.check_html(self.widget, 'is_cool', '', html='<input type="checkbox" name="is_cool">')
  7. def test_render_none(self):
  8. self.check_html(self.widget, 'is_cool', None, html='<input type="checkbox" name="is_cool">')
  9. def test_render_false(self):
  10. self.check_html(self.widget, 'is_cool', False, html='<input type="checkbox" name="is_cool">')
  11. def test_render_true(self):
  12. self.check_html(
  13. self.widget, 'is_cool', True,
  14. html='<input checked type="checkbox" name="is_cool">'
  15. )
  16. def test_render_value(self):
  17. """
  18. Using any value that's not in ('', None, False, True) will check the
  19. checkbox and set the 'value' attribute.
  20. """
  21. self.check_html(
  22. self.widget, 'is_cool', 'foo',
  23. html='<input checked type="checkbox" name="is_cool" value="foo">',
  24. )
  25. def test_render_int(self):
  26. """
  27. Integers are handled by value, not as booleans (#17114).
  28. """
  29. self.check_html(
  30. self.widget, 'is_cool', 0,
  31. html='<input checked type="checkbox" name="is_cool" value="0">',
  32. )
  33. self.check_html(
  34. self.widget, 'is_cool', 1,
  35. html='<input checked type="checkbox" name="is_cool" value="1">',
  36. )
  37. def test_render_check_test(self):
  38. """
  39. You can pass 'check_test' to the constructor. This is a callable that
  40. takes the value and returns True if the box should be checked.
  41. """
  42. widget = CheckboxInput(check_test=lambda value: value.startswith('hello'))
  43. self.check_html(widget, 'greeting', '', html=(
  44. '<input type="checkbox" name="greeting">'
  45. ))
  46. self.check_html(widget, 'greeting', 'hello', html=(
  47. '<input checked type="checkbox" name="greeting" value="hello">'
  48. ))
  49. self.check_html(widget, 'greeting', 'hello there', html=(
  50. '<input checked type="checkbox" name="greeting" value="hello there">'
  51. ))
  52. self.check_html(widget, 'greeting', 'hello & goodbye', html=(
  53. '<input checked type="checkbox" name="greeting" value="hello &amp; goodbye">'
  54. ))
  55. def test_render_check_exception(self):
  56. """
  57. Calling check_test() shouldn't swallow exceptions (#17888).
  58. """
  59. widget = CheckboxInput(
  60. check_test=lambda value: value.startswith('hello'),
  61. )
  62. with self.assertRaises(AttributeError):
  63. widget.render('greeting', True)
  64. def test_value_from_datadict(self):
  65. """
  66. The CheckboxInput widget will return False if the key is not found in
  67. the data dictionary (because HTML form submission doesn't send any
  68. result for unchecked checkboxes).
  69. """
  70. self.assertFalse(self.widget.value_from_datadict({}, {}, 'testing'))
  71. def test_value_from_datadict_string_int(self):
  72. value = self.widget.value_from_datadict({'testing': '0'}, {}, 'testing')
  73. self.assertIs(value, True)
  74. def test_value_omitted_from_data(self):
  75. self.assertIs(self.widget.value_omitted_from_data({'field': 'value'}, {}, 'field'), False)
  76. self.assertIs(self.widget.value_omitted_from_data({}, {}, 'field'), False)