Skip to content

Commit

Permalink
Fix BeautifulSoup 4 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed Apr 24, 2024
1 parent 93b971b commit 347cda3
Showing 1 changed file with 109 additions and 35 deletions.
144 changes: 109 additions & 35 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ def test_show_help(self):
res = render_form_field("subject")
self.assertIn("my_help_text", res)
self.assertNotIn("<i>my_help_text</i>", 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")
Expand All @@ -54,7 +61,12 @@ def test_xss_field(self):
),
res,
)
self.assertIn(('placeholder="XSS&quot; onmouseover=&quot;alert(&#x27;Hello, XSS&#x27;)&quot; foo=&quot;"'), res)
self.assertIn(
(
'placeholder="XSS&quot; onmouseover=&quot;alert(&#x27;Hello, XSS&#x27;)&quot; foo=&quot;"'
),
res,
)

def test_password(self):
res = render_form_field("password")
Expand Down Expand Up @@ -85,7 +97,8 @@ def test_radio_select(self):
)
if DJANGO_VERSION >= 4:
expected_html = expected_html.replace(
'<label for="id_category1_0">Category1</label>', "<label>Category1</label>"
'<label for="id_category1_0">Category1</label>',
"<label>Category1</label>",
)
self.assertHTMLEqual(res, expected_html)

Expand All @@ -95,23 +108,49 @@ 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.nextSibling
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
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["for"], checkbox["id"], "The for attribute of the label should be the id of the checkbox."
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 <small> 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'.",
)
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 <small> 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")
Expand Down Expand Up @@ -148,7 +187,9 @@ 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)

Expand All @@ -163,10 +204,16 @@ 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"><span class="input-group-text">$', res)
self.assertIn('class="input-group-append"><span class="input-group-text">.00', res)
self.assertIn(
'class="input-group-prepend"><span class="input-group-text">$', res
)
self.assertIn(
'class="input-group-append"><span class="input-group-text">.00', res
)

def test_input_group_addon_button(self):
res = render_template_with_form(
Expand All @@ -180,10 +227,15 @@ def test_input_group_addon_button(self):
self.assertIn('<div class="input-group-append">.00</div>', 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('<div class="input-group-append"><span class="input-group-text">after</span></div>', res)
self.assertIn(
'<div class="input-group-append"><span class="input-group-text">after</span></div>',
res,
)

def test_input_group_addon_validation(self):
"""
Expand All @@ -194,7 +246,8 @@ 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(
Expand All @@ -204,26 +257,34 @@ 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)
Expand All @@ -248,7 +309,9 @@ 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('<label for="subject">foobar</label>', res)

def test_attributes_consistency(self):
Expand All @@ -259,7 +322,9 @@ 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(),
'<div class="alert alert-danger alert-dismissible" role="alert">'
Expand All @@ -272,21 +337,30 @@ def test_bootstrap_alert(self):
class ShowLabelTest(TestCase):
def test_show_label_false(self):
form = CharFieldTestForm()
res = render_template_with_form("{% bootstrap_form form show_label=False %}", {"form": form})
res = render_template_with_form(
"{% bootstrap_form form show_label=False %}", {"form": form}
)
self.assertIn("sr-only", res)

def test_show_label_sr_only(self):
form = CharFieldTestForm()
res = render_template_with_form("{% bootstrap_form form show_label='sr-only' %}", {"form": form})
res = render_template_with_form(
"{% bootstrap_form form show_label='sr-only' %}", {"form": form}
)
self.assertIn("sr-only", res)

def test_show_label_skip(self):
form = CharFieldTestForm()
res = render_template_with_form("{% bootstrap_form form show_label='skip' %}", {"form": form})
res = render_template_with_form(
"{% bootstrap_form form show_label='skip' %}", {"form": form}
)
self.assertNotIn("<label>", res)

def test_for_formset(self):
TestFormSet = formset_factory(CharFieldTestForm, extra=1)
test_formset = TestFormSet()
res = render_template_with_form("{% bootstrap_formset formset show_label=False %}", {"formset": test_formset})
res = render_template_with_form(
"{% bootstrap_formset formset show_label=False %}",
{"formset": test_formset},
)
self.assertIn("sr-only", res)

0 comments on commit 347cda3

Please sign in to comment.