Skip to content

metrics/plots: test metrics collection on no target on revision #4789

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

Merged
merged 4 commits into from
Oct 30, 2020
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
2 changes: 1 addition & 1 deletion dvc/repo/experiments/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _collect_experiment(repo, rev, stash=False, sha_only=True):

res["queued"] = stash
if not stash:
metrics = _collect_metrics(repo, None, False)
metrics = _collect_metrics(repo, None, rev, False)
vals = _read_metrics(repo, metrics, rev)
res["metrics"] = vals

Expand Down
10 changes: 7 additions & 3 deletions dvc/repo/metrics/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ def _is_metric(out):
return bool(out.metric)


def _collect_metrics(repo, targets, recursive):
def _collect_metrics(repo, targets, revision, recursive):
metrics, path_infos = collect(
repo, targets=targets, output_filter=_is_metric, recursive=recursive
repo,
targets=targets,
output_filter=_is_metric,
recursive=recursive,
rev=revision,
)
return [m.path_info for m in metrics] + list(path_infos)

Expand Down Expand Up @@ -88,7 +92,7 @@ def show(
all_tags=all_tags,
all_commits=all_commits,
):
metrics = _collect_metrics(repo, targets, recursive)
metrics = _collect_metrics(repo, targets, rev, recursive)

if not metrics_found and metrics:
metrics_found = True
Expand Down
File renamed without changes.
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from .test_plots import PlotData, _write_json
from dvc.repo.plots.data import PlotData
from tests.func.metrics.utils import _write_json


def test_diff_dirty(tmp_dir, scm, dvc, run_copy_metrics):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from dvc.repo.plots import PropsNotFoundError
from dvc.repo.plots.template import TemplateNotFoundError
from dvc.utils import relpath

from .test_plots import _write_json
from tests.func.metrics.utils import _write_json


def test_plots_modify_existing_template(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import csv
import json
import logging
import os
import shutil
from collections import OrderedDict

import pytest
from funcy import first

from dvc.repo import Repo
from dvc.repo.plots.data import (
Expand All @@ -23,25 +21,7 @@
)
from dvc.utils.fs import remove
from dvc.utils.serialize import dump_yaml, dumps_yaml


def _write_csv(metric, filename, header=True):
with open(filename, "w", newline="") as csvobj:
if header:
writer = csv.DictWriter(
csvobj, fieldnames=list(first(metric).keys())
)
writer.writeheader()
writer.writerows(metric)
else:
writer = csv.writer(csvobj)
for d in metric:
assert len(d) == 1
writer.writerow(list(d.values()))


def _write_json(tmp_dir, metric, filename):
tmp_dir.gen(filename, json.dumps(metric, sort_keys=True))
from tests.func.metrics.utils import _write_csv, _write_json


def test_plot_csv_one_column(tmp_dir, scm, dvc, run_copy_metrics):
Expand Down
29 changes: 29 additions & 0 deletions tests/func/metrics/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logging

import pytest

from tests.func.metrics.utils import _write_json


def metrics_diff(dvc, filename, revision):
dvc.metrics.diff(targets=[filename], a_rev=revision)


def plots_diff(dvc, filename, revision):
dvc.plots.diff(targets=[filename], revs=[revision])


@pytest.mark.parametrize(
"diff_fun, metric_value",
((metrics_diff, {"m": 1}), (plots_diff, [{"m": 1}, {"m": 2}])),
)
def test_diff_no_file_on_target_rev(
tmp_dir, scm, dvc, caplog, diff_fun, 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"):
diff_fun(dvc, "metric.json", "master")

assert "'metric.json' was not found at: 'master'." in caplog.text
23 changes: 23 additions & 0 deletions tests/func/metrics/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import csv
import json

from funcy import first


def _write_csv(metric, filename, header=True):
with open(filename, "w", newline="") as csvobj:
if header:
writer = csv.DictWriter(
csvobj, fieldnames=list(first(metric).keys())
)
writer.writeheader()
writer.writerows(metric)
else:
writer = csv.writer(csvobj)
for d in metric:
assert len(d) == 1
writer.writerow(list(d.values()))


def _write_json(tmp_dir, metric, filename):
tmp_dir.gen(filename, json.dumps(metric, sort_keys=True))