Skip to content

Commit 56c9472

Browse files
✅ [#6] Add tests for PDF generation
1 parent 579eb2e commit 56c9472

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed

maykin_common/pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def get_match_candidate(
134134
return None
135135

136136

137-
def render_to_pdf(template_name: str, context: dict) -> tuple[str, bytes]:
137+
def render_to_pdf(template_name: str, context: dict[str, object]) -> tuple[str, bytes]:
138138
"""
139139
Render a (HTML) template to PDF with the given context.
140140
"""

testapp/static/testapp/some.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:root {
2+
--color-primary: purple;
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>External URL</title>
7+
<link href="https://example.com/index.css" rel="stylesheet">
8+
</head>
9+
<body>
10+
External URL.
11+
</body>
12+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>Hello world</title>
7+
</head>
8+
<body>
9+
Hello {{ world }}.
10+
</body>
11+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% load static %}
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Local URL</title>
8+
<link href="http://testserver/static/testapp/some.css" rel="stylesheet">
9+
<link href="{% static 'testapp/some.css' %}" rel="stylesheet">
10+
</head>
11+
<body>
12+
Local URL.
13+
</body>
14+
</html>

tests/pdf/test_pdf_generation.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from unittest.mock import patch
2+
3+
import pytest
4+
5+
from maykin_common.pdf import render_to_pdf
6+
7+
8+
def get_base_url():
9+
return "http://testserver"
10+
11+
12+
@pytest.fixture(autouse=True)
13+
def _settings(settings):
14+
settings.PDF_BASE_URL_FUNCTION = f"{__name__}.get_base_url"
15+
16+
17+
def test_raises_if_setting_not_configured_properly(settings):
18+
settings.PDF_BASE_URL_FUNCTION = None
19+
20+
with pytest.raises(NotImplementedError):
21+
render_to_pdf("testapp/pdf/hello_world.html", {})
22+
23+
24+
def test_render_template_returns_html():
25+
html, pdf = render_to_pdf("testapp/pdf/hello_world.html", {"world": "pytest"})
26+
27+
assert isinstance(html, str)
28+
assert "Hello pytest" in html
29+
assert isinstance(pdf, bytes)
30+
31+
32+
def test_external_url_uses_default_resolver():
33+
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher:
34+
render_to_pdf("testapp/pdf/external_url.html", {})
35+
36+
mock_fetcher.assert_called_once_with("https://example.com/index.css")
37+
38+
39+
def test_local_asset_does_not_use_default_resolver():
40+
with patch("maykin_common.pdf.weasyprint.default_url_fetcher") as mock_fetcher:
41+
render_to_pdf("testapp/pdf/local_url.html", {})
42+
43+
mock_fetcher.assert_not_called()

0 commit comments

Comments
 (0)