From 7501302baa6f724a2e962446249044a563b44568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Redzy=C5=84ski?= Date: Wed, 18 Nov 2020 13:14:19 +0100 Subject: [PATCH] metrics/plots: common tests: check for particular exceptions (#4865) --- dvc/command/metrics.py | 2 +- dvc/command/plots.py | 2 +- tests/func/metrics/test_common.py | 63 +++++++++++++++++-------------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index de11479691..81dc25f18c 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -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 diff --git a/dvc/command/plots.py b/dvc/command/plots.py index 1c3be83003..27ffd16f24 100644 --- a/dvc/command/plots.py +++ b/dvc/command/plots.py @@ -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 diff --git a/tests/func/metrics/test_common.py b/tests/func/metrics/test_common.py index d0ebc37156..2ba3b6e54b 100644 --- a/tests/func/metrics/test_common.py +++ b/tests/func/metrics/test_common.py @@ -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)