Files
dvc/tests/unit/render/test_render.py
T
Paweł Redzyński ab08db5065 plots: introduce support for images (#6431)
* plots: support images initial

* plots: data: unify PlotsData

* plots: data conversion refactor

* plots: renderers: abstract functionality

* plots: uncommenting tests

* plots: self implement grouping

* plots: rename group

* plots: extract rendering tests

* plots: move rendering tests to unit

* plots: add image rendering tests

* plots: dont use dpath to find vega plot

* plots: test find_vega

* plots: test render function

* plots: move rendering out of repo package

* plots: refactoring

* plots: self review refactor
2021-08-26 20:00:31 +03:00

68 lines
2.1 KiB
Python

import os
from dvc.render.utils import find_vega, render
def assert_website_has_image(page_path, revision, filename, image_content):
index_path = page_path / "index.html"
assert index_path.is_file()
index_content = index_path.read_text()
resources_filename = f"{revision}_{filename.replace(os.sep, '_')}"
image_path = page_path / "static" / resources_filename
assert image_path.is_file()
img_html = f'<img src="{os.path.join("static", resources_filename)}">'
assert img_html in index_content
with open(image_path, "rb") as fobj:
assert fobj.read() == image_content
def test_render(tmp_dir, dvc):
data = {
"HEAD": {
"data": {
"file.json": {
"data": [{"y": 5}, {"y": 6}],
"props": {"fields": {"y"}},
},
os.path.join("sub", "other_file.jpg"): {"data": b"content"},
}
},
"v2": {
"data": {
"file.json": {
"data": [{"y": 3}, {"y": 5}],
"props": {"fields": {"y"}},
},
"other_file.jpg": {"data": b"content2"},
}
},
"v1": {
"data": {
"some.csv": {
"data": [{"y": 2}, {"y": 3}],
"props": {"fields": {"y"}},
},
"another.gif": {"data": b"content3"},
}
},
}
render(dvc, data, path=os.path.join("results", "dir"))
page_path = tmp_dir / "results" / "dir"
index_path = page_path / "index.html"
assert index_path.is_file()
assert_website_has_image(
page_path, "HEAD", os.path.join("sub", "other_file.jpg"), b"content"
)
assert_website_has_image(page_path, "v2", "other_file.jpg", b"content2")
assert_website_has_image(page_path, "v1", "another.gif", b"content3")
index_content = index_path.read_text()
file_vega = find_vega(dvc, data, "file.json")
some_vega = find_vega(dvc, data, "some.csv")
assert file_vega in index_content.strip()
assert some_vega in index_content.strip()