-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_plots.py
395 lines (314 loc) · 10.4 KB
/
test_plots.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import json
import os
import posixpath
from pathlib import Path
import pytest
from funcy import pluck_attr
from dvc.cli import parse_args
from dvc.commands.plots import CmdPlotsDiff, CmdPlotsShow, CmdPlotsTemplates
@pytest.fixture
def plots_data():
yield {
"revision": {
"data": {
"plot.csv": {"data": [{"val": 1}, {"val": 2}], "props": {}},
"other.jpg": {"data": b"content"},
}
}
}
def test_plots_diff(dvc, mocker, plots_data):
cli_args = parse_args(
[
"plots",
"diff",
"--out",
"result.extension",
"-t",
"template",
"--targets",
"datafile",
"--show-vega",
"-x",
"x_field",
"-y",
"y_field",
"--title",
"my_title",
"--x-label",
"x_title",
"--y-label",
"y_title",
"--experiment",
"HEAD",
"tag1",
"tag2",
]
)
assert cli_args.func == CmdPlotsDiff
cmd = cli_args.func(cli_args)
m = mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
render_mock = mocker.patch(
"dvc_render.render_html", return_value="html_path"
)
assert cmd.run() == 0
m.assert_called_once_with(
cmd.repo,
targets=["datafile"],
revs=["HEAD", "tag1", "tag2"],
props={
"template": "template",
"x": "x_field",
"y": "y_field",
"title": "my_title",
"x_label": "x_title",
"y_label": "y_title",
},
experiment=True,
)
render_mock.assert_not_called()
def test_plots_show_vega(dvc, mocker, plots_data):
cli_args = parse_args(
[
"plots",
"show",
"-o",
"result.extension",
"-t",
"template",
"--show-vega",
"--no-header",
"datafile",
]
)
assert cli_args.func == CmdPlotsShow
cmd = cli_args.func(cli_args)
m = mocker.patch(
"dvc.repo.plots.Plots.show",
return_value=plots_data,
)
render_mock = mocker.patch(
"dvc_render.render_html", return_value="html_path"
)
assert cmd.run() == 0
m.assert_called_once_with(
targets=["datafile"],
props={"template": "template", "header": False},
)
render_mock.assert_not_called()
def test_plots_diff_vega(dvc, mocker, capsys, plots_data):
cli_args = parse_args(
[
"plots",
"diff",
"HEAD~10",
"HEAD~1",
"--show-vega",
"--targets",
"plot.csv",
]
)
cmd = cli_args.func(cli_args)
mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
mocker.patch(
"dvc_render.VegaRenderer.get_filled_template",
return_value=json.dumps({"this": "is vega json"}),
)
render_mock = mocker.patch("dvc_render.render_html")
assert cmd.run() == 0
out, _ = capsys.readouterr()
assert json.dumps({"this": "is vega json"}) in out
render_mock.assert_not_called()
@pytest.mark.parametrize("auto_open", [True, False])
def test_plots_diff_open(tmp_dir, dvc, mocker, capsys, plots_data, auto_open):
mocked_open = mocker.patch("webbrowser.open", return_value=True)
args = ["plots", "diff", "--targets", "plots.csv"]
if auto_open:
with dvc.config.edit() as conf:
conf["plots"]["auto_open"] = True
else:
args.append("--open")
cli_args = parse_args(args)
cmd = cli_args.func(cli_args)
mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
index_path = tmp_dir / "dvc_plots" / "index.html"
mocker.patch("dvc_render.render_html", return_value=index_path)
assert cmd.run() == 0
mocked_open.assert_called_once_with(index_path.as_uri())
out, _ = capsys.readouterr()
assert index_path.as_uri() in out
def test_plots_diff_open_WSL(tmp_dir, dvc, mocker, plots_data):
mocked_open = mocker.patch("webbrowser.open", return_value=True)
mocked_uname_result = mocker.MagicMock()
mocked_uname_result.release = "Microsoft"
mocker.patch("platform.uname", return_value=mocked_uname_result)
cli_args = parse_args(
["plots", "diff", "--targets", "plots.csv", "--open"]
)
cmd = cli_args.func(cli_args)
mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
index_path = tmp_dir / "dvc_plots" / "index.html"
mocker.patch("dvc_render.render_html", return_value=index_path)
assert cmd.run() == 0
mocked_open.assert_called_once_with(str(Path("dvc_plots") / "index.html"))
def test_plots_diff_open_failed(tmp_dir, dvc, mocker, capsys, plots_data):
mocked_open = mocker.patch("webbrowser.open", return_value=False)
cli_args = parse_args(
["plots", "diff", "--targets", "plots.csv", "--open"]
)
cmd = cli_args.func(cli_args)
mocker.patch(
"dvc.repo.plots.diff.diff", return_value={"datafile": plots_data}
)
assert cmd.run() == 1
expected_url = tmp_dir / "dvc_plots" / "index.html"
mocked_open.assert_called_once_with(expected_url.as_uri())
error_message = (
f"Failed to open {expected_url.as_uri()}. "
"Please try opening it manually."
)
out, err = capsys.readouterr()
assert expected_url.as_uri() in out
assert error_message in err
@pytest.mark.parametrize(
"output, expected_url_path",
[
(
"plots file with spaces",
posixpath.join("plots%20file%20with%20spaces", "index.html"),
),
(
os.path.join("dir", "..", "plots"),
posixpath.join("plots", "index.html"),
),
],
ids=["quote", "resolve"],
)
def test_plots_path_is_quoted_and_resolved_properly(
tmp_dir, dvc, mocker, capsys, output, expected_url_path, plots_data
):
cli_args = parse_args(
["plots", "diff", "--targets", "datafile", "--out", output]
)
cmd = cli_args.func(cli_args)
mocker.patch(
"dvc.repo.plots.diff.diff", return_value={"datafile": plots_data}
)
assert cmd.run() == 0
expected_url = posixpath.join(tmp_dir.as_uri(), expected_url_path)
out, _ = capsys.readouterr()
assert expected_url in out
def test_should_pass_template_dir(tmp_dir, dvc, mocker, capsys):
cli_args = parse_args(
[
"plots",
"diff",
"HEAD~1",
"--json",
"--targets",
"plot.csv",
]
)
cmd = cli_args.func(cli_args)
data = mocker.MagicMock()
mocker.patch("dvc.repo.plots.diff.diff", return_value=data)
renderers = mocker.MagicMock()
match_renderers = mocker.patch(
"dvc.render.match.match_renderers", return_value=renderers
)
assert cmd.run() == 0
match_renderers.assert_called_once_with(
plots_data=data,
out="dvc_plots",
templates_dir=str(tmp_dir / ".dvc/plots"),
)
@pytest.mark.parametrize(
"output", ("some_out", os.path.join("to", "subdir"), None)
)
def test_should_call_render(tmp_dir, mocker, capsys, plots_data, output):
cli_args = parse_args(
["plots", "diff", "--targets", "plots.csv", "--out", output]
)
cmd = cli_args.func(cli_args)
mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
output = output or "dvc_plots"
index_path = tmp_dir / output / "index.html"
renderers = mocker.MagicMock()
mocker.patch("dvc.render.match.match_renderers", return_value=renderers)
render_mock = mocker.patch(
"dvc_render.render_html", return_value=index_path
)
assert cmd.run() == 0
out, _ = capsys.readouterr()
assert index_path.as_uri() in out
render_mock.assert_called_once_with(
renderers=renderers,
output_file=Path(tmp_dir / output / "index.html"),
template_path=None,
)
def test_plots_diff_json(dvc, mocker, capsys):
cli_args = parse_args(
[
"plots",
"diff",
"HEAD~10",
"HEAD~1",
"--json",
"--split",
"--targets",
"plot.csv",
"-o",
"out",
]
)
cmd = cli_args.func(cli_args)
data = mocker.MagicMock()
mocker.patch("dvc.repo.plots.diff.diff", return_value=data)
renderers = mocker.MagicMock()
mocker.patch("dvc.render.match.match_renderers", return_value=renderers)
render_mock = mocker.patch("dvc_render.render_html")
show_json_mock = mocker.patch("dvc.commands.plots._show_json")
assert cmd.run() == 0
show_json_mock.assert_called_once_with(renderers, True)
render_mock.assert_not_called()
@pytest.mark.parametrize("target", (("t1"), (None)))
def test_plots_templates(tmp_dir, dvc, mocker, capsys, target):
assert not os.path.exists(dvc.plots.templates_dir)
mocker.patch(
"dvc.commands.plots.CmdPlotsTemplates.TEMPLATES_CHOICES",
["t1", "t2"],
)
arguments = ["plots", "templates", "--out", "output"]
if target:
arguments += [target]
cli_args = parse_args(arguments)
assert cli_args.func == CmdPlotsTemplates
dump_mock = mocker.patch("dvc_render.vega_templates.dump_templates")
cmd = cli_args.func(cli_args)
assert cmd.run() == 0
out, _ = capsys.readouterr()
dump_mock.assert_called_once_with(
output=os.path.abspath("output"), targets=[target] if target else None
)
assert "Templates have been written into 'output'." in out
def test_plots_templates_choices(tmp_dir, dvc):
from dvc_render import TEMPLATES
assert CmdPlotsTemplates.TEMPLATES_CHOICES == list(
pluck_attr("DEFAULT_NAME", TEMPLATES)
)
@pytest.mark.parametrize("split", (True, False))
def test_show_json(split, mocker, capsys):
import dvc.commands.plots
renderer = mocker.MagicMock()
renderer.name = "rname"
to_json_mock = mocker.patch(
"dvc.render.convert.to_json", return_value={"renderer": "json"}
)
dvc.commands.plots._show_json([renderer], split)
to_json_mock.assert_called_once_with(renderer, split)
out, _ = capsys.readouterr()
assert json.dumps({"rname": {"renderer": "json"}}) in out
def test_show_json_no_renderers(capsys):
import dvc.commands.plots
dvc.commands.plots._show_json([])
out, _ = capsys.readouterr()
assert json.dumps({}) in out