Skip to content

Commit

Permalink
Fix tests for Django 5 (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed Dec 8, 2023
1 parent 12cd558 commit ba54f3f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ jobs:
run: pip install --upgrade pip hatch coveralls

- name: Install GDAL binaries
run: sudo apt-get install binutils libproj-dev gdal-bin
run: |
sudo apt-get update
sudo apt-get install binutils libproj-dev gdal-bin
- name: Run tests
run: hatch run test-cov
Expand Down
14 changes: 10 additions & 4 deletions src/bootstrap3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

from .text import text_value

# TODO: Remove after support for Django 3.2 ends
IS_PRE_DJANGO4 = get_version() < "4"

DJANGO_VERSION = get_version()
# TODO: Remove after support for Django 3.x ends
IS_PRE_DJANGO4 = DJANGO_VERSION < "4"
IS_DJANGO5 = DJANGO_VERSION >= "5"

# RegEx for quoted string
QUOTED_STRING = re.compile(r'^["\'](?P<noquotes>.+)["\']$')
Expand Down Expand Up @@ -106,7 +107,12 @@ def render_tag(tag, attrs=None, content=None, close=True):
builder = "<{tag}{attrs}>{content}"
if content or close:
builder += "</{tag}>"
return format_html(builder, tag=tag, attrs=mark_safe(flatatt(attrs)) if attrs else "", content=text_value(content))
return format_html(
builder,
tag=tag,
attrs=mark_safe(flatatt(attrs)) if attrs else "",
content=text_value(content),
)


def render_template_file(template, context=None):
Expand Down
49 changes: 40 additions & 9 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

from bootstrap3.exceptions import BootstrapError
from bootstrap3.text import text_concat, text_value
from bootstrap3.utils import IS_PRE_DJANGO4, add_css_class, render_tag, url_to_attrs_dict
from bootstrap3.utils import (
IS_DJANGO5,
IS_PRE_DJANGO4,
add_css_class,
render_tag,
url_to_attrs_dict,
)
from tests.app.forms import (
RadioSetTestForm,
SmallTestForm,
Expand Down Expand Up @@ -45,7 +51,10 @@ def test_field_names(self):
def test_field_addons(self):
form = TestForm()
res = render_form(form)
self.assertIn('<div class="input-group"><span class="input-group-addon">before</span><input', res)
self.assertIn(
'<div class="input-group"><span class="input-group-addon">before</span><input',
res,
)
self.assertIn('><span class="input-group-addon">after</span></div>', res)

def test_exclude(self):
Expand Down Expand Up @@ -78,7 +87,10 @@ def test_error_class(self):
res = render_template_with_form("{% bootstrap_form form %}", {"form": form})
self.assertIn("bootstrap3-err", res)

res = render_template_with_form('{% bootstrap_form form error_css_class="successful-test" %}', {"form": form})
res = render_template_with_form(
'{% bootstrap_form form error_css_class="successful-test" %}',
{"form": form},
)
self.assertIn("successful-test", res)

res = render_template_with_form('{% bootstrap_form form error_css_class="" %}', {"form": form})
Expand All @@ -90,7 +102,8 @@ def test_required_class(self):
self.assertIn("bootstrap3-req", res)

res = render_template_with_form(
'{% bootstrap_form form required_css_class="successful-test" %}', {"form": form}
'{% bootstrap_form form required_css_class="successful-test" %}',
{"form": form},
)
self.assertIn("successful-test", res)

Expand All @@ -103,7 +116,10 @@ def test_bound_class(self):
res = render_template_with_form("{% bootstrap_form form %}", {"form": form})
self.assertIn("bootstrap3-bound", res)

res = render_template_with_form('{% bootstrap_form form bound_css_class="successful-test" %}', {"form": form})
res = render_template_with_form(
'{% bootstrap_form form bound_css_class="successful-test" %}',
{"form": form},
)
self.assertIn("successful-test", res)

res = render_template_with_form('{% bootstrap_form form bound_css_class="" %}', {"form": form})
Expand Down Expand Up @@ -154,6 +170,8 @@ def test_illegal_field(self):

def test_checkbox(self):
res = render_form_field("cc_myself")
if IS_DJANGO5:
res = res.replace('aria-describedby="id_cc_myself_helptext"', "")
self.assertHTMLEqual(
"""
<div class="form-group">
Expand Down Expand Up @@ -424,13 +442,19 @@ def test_text_concat(self):
def test_render_tag(self):
self.assertEqual(render_tag("span"), "<span></span>")
self.assertEqual(render_tag("span", content="foo"), "<span>foo</span>")
self.assertEqual(render_tag("span", attrs={"bar": 123}, content="foo"), '<span bar="123">foo</span>')
self.assertEqual(
render_tag("span", attrs={"bar": 123}, content="foo"),
'<span bar="123">foo</span>',
)

def test_url_to_attrs_dict(self):
self.assertEqual(url_to_attrs_dict("my_link", "src"), {"src": "my_link"})
self.assertEqual(url_to_attrs_dict({"url": "my_link"}, "src"), {"src": "my_link"})
self.assertEqual(
url_to_attrs_dict({"url": "my_link", "crossorigin": "anonymous", "integrity": "super"}, "src"),
url_to_attrs_dict(
{"url": "my_link", "crossorigin": "anonymous", "integrity": "super"},
"src",
),
{"src": "my_link", "crossorigin": "anonymous", "integrity": "super"},
)
with self.assertRaises(BootstrapError):
Expand All @@ -457,7 +481,10 @@ def test_show_label(self):
def test_for_formset(self):
TestFormSet = formset_factory(TestForm, 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)

def test_button_with_icon(self):
Expand Down Expand Up @@ -551,4 +578,8 @@ def test_radioset_inline(self):
'<label class="sr-only" for="id_radio_0">Radio</label>',
)
self.assertHTMLEqual(res, expected)
self.assertIn(' <div class="radio">', res, msg="Missing relevant whitespace for inline rendering.")
self.assertIn(
' <div class="radio">',
res,
msg="Missing relevant whitespace for inline rendering.",
)

0 comments on commit ba54f3f

Please sign in to comment.