forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_show.py
304 lines (240 loc) · 8.62 KB
/
test_show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import logging
import os
import pytest
from funcy import get_in
from dvc.dvcfile import PIPELINE_FILE
from dvc.exceptions import OverlappingOutputPathsError
from dvc.repo import Repo
from dvc.utils.fs import remove
from dvc.utils.serialize import YAMLFileCorruptedError
def test_show_simple(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("metrics_t.yaml", "1.1")
run_copy_metrics(
"metrics_t.yaml", "metrics.yaml", metrics=["metrics.yaml"]
)
assert dvc.metrics.show() == {
"": {"data": {"metrics.yaml": {"data": 1.1}}}
}
def test_show(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("metrics_t.yaml", "foo: 1.1")
run_copy_metrics(
"metrics_t.yaml", "metrics.yaml", metrics=["metrics.yaml"]
)
assert dvc.metrics.show() == {
"": {"data": {"metrics.yaml": {"data": {"foo": 1.1}}}}
}
def test_show_targets(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("metrics_t.yaml", "foo: 1.1")
run_copy_metrics(
"metrics_t.yaml", "metrics.yaml", metrics=["metrics.yaml"]
)
expected = {"": {"data": {"metrics.yaml": {"data": {"foo": 1.1}}}}}
assert dvc.metrics.show(targets=["metrics.yaml"]) == expected
assert (
dvc.metrics.show(targets=(tmp_dir / "metrics.yaml").fs_path)
== expected
)
def test_show_multiple(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("foo_temp", "foo: 1\n")
tmp_dir.gen("baz_temp", "baz: 2\n")
run_copy_metrics("foo_temp", "foo", fname="foo.dvc", metrics=["foo"])
run_copy_metrics("baz_temp", "baz", fname="baz.dvc", metrics=["baz"])
assert dvc.metrics.show() == {
"": {
"data": {"foo": {"data": {"foo": 1}}, "baz": {"data": {"baz": 2}}}
}
}
def test_show_branch(tmp_dir, scm, dvc, run_copy_metrics):
tmp_dir.gen("metrics_temp.yaml", "foo: 1")
run_copy_metrics(
"metrics_temp.yaml", "metrics.yaml", metrics_no_cache=["metrics.yaml"]
)
scm.add(["metrics.yaml", "metrics.yaml.dvc"])
scm.commit("init")
with tmp_dir.branch("branch", new=True):
tmp_dir.scm_gen("metrics.yaml", "foo: 2", commit="branch")
assert dvc.metrics.show(revs=["branch"]) == {
"workspace": {"data": {"metrics.yaml": {"data": {"foo": 1}}}},
"branch": {"data": {"metrics.yaml": {"data": {"foo": 2}}}},
}
def test_show_subrepo_with_preexisting_tags(tmp_dir, scm):
tmp_dir.gen("foo", "foo")
scm.add("foo")
scm.commit("init")
scm.tag("no-metrics")
tmp_dir.gen({"subdir": {}})
subrepo_dir = tmp_dir / "subdir"
with subrepo_dir.chdir():
dvc = Repo.init(subdir=True)
scm.commit("init dvc")
dvc.run(
cmd="echo foo: 1 > metrics.yaml",
metrics=["metrics.yaml"],
single_stage=True,
)
scm.add(
[
str(subrepo_dir / "metrics.yaml"),
str(subrepo_dir / "metrics.yaml.dvc"),
]
)
scm.commit("init metrics")
scm.tag("v1")
expected_path = os.path.join("subdir", "metrics.yaml")
assert dvc.metrics.show(all_tags=True) == {
"workspace": {"data": {expected_path: {"data": {"foo": 1}}}},
"v1": {"data": {expected_path: {"data": {"foo": 1}}}},
}
def test_missing_cache(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("metrics_t.yaml", "1.1")
run_copy_metrics(
"metrics_t.yaml", "metrics.yaml", metrics=["metrics.yaml"]
)
# This one should be skipped
stage = run_copy_metrics(
"metrics_t.yaml", "metrics2.yaml", metrics=["metrics2.yaml"]
)
remove(stage.outs[0].fspath)
remove(stage.outs[0].cache_path)
result = dvc.metrics.show()
metrics2 = result[""]["data"].pop("metrics2.yaml")
assert isinstance(metrics2["error"], FileNotFoundError)
assert result == {
"": {
"data": {
"metrics.yaml": {"data": 1.1},
}
}
}
@pytest.mark.parametrize("use_dvc", [True, False])
def test_show_non_metric(tmp_dir, scm, use_dvc):
tmp_dir.gen("metrics.yaml", "foo: 1.1")
if use_dvc:
dvc = Repo.init()
else:
dvc = Repo(uninitialized=True)
assert dvc.metrics.show(targets=["metrics.yaml"]) == {
"": {"data": {"metrics.yaml": {"data": {"foo": 1.1}}}}
}
if not use_dvc:
assert not (tmp_dir / ".dvc").exists()
@pytest.mark.parametrize("use_dvc", [True, False])
def test_show_non_metric_branch(tmp_dir, scm, use_dvc):
tmp_dir.scm_gen("metrics.yaml", "foo: 1.1", commit="init")
with tmp_dir.branch("branch", new=True):
tmp_dir.scm_gen("metrics.yaml", "foo: 2.2", commit="other")
if use_dvc:
dvc = Repo.init()
else:
dvc = Repo(uninitialized=True)
assert dvc.metrics.show(targets=["metrics.yaml"], revs=["branch"]) == {
"workspace": {"data": {"metrics.yaml": {"data": {"foo": 1.1}}}},
"branch": {"data": {"metrics.yaml": {"data": {"foo": 2.2}}}},
}
if not use_dvc:
assert not (tmp_dir / ".dvc").exists()
def test_non_metric_and_recurisve_show(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen(
{"metrics_t.yaml": "foo: 1.1", "metrics": {"metric1.yaml": "bar: 1.2"}}
)
metric2 = os.fspath(tmp_dir / "metrics" / "metric2.yaml")
run_copy_metrics("metrics_t.yaml", metric2, metrics=[metric2])
assert dvc.metrics.show(
targets=["metrics_t.yaml", "metrics"], recursive=True
) == {
"": {
"data": {
os.path.join("metrics", "metric1.yaml"): {
"data": {"bar": 1.2}
},
os.path.join("metrics", "metric2.yaml"): {
"data": {"foo": 1.1}
},
"metrics_t.yaml": {"data": {"foo": 1.1}},
}
}
}
def test_show_falsey(tmp_dir, dvc):
tmp_dir.gen("metrics.json", '{"foo": 0, "bar": 0.0, "baz": {}}')
assert dvc.metrics.show(targets=["metrics.json"]) == {
"": {"data": {"metrics.json": {"data": {"foo": 0, "bar": 0.0}}}}
}
def test_show_no_repo(tmp_dir):
tmp_dir.gen("metrics.json", '{"foo": 0, "bar": 0.0, "baz": {}}')
dvc = Repo(uninitialized=True)
assert dvc.metrics.show(targets=["metrics.json"]) == {
"": {"data": {"metrics.json": {"data": {"foo": 0, "bar": 0.0}}}}
}
def test_show_malformed_metric(tmp_dir, scm, dvc, caplog):
tmp_dir.gen("metric.json", '{"m":1')
assert isinstance(
dvc.metrics.show(targets=["metric.json"])[""]["data"]["metric.json"][
"error"
],
YAMLFileCorruptedError,
)
def test_metrics_show_no_target(tmp_dir, dvc, caplog):
with caplog.at_level(logging.WARNING):
assert dvc.metrics.show(targets=["metrics.json"]) == {"": {}}
assert (
"'metrics.json' was not found in current workspace." in caplog.messages
)
def test_show_no_metrics_files(tmp_dir, dvc, caplog):
assert dvc.metrics.show() == {"": {}}
@pytest.mark.parametrize("clear_before_run", [True, False])
def test_metrics_show_overlap(
tmp_dir, dvc, run_copy_metrics, clear_before_run
):
data_dir = tmp_dir / "data"
data_dir.mkdir()
(data_dir / "m1_temp.yaml").dump({"a": {"b": {"c": 2, "d": 1}}})
run_copy_metrics(
str(data_dir / "m1_temp.yaml"),
str(data_dir / "m1.yaml"),
single_stage=False,
commit="add m1",
name="cp-m1",
metrics=[str(data_dir / "m1.yaml")],
)
with (tmp_dir / "dvc.yaml").modify() as d:
# trying to make an output overlaps error
d["stages"]["corrupted-stage"] = {
"cmd": "mkdir data",
"outs": ["data"],
}
# running by clearing and not clearing stuffs
# so as it works even for optimized cases
if clear_before_run:
remove(data_dir)
remove(dvc.odb.local.cache_dir)
dvc._reset()
res = dvc.metrics.show()
assert isinstance(res[""]["error"], OverlappingOutputPathsError)
@pytest.mark.parametrize(
"file,error_path",
(
(PIPELINE_FILE, ["workspace", "error"]),
("metrics.yaml", ["workspace", "data", "metrics.yaml", "error"]),
),
)
def test_log_errors(
tmp_dir, scm, dvc, capsys, run_copy_metrics, file, error_path
):
tmp_dir.gen("metrics_t.yaml", "m: 1.1")
run_copy_metrics(
"metrics_t.yaml",
"metrics.yaml",
metrics=["metrics.yaml"],
single_stage=False,
name="train",
)
scm.tag("v1")
with open(file, "a", encoding="utf-8") as fd:
fd.write("\nMALFORMED!")
result = dvc.metrics.show(revs=["v1"])
_, error = capsys.readouterr()
assert isinstance(get_in(result, error_path), YAMLFileCorruptedError)
assert (
"DVC failed to load some metrics for following revisions: 'workspace'."
in error
)