diff --git a/tests/test_forms.py b/tests/test_forms.py index 3cefda0f..622991ef 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -29,22 +29,15 @@ def test_show_help(self): res = render_form_field("subject") self.assertIn("my_help_text", res) self.assertNotIn("my_help_text", res) - res = render_template_with_form( - "{% bootstrap_field form.subject show_help=0 %}" - ) + res = render_template_with_form("{% bootstrap_field form.subject show_help=0 %}") self.assertNotIn("my_help_text", res) def test_help_with_quotes(self): # Checkboxes get special handling, so test a checkbox and something else res = render_form_field("sender") - self.assertIn( - 'title="{}"'.format(escape(TestForm.base_fields["sender"].help_text)), res - ) + self.assertIn('title="{}"'.format(escape(TestForm.base_fields["sender"].help_text)), res) res = render_form_field("cc_myself") - self.assertIn( - 'title="{}"'.format(escape(TestForm.base_fields["cc_myself"].help_text)), - res, - ) + self.assertIn('title="{}"'.format(escape(TestForm.base_fields["cc_myself"].help_text)), res) def test_subject(self): res = render_form_field("subject") @@ -61,12 +54,7 @@ def test_xss_field(self): ), res, ) - self.assertIn( - ( - 'placeholder="XSS" onmouseover="alert('Hello, XSS')" foo=""' - ), - res, - ) + self.assertIn(('placeholder="XSS" onmouseover="alert('Hello, XSS')" foo=""'), res) def test_password(self): res = render_form_field("password") @@ -97,8 +85,7 @@ def test_radio_select(self): ) if DJANGO_VERSION >= 4: expected_html = expected_html.replace( - '', - "", + '', "" ) self.assertHTMLEqual(res, expected_html) @@ -108,49 +95,23 @@ def test_checkbox(self): # strip out newlines and spaces around newlines res = "".join(line.strip() for line in res.split("\n")) res = BeautifulSoup(res, "html.parser") - form_group = self._select_one_element( - res, ".form-group", "Checkbox should be rendered inside a .form-group." - ) + form_group = self._select_one_element(res, ".form-group", "Checkbox should be rendered inside a .form-group.") form_check = self._select_one_element( - form_group, - ".form-check", - "There should be a .form-check inside .form-group", + form_group, ".form-check", "There should be a .form-check inside .form-group" ) - checkbox = self._select_one_element( - form_check, "input", "The checkbox should be inside the .form-check" - ) - self.assertIn( - "form-check-input", - checkbox["class"], - "The checkbox should have the class 'form-check-input'.", - ) - label = checkbox.next_sibling + checkbox = self._select_one_element(form_check, "input", "The checkbox should be inside the .form-check") + self.assertIn("form-check-input", checkbox["class"], "The checkbox should have the class 'form-check-input'.") + label = checkbox.nextSibling self.assertIsNotNone(label, "The label should be rendered after the checkbox.") + self.assertEqual(label.name, "label", "After the checkbox there should be a label.") self.assertEqual( - label.name, "label", "After the checkbox there should be a label." - ) - self.assertEqual( - label["for"], - checkbox["id"], - "The for attribute of the label should be the id of the checkbox.", - ) - help_text = label.next_sibling - self.assertIsNotNone( - help_text, "The help text should be rendered after the label." - ) - self.assertEqual( - help_text.name, "small", "The help text should be rendered as tag." - ) - self.assertIn( - "form-text", - help_text["class"], - "The help text should have the class 'form-text'.", - ) - self.assertIn( - "text-muted", - help_text["class"], - "The help text should have the class 'text-muted'.", + label["for"], checkbox["id"], "The for attribute of the label should be the id of the checkbox." ) + help_text = label.nextSibling + self.assertIsNotNone(help_text, "The help text should be rendered after the label.") + self.assertEqual(help_text.name, "small", "The help text should be rendered as tag.") + self.assertIn("form-text", help_text["class"], "The help text should have the class 'form-text'.") + self.assertIn("text-muted", help_text["class"], "The help text should have the class 'text-muted'.") def test_checkbox_multiple_select(self): res = render_form_field("category2") @@ -187,9 +148,7 @@ def test_required_field(self): # Required settings in field form_field = "form.subject" rendered = render_template_with_form( - "{% bootstrap_field " - + form_field - + ' required_css_class="test-required" %}' + "{% bootstrap_field " + form_field + ' required_css_class="test-required" %}' ) self.assertIn("test-required", rendered) @@ -204,16 +163,10 @@ def test_empty_permitted(self): self.assertNotIn(required_css_class, res) def test_input_group(self): - res = render_template_with_form( - '{% bootstrap_field form.subject addon_before="$" addon_after=".00" %}' - ) + res = render_template_with_form('{% bootstrap_field form.subject addon_before="$" addon_after=".00" %}') self.assertIn('class="input-group"', res) - self.assertIn( - 'class="input-group-prepend">$', res - ) - self.assertIn( - 'class="input-group-append">.00', res - ) + self.assertIn('class="input-group-prepend">$', res) + self.assertIn('class="input-group-append">.00', res) def test_input_group_addon_button(self): res = render_template_with_form( @@ -227,15 +180,10 @@ def test_input_group_addon_button(self): self.assertIn('
.00
', res) def test_input_group_addon_empty(self): - res = render_template_with_form( - '{% bootstrap_field form.subject addon_before=None addon_after="after" %}' - ) # noqa + res = render_template_with_form('{% bootstrap_field form.subject addon_before=None addon_after="after" %}') # noqa self.assertIn('class="input-group"', res) self.assertNotIn("input-group-prepend", res) - self.assertIn( - '
after
', - res, - ) + self.assertIn('
after
', res) def test_input_group_addon_validation(self): """ @@ -246,8 +194,7 @@ def test_input_group_addon_validation(self): # invalid form data: data = {"subject": ""} res = render_template_with_form( - '{% bootstrap_field form.subject addon_before=None addon_after="after" %}', - data=data, + '{% bootstrap_field form.subject addon_before=None addon_after="after" %}', data=data ) # noqa res = BeautifulSoup(res, "html.parser") self._select_one_element( @@ -257,34 +204,26 @@ def test_input_group_addon_validation(self): "required, must be placed inside the input-group", ) self._select_one_element( - res, - ".form-group > .form-text", - "The form-text message must be placed inside the form-group", + res, ".form-group > .form-text", "The form-text message must be placed inside the form-group" ) self.assertEqual( len(res.select(".form-group > .invalid-feedback")), 0, - "The invalid-feedback message must be placed inside the " - "input-group and not inside the form-group", + "The invalid-feedback message must be placed inside the " "input-group and not inside the form-group", ) self.assertEqual( len(res.select(".input-group > .form-text")), 0, - "The form-text message must be placed inside the form-group and " - "not inside the input-group", + "The form-text message must be placed inside the form-group and " "not inside the input-group", ) def test_size(self): def _test_size(param, klass): - res = render_template_with_form( - '{% bootstrap_field form.subject size="' + param + '" %}' - ) + res = render_template_with_form('{% bootstrap_field form.subject size="' + param + '" %}') self.assertIn(klass, res) def _test_size_medium(param): - res = render_template_with_form( - '{% bootstrap_field form.subject size="' + param + '" %}' - ) + res = render_template_with_form('{% bootstrap_field form.subject size="' + param + '" %}') self.assertNotIn("form-control-lg", res) self.assertNotIn("form-control-sm", res) self.assertNotIn("form-control-md", res) @@ -309,9 +248,7 @@ def test_field_same_render(self): self.assertEqual(rendered_a, rendered_b) def test_label(self): - res = render_template_with_form( - '{% bootstrap_label "foobar" label_for="subject" %}' - ) + res = render_template_with_form('{% bootstrap_label "foobar" label_for="subject" %}') self.assertEqual('', res) def test_attributes_consistency(self): @@ -322,9 +259,7 @@ def test_attributes_consistency(self): class ComponentsTest(TestCase): def test_bootstrap_alert(self): - res = render_template_with_form( - '{% bootstrap_alert "content" alert_type="danger" %}' - ) + res = render_template_with_form('{% bootstrap_alert "content" alert_type="danger" %}') self.assertEqual( res.strip(), '