Skip to content

Commit dc5fb16

Browse files
authored
vega: Fix custom templates when data is not the first dict in the template. (#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.
1 parent a4099d3 commit dc5fb16

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tests =
4242
%(markdown)s
4343
funcy>=1.17
4444
pytest==7.2.0
45-
pytest-sugar==0.9.5
45+
pytest-sugar>=0.9.6,<1.0
4646
pytest-cov==3.0.0
4747
pytest-mock==3.8.2
4848
pylint==2.15.0

src/dvc_render/vega_templates.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def list_replace_value(l: list, name: str, value: str) -> list: # noqa: E741
6464
def dict_find_value(d: dict, value: str) -> bool:
6565
for v in d.values():
6666
if isinstance(v, dict):
67-
return dict_find_value(v, value)
67+
if dict_find_value(v, value):
68+
return True
6869
if isinstance(v, str):
6970
if v == value:
7071
return True

tests/test_vega.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
import pytest
24

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

105107

106-
def test_bad_template():
108+
def test_bad_template_on_init():
107109
with pytest.raises(BadTemplateError):
108110
Template("name", "content")
109111

110112

113+
def test_bad_template_on_missing_data(tmp_dir):
114+
template_content = {"data": {"values": "BAD_ANCHOR"}}
115+
tmp_dir.gen("bar.json", json.dumps(template_content))
116+
datapoints = [{"val": 2}, {"val": 3}]
117+
renderer = VegaRenderer(datapoints, "foo", template="bar.json")
118+
119+
with pytest.raises(BadTemplateError):
120+
renderer.get_filled_template()
121+
122+
template_content = {
123+
"mark": {"type": "bar"},
124+
"data": {"values": Template.anchor("data")},
125+
}
126+
tmp_dir.gen("bar.json", json.dumps(template_content))
127+
renderer = VegaRenderer(datapoints, "foo", template="bar.json")
128+
assert renderer.get_filled_template()
129+
130+
111131
def test_raise_on_wrong_field():
112132
datapoints = [{"val": 2}, {"val": 3}]
113133
props = {"x": "no_val"}

0 commit comments

Comments
 (0)