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

vega: Fix custom templates when data is not the first dict in the t… #132

Merged
merged 1 commit into from
May 16, 2023
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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tests =
%(markdown)s
funcy>=1.17
pytest==7.2.0
pytest-sugar==0.9.5
pytest-sugar>=0.9.6,<1.0
pytest-cov==3.0.0
pytest-mock==3.8.2
pylint==2.15.0
Expand Down
3 changes: 2 additions & 1 deletion src/dvc_render/vega_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def list_replace_value(l: list, name: str, value: str) -> list: # noqa: E741
def dict_find_value(d: dict, value: str) -> bool:
for v in d.values():
if isinstance(v, dict):
return dict_find_value(v, value)
if dict_find_value(v, value):
return True
if isinstance(v, str):
if v == value:
return True
Expand Down
22 changes: 21 additions & 1 deletion tests/test_vega.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import pytest

from dvc_render.vega import BadTemplateError, VegaRenderer
Expand Down Expand Up @@ -103,11 +105,29 @@ def test_confusion():
assert plot_content["spec"]["encoding"]["y"]["field"] == "actual"


def test_bad_template():
def test_bad_template_on_init():
with pytest.raises(BadTemplateError):
Template("name", "content")


def test_bad_template_on_missing_data(tmp_dir):
template_content = {"data": {"values": "BAD_ANCHOR"}}
tmp_dir.gen("bar.json", json.dumps(template_content))
datapoints = [{"val": 2}, {"val": 3}]
renderer = VegaRenderer(datapoints, "foo", template="bar.json")

with pytest.raises(BadTemplateError):
renderer.get_filled_template()

template_content = {
"mark": {"type": "bar"},
"data": {"values": Template.anchor("data")},
}
tmp_dir.gen("bar.json", json.dumps(template_content))
renderer = VegaRenderer(datapoints, "foo", template="bar.json")
assert renderer.get_filled_template()


def test_raise_on_wrong_field():
datapoints = [{"val": 2}, {"val": 3}]
props = {"x": "no_val"}
Expand Down