1. from django.forms import BooleanField, CheckboxInput, Form
    
  2. 
    
  3. from .base import WidgetTest
    
  4. 
    
  5. 
    
  6. class CheckboxInputTest(WidgetTest):
    
  7.     widget = CheckboxInput()
    
  8. 
    
  9.     def test_render_empty(self):
    
  10.         self.check_html(
    
  11.             self.widget, "is_cool", "", html='<input type="checkbox" name="is_cool">'
    
  12.         )
    
  13. 
    
  14.     def test_render_none(self):
    
  15.         self.check_html(
    
  16.             self.widget, "is_cool", None, html='<input type="checkbox" name="is_cool">'
    
  17.         )
    
  18. 
    
  19.     def test_render_false(self):
    
  20.         self.check_html(
    
  21.             self.widget, "is_cool", False, html='<input type="checkbox" name="is_cool">'
    
  22.         )
    
  23. 
    
  24.     def test_render_true(self):
    
  25.         self.check_html(
    
  26.             self.widget,
    
  27.             "is_cool",
    
  28.             True,
    
  29.             html='<input checked type="checkbox" name="is_cool">',
    
  30.         )
    
  31. 
    
  32.     def test_render_value(self):
    
  33.         """
    
  34.         Using any value that's not in ('', None, False, True) will check the
    
  35.         checkbox and set the 'value' attribute.
    
  36.         """
    
  37.         self.check_html(
    
  38.             self.widget,
    
  39.             "is_cool",
    
  40.             "foo",
    
  41.             html='<input checked type="checkbox" name="is_cool" value="foo">',
    
  42.         )
    
  43. 
    
  44.     def test_render_int(self):
    
  45.         """
    
  46.         Integers are handled by value, not as booleans (#17114).
    
  47.         """
    
  48.         self.check_html(
    
  49.             self.widget,
    
  50.             "is_cool",
    
  51.             0,
    
  52.             html='<input checked type="checkbox" name="is_cool" value="0">',
    
  53.         )
    
  54.         self.check_html(
    
  55.             self.widget,
    
  56.             "is_cool",
    
  57.             1,
    
  58.             html='<input checked type="checkbox" name="is_cool" value="1">',
    
  59.         )
    
  60. 
    
  61.     def test_render_check_test(self):
    
  62.         """
    
  63.         You can pass 'check_test' to the constructor. This is a callable that
    
  64.         takes the value and returns True if the box should be checked.
    
  65.         """
    
  66.         widget = CheckboxInput(check_test=lambda value: value.startswith("hello"))
    
  67.         self.check_html(
    
  68.             widget, "greeting", "", html=('<input type="checkbox" name="greeting">')
    
  69.         )
    
  70.         self.check_html(
    
  71.             widget,
    
  72.             "greeting",
    
  73.             "hello",
    
  74.             html=('<input checked type="checkbox" name="greeting" value="hello">'),
    
  75.         )
    
  76.         self.check_html(
    
  77.             widget,
    
  78.             "greeting",
    
  79.             "hello there",
    
  80.             html=(
    
  81.                 '<input checked type="checkbox" name="greeting" value="hello there">'
    
  82.             ),
    
  83.         )
    
  84.         self.check_html(
    
  85.             widget,
    
  86.             "greeting",
    
  87.             "hello & goodbye",
    
  88.             html=(
    
  89.                 '<input checked type="checkbox" name="greeting" '
    
  90.                 'value="hello &amp; goodbye">'
    
  91.             ),
    
  92.         )
    
  93. 
    
  94.     def test_render_check_exception(self):
    
  95.         """
    
  96.         Calling check_test() shouldn't swallow exceptions (#17888).
    
  97.         """
    
  98.         widget = CheckboxInput(
    
  99.             check_test=lambda value: value.startswith("hello"),
    
  100.         )
    
  101. 
    
  102.         with self.assertRaises(AttributeError):
    
  103.             widget.render("greeting", True)
    
  104. 
    
  105.     def test_value_from_datadict(self):
    
  106.         """
    
  107.         The CheckboxInput widget will return False if the key is not found in
    
  108.         the data dictionary (because HTML form submission doesn't send any
    
  109.         result for unchecked checkboxes).
    
  110.         """
    
  111.         self.assertFalse(self.widget.value_from_datadict({}, {}, "testing"))
    
  112. 
    
  113.     def test_value_from_datadict_string_int(self):
    
  114.         value = self.widget.value_from_datadict({"testing": "0"}, {}, "testing")
    
  115.         self.assertIs(value, True)
    
  116. 
    
  117.     def test_value_omitted_from_data(self):
    
  118.         self.assertIs(
    
  119.             self.widget.value_omitted_from_data({"field": "value"}, {}, "field"), False
    
  120.         )
    
  121.         self.assertIs(self.widget.value_omitted_from_data({}, {}, "field"), False)
    
  122. 
    
  123.     def test_get_context_does_not_mutate_attrs(self):
    
  124.         attrs = {"checked": False}
    
  125.         self.widget.get_context("name", True, attrs)
    
  126.         self.assertIs(attrs["checked"], False)
    
  127. 
    
  128.     def test_fieldset(self):
    
  129.         class TestForm(Form):
    
  130.             template_name = "forms_tests/use_fieldset.html"
    
  131.             field = BooleanField(widget=self.widget)
    
  132. 
    
  133.         form = TestForm()
    
  134.         self.assertIs(self.widget.use_fieldset, False)
    
  135.         self.assertHTMLEqual(
    
  136.             form.render(),
    
  137.             '<div><label for="id_field">Field:</label>'
    
  138.             '<input id="id_field" name="field" required type="checkbox"></div>',
    
  139.         )