forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_show.py
251 lines (202 loc) · 7.08 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
import operator
from functools import reduce
import pytest
from dvc.repo import Repo
from dvc.repo.stage import PIPELINE_FILE
from dvc.utils.serialize import YAMLFileCorruptedError
def test_show_empty(dvc):
assert dvc.params.show() == {}
def test_show(tmp_dir, dvc):
tmp_dir.gen("params.yaml", "foo: bar")
dvc.run(cmd="echo params.yaml", params=["foo"], single_stage=True)
assert dvc.params.show() == {
"": {"data": {"params.yaml": {"data": {"foo": "bar"}}}}
}
def test_show_targets(tmp_dir, dvc):
tmp_dir.gen("params.yaml", "foo: bar")
dvc.run(cmd="echo params.yaml", params=["foo"], single_stage=True)
expected = {"": {"data": {"params.yaml": {"data": {"foo": "bar"}}}}}
assert dvc.params.show(targets=["params.yaml"]) == expected
assert (
dvc.params.show(targets=(tmp_dir / "params.yaml").fs_path) == expected
)
def test_show_toml(tmp_dir, dvc):
tmp_dir.gen("params.toml", "[foo]\nbar = 42\nbaz = [1, 2]\n")
dvc.run(
cmd="echo params.toml", params=["params.toml:foo"], single_stage=True
)
assert dvc.params.show() == {
"": {
"data": {
"params.toml": {"data": {"foo": {"bar": 42, "baz": [1, 2]}}}
}
}
}
def test_show_py(tmp_dir, dvc):
tmp_dir.gen(
"params.py",
"CONST = 1\nIS_DIR: bool = True\n\n\nclass Config:\n foo = 42\n",
)
dvc.run(
cmd="echo params.py",
params=["params.py:CONST,IS_DIR,Config.foo"],
single_stage=True,
)
assert dvc.params.show() == {
"": {
"data": {
"params.py": {
"data": {"CONST": 1, "Config": {"foo": 42}, "IS_DIR": True}
}
}
}
}
def test_show_multiple(tmp_dir, dvc):
tmp_dir.gen("params.yaml", "foo: bar\nbaz: qux\n")
dvc.run(
cmd="echo params.yaml",
fname="foo.dvc",
params=["foo"],
single_stage=True,
)
dvc.run(
cmd="echo params.yaml",
fname="baz.dvc",
params=["baz"],
single_stage=True,
)
assert dvc.params.show() == {
"": {"data": {"params.yaml": {"data": {"baz": "qux", "foo": "bar"}}}}
}
def test_show_list(tmp_dir, dvc):
tmp_dir.gen("params.yaml", "foo:\n- bar\n- baz\n")
dvc.run(cmd="echo params.yaml", params=["foo"], single_stage=True)
assert dvc.params.show() == {
"": {"data": {"params.yaml": {"data": {"foo": ["bar", "baz"]}}}}
}
def test_show_branch(tmp_dir, scm, dvc):
tmp_dir.gen("params.yaml", "foo: bar")
dvc.run(cmd="echo params.yaml", params=["foo"], single_stage=True)
scm.add(["params.yaml", "Dvcfile"])
scm.commit("init")
with tmp_dir.branch("branch", new=True):
tmp_dir.scm_gen("params.yaml", "foo: baz", commit="branch")
assert dvc.params.show(revs=["branch"]) == {
"branch": {"data": {"params.yaml": {"data": {"foo": "baz"}}}},
"workspace": {"data": {"params.yaml": {"data": {"foo": "bar"}}}},
}
def test_pipeline_params(tmp_dir, scm, dvc, run_copy):
tmp_dir.gen(
{"foo": "foo", "params.yaml": "foo: bar\nxyz: val\nabc: ignore"}
)
run_copy("foo", "bar", name="copy-foo-bar", params=["foo,xyz"])
scm.add(["params.yaml", PIPELINE_FILE])
scm.commit("add stage")
tmp_dir.scm_gen(
"params.yaml", "foo: baz\nxyz: val\nabc: ignore", commit="baz"
)
tmp_dir.scm_gen(
"params.yaml", "foo: qux\nxyz: val\nabc: ignore", commit="qux"
)
assert dvc.params.show(revs=["master"], deps=True) == {
"master": {
"data": {"params.yaml": {"data": {"foo": "qux", "xyz": "val"}}}
}
}
assert dvc.params.show(revs=["master"]) == {
"master": {
"data": {
"params.yaml": {
"data": {"abc": "ignore", "foo": "qux", "xyz": "val"}
}
}
}
}
def test_show_no_repo(tmp_dir):
tmp_dir.gen({"foo": "foo", "params_file.yaml": "foo: bar\nxyz: val"})
dvc = Repo(uninitialized=True)
assert dvc.params.show(targets=["params_file.yaml"]) == {
"": {
"data": {
"params_file.yaml": {"data": {"foo": "bar", "xyz": "val"}}
}
}
}
@pytest.mark.parametrize(
"file,error_path",
(
(PIPELINE_FILE, ["v1", "error"]),
("params_other.yaml", ["v1", "data", "params_other.yaml", "error"]),
),
)
def test_log_errors(tmp_dir, scm, dvc, capsys, file, error_path):
tmp_dir.gen("params_other.yaml", "foo: bar")
dvc.run(
cmd="echo params_other.yaml",
params=["params_other.yaml:foo"],
name="train",
)
rename = (tmp_dir / file).read_text()
with open(tmp_dir / file, "a", encoding="utf-8") as fd:
fd.write("\nmalformed!")
scm.add([PIPELINE_FILE, "params_other.yaml"])
scm.commit("init")
scm.tag("v1")
(tmp_dir / file).write_text(rename)
result = dvc.params.show(revs=["v1"])
_, error = capsys.readouterr()
assert isinstance(
reduce(operator.getitem, error_path, result), YAMLFileCorruptedError
)
assert (
"DVC failed to load some parameters for following revisions: 'v1'."
in error
)
@pytest.mark.parametrize("file", ["params.yaml", "other_params.yaml"])
def test_show_without_targets_specified(tmp_dir, dvc, scm, file):
params_file = tmp_dir / file
data = {"foo": {"bar": "bar"}, "x": "0"}
params_file.dump(data)
dvc.stage.add(
name="test",
cmd=f"echo {file}",
params=[{file: None}],
)
assert dvc.params.show() == {"": {"data": {file: {"data": data}}}}
def test_deps_multi_stage(tmp_dir, scm, dvc, run_copy):
tmp_dir.gen(
{"foo": "foo", "params.yaml": "foo: bar\nxyz: val\nabc: ignore"}
)
run_copy("foo", "bar", name="copy-foo-bar", params=["foo"])
run_copy("foo", "bar1", name="copy-foo-bar-1", params=["xyz"])
scm.add(["params.yaml", PIPELINE_FILE])
scm.commit("add stage")
assert dvc.params.show(revs=["master"], deps=True) == {
"master": {
"data": {"params.yaml": {"data": {"foo": "bar", "xyz": "val"}}}
}
}
def test_deps_with_targets(tmp_dir, scm, dvc, run_copy):
tmp_dir.gen(
{"foo": "foo", "params.yaml": "foo: bar\nxyz: val\nabc: ignore"}
)
run_copy("foo", "bar", name="copy-foo-bar", params=["foo"])
run_copy("foo", "bar1", name="copy-foo-bar-1", params=["xyz"])
scm.add(["params.yaml", PIPELINE_FILE])
scm.commit("add stage")
assert dvc.params.show(targets=["params.yaml"], deps=True) == {
"": {"data": {"params.yaml": {"data": {"foo": "bar", "xyz": "val"}}}}
}
def test_deps_with_bad_target(tmp_dir, scm, dvc, run_copy):
tmp_dir.gen(
{
"foo": "foo",
"foobar": "",
"params.yaml": "foo: bar\nxyz: val\nabc: ignore",
}
)
run_copy("foo", "bar", name="copy-foo-bar", params=["foo"])
run_copy("foo", "bar1", name="copy-foo-bar-1", params=["xyz"])
scm.add(["params.yaml", PIPELINE_FILE])
scm.commit("add stage")
assert dvc.params.show(targets=["foobar"], deps=True) == {}