forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_json.py
254 lines (202 loc) · 6.8 KB
/
test_json.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
import json
import os
from copy import deepcopy
from typing import Dict, List
from urllib.parse import urlparse
from urllib.request import url2pathname
import dpath.util
import pytest
from funcy import first
from dvc.cli import main
JSON_OUT = "vis_data"
def call(capsys, subcommand="show"):
capsys.readouterr()
assert (
main(["plots", subcommand, "--json", "-o", JSON_OUT, "--split"]) == 0
)
split_json_out, _ = capsys.readouterr()
split_json_result = json.loads(split_json_out)
capsys.readouterr()
assert main(["plots", subcommand, "--json", "-o", JSON_OUT]) == 0
json_out, _ = capsys.readouterr()
json_result = json.loads(json_out)
assert main(["plots", subcommand]) == 0
html_path_out, _ = capsys.readouterr()
parsed = urlparse(html_path_out.strip())
abspath = url2pathname(parsed.path)
return abspath, json_result, split_json_result
def filter_fields(datapoints: List[Dict], fields: List[str]):
tmp = deepcopy(datapoints)
for datapoint in tmp:
keys = set(datapoint.keys())
for key in keys:
if key not in fields:
datapoint.pop(key)
return tmp
def verify_image(tmp_dir, version, filename, content, html_path, json_result):
assert os.path.exists(html_path)
with open(html_path, "r", encoding="utf-8") as fd:
html_content = fd.read()
image_data = {}
for datapoint in json_result[filename]:
if datapoint["revisions"] == [version]:
image_data = datapoint
break
assert image_data, f"{version} data for {filename} was not found"
assert image_data["type"] == "image"
assert image_data["url"] == str(
tmp_dir / JSON_OUT / f"{version}_{filename}"
)
assert (
tmp_dir / JSON_OUT / f"{version}_{filename}"
).read_bytes() == content
assert os.path.join("static", f"{version}_{filename}") in html_content
# there should be no absolute paths in produced HTML
# TODO uncomment once dvc-render is adjusted
# assert str(tmp_dir) not in html_content
assert (
tmp_dir / "dvc_plots" / "static" / f"{version}_{filename}"
).read_bytes() == content
def _remove_blanks(text: str):
return text.replace("\t", "").replace("\n", "").replace(" ", "")
def verify_vega(
versions,
filename,
fields,
expected_data,
html_path,
json_result,
split_json_result,
):
assert os.path.exists(html_path)
with open(html_path, "r", encoding="utf-8") as fd:
html_content = fd.read()
assert _remove_blanks(
json.dumps(dpath.util.get(json_result, [filename, 0, "content"]))
) in _remove_blanks(html_content)
if isinstance(versions, str):
versions = [versions]
for j in [json_result, split_json_result]:
assert len(j[filename]) == 1
assert dpath.util.get(j, [filename, 0, "type"]) == "vega"
assert dpath.util.get(j, [filename, 0, "revisions"]) == versions
assert dpath.util.get(
json_result, [filename, 0, "datapoints"]
) == dpath.util.get(split_json_result, [filename, 0, "datapoints"])
assert set(versions) == set(
dpath.util.get(json_result, [filename, 0, "datapoints"]).keys()
)
assert (
filter_fields(
dpath.util.get(
json_result, [filename, 0, "content", "data", "values"]
),
fields,
)
== expected_data
)
assert (
dpath.util.get(
split_json_result, [filename, 0, "content", "data", "values"]
)
== "<DVC_METRIC_DATA>"
)
# if data is not considered, json and split json should be identical
json_content = deepcopy(
dpath.util.get(json_result, [filename, 0, "content"])
)
split_json_content = deepcopy(
dpath.util.get(split_json_result, [filename, 0, "content"])
)
dpath.util.set(json_content, ["data"], {})
dpath.util.set(split_json_content, ["data"], {})
assert json_content == split_json_content
def verify_vega_props(filename, json_result, title, x, y, **kwargs):
data = json_result[filename]
assert len(data) == 1
data = first(data)
assert dpath.util.get(data, ["content", "title"]) == title
assert (
dpath.util.get(data, ["content", "spec", "encoding", "x", "field"])
== x
)
assert (
dpath.util.get(data, ["content", "spec", "encoding", "y", "field"])
== y
)
@pytest.mark.vscode
def test_no_plots(tmp_dir, scm, dvc, capsys):
html_path, json_result, split_json_result = call(capsys)
assert os.path.exists(html_path)
assert json_result == {}
assert split_json_result == {}
@pytest.mark.vscode
def test_repo_with_plots(
tmp_dir, scm, dvc, capsys, run_copy_metrics, repo_with_plots
):
repo_state = repo_with_plots()
image_v1, linear_v1, confusion_v1, confusion_props = next(repo_state)
html_path, json_result, split_json_result = call(capsys)
verify_image(
tmp_dir, "workspace", "image.png", image_v1, html_path, json_result
)
verify_vega(
"workspace",
"linear.json",
["x", "y"],
linear_v1,
html_path,
json_result,
split_json_result,
)
verify_vega(
"workspace",
"confusion.json",
["actual", "predicted"],
confusion_v1,
html_path,
json_result,
split_json_result,
)
verify_vega_props("confusion.json", json_result, **confusion_props)
image_v2, linear_v2, confusion_v2, confusion_props = next(repo_state)
html_path, json_result, split_json_result = call(capsys, subcommand="diff")
verify_image(
tmp_dir, "workspace", "image.png", image_v2, html_path, json_result
)
verify_image(
tmp_dir, "HEAD", "image.png", image_v1, html_path, json_result
)
verify_vega(
["HEAD", "workspace"],
"linear.json",
["x", "y"],
linear_v2 + linear_v1,
html_path,
json_result,
split_json_result,
)
verify_vega(
["HEAD", "workspace"],
"confusion.json",
["actual", "predicted"],
confusion_v2 + confusion_v1,
html_path,
json_result,
split_json_result,
)
verify_vega_props("confusion.json", json_result, **confusion_props)
@pytest.mark.vscode
def test_repo_with_removed_plots(tmp_dir, capsys, repo_with_plots):
from dvc.utils.fs import remove
next(repo_with_plots())
# even if there is no data, call should be successful
remove(tmp_dir / ".dvc" / "cache")
remove("linear.json")
remove("confusion.json")
remove("image.png")
for s in {"show", "diff"}:
_, json_result, split_json_result = call(capsys, subcommand=s)
for p in {"linear.json", "confusion.json", "image.png"}:
assert json_result[p] == []
assert split_json_result[p] == []