metrics/plots: common tests: check for particular exceptions (#4865)

This commit is contained in:
Paweł Redzyński
2020-11-18 14:14:19 +02:00
committed by GitHub
parent d7564e9965
commit 7501302baa
3 changed files with 36 additions and 31 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ class CmdMetricsShow(CmdMetricsBase):
if table:
logger.info(table)
except DvcException:
logger.exception("failed to show metrics")
logger.exception("")
return 1
return 0
+1 -1
View File
@@ -72,7 +72,7 @@ class CmdPlots(CmdBase):
logger.info(f"file://{path}")
except DvcException:
logger.exception("failed to show plots")
logger.exception("")
return 1
return 0
+34 -29
View File
@@ -2,57 +2,62 @@ import logging
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)