mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-27 23:09:28 +01:00
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
This commit is contained in:
@@ -7,7 +7,19 @@ from dvc.cli import parse_args
|
||||
from dvc.command.plots import CmdPlotsDiff, CmdPlotsShow
|
||||
|
||||
|
||||
def test_plots_diff(dvc, mocker):
|
||||
@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",
|
||||
@@ -38,8 +50,9 @@ def test_plots_diff(dvc, mocker):
|
||||
assert cli_args.func == CmdPlotsDiff
|
||||
|
||||
cmd = cli_args.func(cli_args)
|
||||
m = mocker.patch(
|
||||
"dvc.repo.plots.diff.diff", return_value={"datafile": "filledtemplate"}
|
||||
m = mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
|
||||
render_mock = mocker.patch(
|
||||
"dvc.command.plots.render", return_value="html_path"
|
||||
)
|
||||
|
||||
assert cmd.run() == 0
|
||||
@@ -58,9 +71,10 @@ def test_plots_diff(dvc, mocker):
|
||||
},
|
||||
experiment=True,
|
||||
)
|
||||
render_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_plots_show_vega(dvc, mocker):
|
||||
def test_plots_show_vega(dvc, mocker, plots_data):
|
||||
cli_args = parse_args(
|
||||
[
|
||||
"plots",
|
||||
@@ -80,7 +94,10 @@ def test_plots_show_vega(dvc, mocker):
|
||||
|
||||
m = mocker.patch(
|
||||
"dvc.repo.plots.Plots.show",
|
||||
return_value={"datafile": "filledtemplate"},
|
||||
return_value=plots_data,
|
||||
)
|
||||
render_mock = mocker.patch(
|
||||
"dvc.command.plots.render", return_value="html_path"
|
||||
)
|
||||
|
||||
assert cmd.run() == 0
|
||||
@@ -89,9 +106,10 @@ def test_plots_show_vega(dvc, mocker):
|
||||
targets=["datafile"],
|
||||
props={"template": "template", "header": False},
|
||||
)
|
||||
render_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_plots_diff_vega(dvc, mocker, capsys):
|
||||
def test_plots_diff_vega(dvc, mocker, capsys, plots_data):
|
||||
cli_args = parse_args(
|
||||
[
|
||||
"plots",
|
||||
@@ -100,72 +118,87 @@ def test_plots_diff_vega(dvc, mocker, capsys):
|
||||
"HEAD~1",
|
||||
"--show-vega",
|
||||
"--targets",
|
||||
"plots.csv",
|
||||
"plot.csv",
|
||||
]
|
||||
)
|
||||
cmd = cli_args.func(cli_args)
|
||||
mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)
|
||||
mocker.patch(
|
||||
"dvc.repo.plots.diff.diff", return_value={"plots.csv": "plothtml"}
|
||||
"dvc.command.plots.find_vega", return_value="vega_json_content"
|
||||
)
|
||||
render_mock = mocker.patch(
|
||||
"dvc.command.plots.render", return_value="html_path"
|
||||
)
|
||||
assert cmd.run() == 0
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert "plothtml" in out
|
||||
|
||||
assert "vega_json_content" in out
|
||||
render_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_plots_diff_open(tmp_dir, dvc, mocker, capsys):
|
||||
def test_plots_diff_open(tmp_dir, dvc, mocker, capsys, plots_data):
|
||||
mocked_open = mocker.patch("webbrowser.open", return_value=True)
|
||||
cli_args = parse_args(["plots", "diff", "--targets", "datafile", "--open"])
|
||||
cmd = cli_args.func(cli_args)
|
||||
mocker.patch(
|
||||
"dvc.repo.plots.diff.diff", return_value={"datafile": "filledtemplate"}
|
||||
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.command.plots.render", return_value=index_path)
|
||||
|
||||
assert cmd.run() == 0
|
||||
mocked_open.assert_called_once_with("plots.html")
|
||||
|
||||
expected_url = posixpath.join(tmp_dir.as_uri(), "plots.html")
|
||||
mocked_open.assert_called_once_with(index_path)
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert expected_url in out
|
||||
assert index_path.as_uri() in out
|
||||
|
||||
|
||||
def test_plots_diff_open_failed(tmp_dir, dvc, mocker, capsys):
|
||||
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", "datafile", "--open"])
|
||||
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": "filledtemplate"}
|
||||
"dvc.repo.plots.diff.diff", return_value={"datafile": plots_data}
|
||||
)
|
||||
|
||||
assert cmd.run() == 1
|
||||
mocked_open.assert_called_once_with("plots.html")
|
||||
expected_url = tmp_dir / "dvc_plots" / "index.html"
|
||||
mocked_open.assert_called_once_with(expected_url)
|
||||
|
||||
error_message = "Failed to open. Please try opening it manually."
|
||||
expected_url = posixpath.join(tmp_dir.as_uri(), "plots.html")
|
||||
|
||||
out, err = capsys.readouterr()
|
||||
assert expected_url in out
|
||||
assert expected_url.as_uri() in out
|
||||
assert error_message in err
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"output, expected_url_path",
|
||||
[
|
||||
("plots file with spaces.html", "plots%20file%20with%20spaces.html"),
|
||||
(os.path.join("dir", "..", "plots.html"), "plots.html"),
|
||||
(
|
||||
"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
|
||||
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": "filledtemplate"}
|
||||
"dvc.repo.plots.diff.diff", return_value={"datafile": plots_data}
|
||||
)
|
||||
|
||||
assert cmd.run() == 0
|
||||
@@ -173,3 +206,29 @@ def test_plots_path_is_quoted_and_resolved_properly(
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert expected_url in out
|
||||
|
||||
|
||||
@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"
|
||||
render_mock = mocker.patch(
|
||||
"dvc.command.plots.render", return_value=index_path
|
||||
)
|
||||
|
||||
assert cmd.run() == 0
|
||||
|
||||
out, _ = capsys.readouterr()
|
||||
assert index_path.as_uri() in out
|
||||
|
||||
render_mock.assert_called_once_with(
|
||||
cmd.repo, plots_data, path=tmp_dir / output, html_template_path=None
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user