Skip to content

Commit

Permalink
metrics/plots: common tests: check for particular exceptions (#4865)
Browse files Browse the repository at this point in the history
  • Loading branch information
pared authored Nov 18, 2020
1 parent d7564e9 commit 7501302
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion dvc/command/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def run(self):
if table:
logger.info(table)
except DvcException:
logger.exception("failed to show metrics")
logger.exception("")
return 1

return 0
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def run(self):
logger.info(f"file://{path}")

except DvcException:
logger.exception("failed to show plots")
logger.exception("")
return 1

return 0
Expand Down
63 changes: 34 additions & 29 deletions tests/func/metrics/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,62 @@

import pytest

from dvc.main import main
from dvc.exceptions import NoMetricsFoundError, NoMetricsParsedError
from tests.func.metrics.utils import _write_json


@pytest.mark.parametrize(
"command, metric_value",
(("metrics", {"m": 1}), ("plots", [{"m": 1}, {"m": 2}])),
"diff, metric_value",
(
(
lambda repo, target, rev: repo.metrics.diff(
targets=[target], a_rev=rev
),
{"m": 1},
),
(
lambda repo, target, rev: repo.plots.diff(
targets=[target], revs=[rev]
),
[{"m": 1}, {"m": 2}],
),
),
)
def test_diff_no_file_on_target_rev(
tmp_dir, scm, dvc, caplog, command, metric_value
tmp_dir, scm, dvc, caplog, diff, metric_value
):
with tmp_dir.branch("new_branch", new=True):
_write_json(tmp_dir, metric_value, "metric.json")

with caplog.at_level(logging.WARNING, "dvc"):
assert (
main([command, "diff", "master", "--targets", "metric.json"])
== 0
)
diff(dvc, "metric.json", "master")

assert "'metric.json' was not found at: 'master'." in caplog.text


@pytest.mark.parametrize(
"command, malformed_metric",
(("metrics", '{"m": 1'), ("plots", '[{"m": 1}, {"m": 2}'),),
"show, malformed_metric",
(
(lambda repo, target: repo.metrics.show(targets=[target]), '{"m": 1'),
(
lambda repo, target: repo.plots.show(targets=[target]),
'[{"m": 1}, {"m": 2}',
),
),
)
def test_show_malformed_metric(
tmp_dir, scm, dvc, caplog, command, malformed_metric
tmp_dir, scm, dvc, caplog, show, malformed_metric
):
tmp_dir.gen("metric.json", malformed_metric)

with caplog.at_level(logging.ERROR, "dvc"):
assert main([command, "show", "metric.json"]) == 1

assert (
f"Could not parse {command} files. "
"Use `-v` option to see more details."
) in caplog.text
with pytest.raises(NoMetricsParsedError):
show(dvc, "metric.json")


@pytest.mark.parametrize(
"command, run_options",
(("metrics", "-m/-M"), ("plots", "--plots/--plots-no-cache"),),
"show",
(lambda repo: repo.metrics.show(), lambda repo: repo.plots.show(),),
)
def test_show_no_metrics_files(tmp_dir, dvc, caplog, command, run_options):
with caplog.at_level(logging.ERROR, "dvc"):
assert main([command, "show"]) == 1

assert (
f"No {command} files in this repository. "
f"Use `{run_options}` options for "
f"`dvc run` to mark stage outputs as {command}."
) in caplog.text
def test_show_no_metrics_files(tmp_dir, dvc, caplog, show):
with pytest.raises(NoMetricsFoundError):
show(dvc)

0 comments on commit 7501302

Please sign in to comment.