Skip to content

Commit

Permalink
Add tests for Django templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bittner committed Jun 19, 2024
1 parent e28030d commit 714935a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ source = "https://github.com/bittner/django-bootstrap-static"

[tool.pytest.ini_options]
addopts = "--color=yes --doctest-modules --verbose"
python_files = ["tests.py", "test_*.py", "*_tests.py"]
pythonpath = ["tests"]
DJANGO_SETTINGS_MODULE = "test_project.settings"
FAIL_INVALID_TEMPLATE_VARS = true

[tool.ruff]
extend-exclude = []
Expand Down
22 changes: 22 additions & 0 deletions tests/test_project/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Minimal Django settings for test project."""

INSTALLED_APPS = [
"django.contrib.staticfiles",
"bootstrap",
"fontawesome",
]

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
],
},
},
]

STATIC_URL = "static"
33 changes: 33 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Tests for Django templates."""

from django.template import Context, Template

template = """
{% load static %}
<head>
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}">
<script defer src="{% static 'fontawesome/js/all.min.js' %}"></script>
</head>
<body>
...
<script src="{% static 'bootstrap/js/jquery.min.js' %}"></script>
<script src="{% static 'bootstrap/js/bootstrap.bundle.min.js' %}"></script>
</body>
"""


def test_template():
"""Importing the package should work as described in the README."""
bootstrap_min_css = "bootstrap/css/bootstrap.min.css"
bootstrap_min_js = "bootstrap/js/bootstrap.bundle.min.js"
fontawesome_min_js = "fontawesome/js/all.min.js"
jquery_min_js = "bootstrap/js/jquery.min.js"

c = Context()
t = Template(template)
html = t.render(c)

assert f'<link rel="stylesheet" href="/static/{bootstrap_min_css}">' in html
assert f'<script defer src="/static/{fontawesome_min_js}"></script>' in html
assert f'<script src="/static/{jquery_min_js}"></script>' in html
assert f'<script src="/static/{bootstrap_min_js}"></script>' in html
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ envlist =

[testenv]
description = Unit tests and test coverage
deps = pytest
commands = pytest {posargs}
deps =
django
pytest-django
commands =
pytest {posargs}

[testenv:clean]
description = Remove bytecode and other debris
Expand Down

0 comments on commit 714935a

Please sign in to comment.