Skip to content

Commit

Permalink
metrics: diff: fix bug with unchanged raw metrics (iterative#3535)
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop authored Mar 26, 2020
1 parent 42e3b8a commit 912a98e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion dvc/repo/metrics/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ def _diff(old_raw, new_raw):
if isinstance(new, dict) or isinstance(old, dict):
return _diff_dicts(old, new)

return {"": _diff_vals(old, new)}
val_diff = _diff_vals(old, new)
if val_diff:
return {"": val_diff}

return {}


def _get_metrics(repo, *args, rev=None, **kwargs):
Expand Down
33 changes: 33 additions & 0 deletions tests/func/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,20 @@ def _gen(val):
}


def test_metrics_diff_raw_unchanged(tmp_dir, scm, dvc):
def _gen(val):
tmp_dir.gen({"metrics": val})
dvc.run(cmd="", metrics=["metrics"])
dvc.scm.add(["metrics.dvc"])
dvc.scm.commit(str(val))

_gen("raw 1")
_gen("raw 2")
_gen("raw 1")

assert dvc.metrics.diff(a_rev="HEAD~2") == {}


@pytest.mark.parametrize("xpath", [True, False])
def test_metrics_diff_json(tmp_dir, scm, dvc, xpath):
def _gen(val):
Expand All @@ -919,6 +933,25 @@ def _gen(val):
assert expected == dvc.metrics.diff(a_rev="HEAD~2")


@pytest.mark.parametrize("xpath", [True, False])
def test_metrics_diff_json_unchanged(tmp_dir, scm, dvc, xpath):
def _gen(val):
metrics = {"a": {"b": {"c": val, "d": 1, "e": str(val)}}}
tmp_dir.gen({"m.json": json.dumps(metrics)})
dvc.run(cmd="", metrics=["m.json"])
dvc.metrics.modify("m.json", typ="json")
if xpath:
dvc.metrics.modify("m.json", xpath="a.b.c")
dvc.scm.add(["m.json.dvc"])
dvc.scm.commit(str(val))

_gen(1)
_gen(2)
_gen(1)

assert dvc.metrics.diff(a_rev="HEAD~2") == {}


def test_metrics_diff_broken_json(tmp_dir, scm, dvc):
metrics = {"a": {"b": {"c": 1, "d": 1, "e": "3"}}}
tmp_dir.gen({"m.json": json.dumps(metrics)})
Expand Down

0 comments on commit 912a98e

Please sign in to comment.