-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_html.py
139 lines (117 loc) · 3.6 KB
/
test_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# pylint: disable=missing-function-docstring, R0801
import os
import pytest
from dvc_render.html import (
HTML,
PAGE_HTML,
MissingPlaceholderError,
_order_image_per_step,
render_html,
)
from dvc_render.image import ImageRenderer
from dvc_render.vega import VegaRenderer
CUSTOM_PAGE_HTML = """<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
<script type="text/javascript" src="vega"></script>
<script type="text/javascript" src="vega-lite"></script>
<script type="text/javascript" src="vega-embed"></script>
</head>
<body>
{plot_divs}
</body>
</html>"""
CSS_PAGE_HTML = """<!DOCTYPE html>
<html>
<head>
<title>TITLE</title>
<script type="text/javascript" src="vega"></script>
<script type="text/javascript" src="vega-lite"></script>
<script type="text/javascript" src="vega-embed"></script>
<style>
.test-malformed{
color: red;
}
</style>
</head>
<body>
{plot_divs}
</body>
</html>"""
@pytest.mark.parametrize(
("template", "page_elements", "expected_page"),
[
(
None,
["content"],
PAGE_HTML.replace("{plot_divs}", "content")
.replace("{scripts}", "")
.replace("{refresh_tag}", ""),
),
(
CUSTOM_PAGE_HTML,
["content"],
CUSTOM_PAGE_HTML.format(plot_divs="content"),
),
(
CSS_PAGE_HTML,
["content"],
CSS_PAGE_HTML.replace("{plot_divs}", "content"),
),
],
)
def test_html(template, page_elements, expected_page):
page = HTML(template)
page.elements = page_elements
result = page.embed()
assert result == expected_page
def test_render_html_with_custom_template(mocker, tmp_path):
output_file = tmp_path / "output_file"
render_html(mocker.MagicMock(), output_file)
assert output_file.read_text() == PAGE_HTML.replace("{plot_divs}", "").replace(
"{scripts}", ""
).replace("{refresh_tag}", "")
render_html(mocker.MagicMock(), output_file, CUSTOM_PAGE_HTML)
assert output_file.read_text() == CUSTOM_PAGE_HTML.format(plot_divs="")
custom_template = tmp_path / "custom_template"
custom_template.write_text(CUSTOM_PAGE_HTML)
render_html(mocker.MagicMock(), output_file, custom_template)
assert output_file.read_text() == CUSTOM_PAGE_HTML.format(plot_divs="")
def test_order_image_per_step():
image_per_step_dir = "dvclive"
other_image_dir = "static"
def create_renderer(filename: str) -> ImageRenderer:
return ImageRenderer(
[
{
"filename": filename,
"rev": "workspace",
"src": filename,
}
],
filename,
)
r1 = VegaRenderer([], "dvc.yaml::Loss")
r2 = VegaRenderer([], "dvc.yaml::Accuracy")
r3 = create_renderer(os.path.join(image_per_step_dir, "0.jpg"))
r4 = create_renderer(os.path.join(image_per_step_dir, "1.jpg"))
r5 = create_renderer(os.path.join(image_per_step_dir, "2.jpg"))
r6 = create_renderer(os.path.join(image_per_step_dir, "10.jpg"))
r7 = create_renderer(os.path.join(other_image_dir, "a_file.jpg"))
r8 = create_renderer(os.path.join(other_image_dir, "z_file.jpg"))
renderers = [r7, r3, r5, r8, r1, r6, r4, r2]
assert sorted(renderers, key=_order_image_per_step) == [
r1,
r2,
r3,
r4,
r5,
r6,
r7,
r8,
]
def test_no_placeholder():
template = "<head></head><body></body>"
with pytest.raises(MissingPlaceholderError):
HTML(template)