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

plots: json: show even if no results #7550

Merged
merged 1 commit into from
Apr 7, 2022
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
3 changes: 1 addition & 2 deletions dvc/commands/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def _show_json(renderers, split=False):
result = {
renderer.name: to_json(renderer, split) for renderer in renderers
}
if result:
ui.write_json(result)
ui.write_json(result)


class CmdPlots(CmdBase):
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/command/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,30 @@ def test_plots_templates_choices(tmp_dir, dvc):
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