Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Django templates #42

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "README.rst"
license = {file = "LICENSE"}
authors = [
{name = "Peter Bittner", email = "[email protected]"},
{name = "Derek Stegelman ", email = "[email protected]"},
]
maintainers = [
{name = "Peter Bittner", email = "[email protected]"},
Expand Down Expand Up @@ -41,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