Skip to content

Commit

Permalink
vega: Fix custom templates when data is not the first dict in the t…
Browse files Browse the repository at this point in the history
…emplate. (#132)

The `has_anchor` check was returning at the first dictionary encounter so even if the template had a valid `data` anchor, it would fail if it was not the first dictionary.
  • Loading branch information
daavoo committed May 16, 2023
1 parent a4099d3 commit dc5fb16
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
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

0 comments on commit dc5fb16

Please sign in to comment.