-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_to_from_json.py
275 lines (207 loc) · 7.15 KB
/
test_to_from_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import plotly.graph_objs as go
import plotly.io as pio
import pytest
import plotly
import numpy as np
import json
import os
import tempfile
from unittest.mock import MagicMock
from pathlib import Path
# fixtures
# --------
@pytest.fixture
def fig1(request):
return go.Figure(
data=[
{"type": "scattergl", "marker": {"color": "green"}},
{
"type": "parcoords",
"dimensions": [{"values": [1, 2, 3]}, {"values": [3, 2, 1]}],
"line": {"color": "blue"},
},
],
layout={"title": "Figure title"},
)
opts = {
"separators": (",", ":"),
"cls": plotly.utils.PlotlyJSONEncoder,
}
pretty_opts = {"indent": 2, "cls": plotly.utils.PlotlyJSONEncoder}
# to_json
# -------
def test_to_json(fig1):
assert pio.to_json(fig1, remove_uids=False) == json.dumps(fig1, **opts)
def test_to_json_remove_uids(fig1):
dict1 = fig1.to_dict()
for trace in dict1["data"]:
trace.pop("uid", None)
assert pio.to_json(fig1) == json.dumps(dict1, **opts)
def test_to_json_validate(fig1):
dict1 = fig1.to_dict()
dict1["layout"]["bogus"] = 37
with pytest.raises(ValueError):
pio.to_json(dict1)
def test_to_json_validate_false(fig1):
dict1 = fig1.to_dict()
dict1["layout"]["bogus"] = 37
assert pio.to_json(dict1, validate=False) == json.dumps(dict1, **opts)
def test_to_json_pretty_print(fig1):
assert pio.to_json(fig1, remove_uids=False, pretty=True) == json.dumps(
fig1, **pretty_opts
)
# from_json
# ---------
def test_from_json(fig1):
fig1_json = json.dumps(fig1, **opts)
fig1_loaded = pio.from_json(fig1_json)
# Check return type
assert isinstance(fig1_loaded, go.Figure)
# Check return json
assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
@pytest.mark.parametrize(
"fig_type_spec,fig_type",
[
("Figure", go.Figure),
(go.Figure, go.Figure),
("FigureWidget", go.FigureWidget),
(go.FigureWidget, go.FigureWidget),
],
)
def test_from_json_output_type(fig1, fig_type_spec, fig_type):
fig1_json = json.dumps(fig1, **opts)
fig1_loaded = pio.from_json(fig1_json, output_type=fig_type_spec)
# Check return type
assert isinstance(fig1_loaded, fig_type)
# Check return json
assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
def test_from_json_invalid(fig1):
dict1 = fig1.to_dict()
# Set bad property name
dict1["data"][0]["marker"]["bogus"] = 123
# Set property with bad value
dict1["data"][0]["marker"]["size"] = -1
# Serialize to json
bad_json = json.dumps(dict1, **opts)
with pytest.raises(ValueError):
pio.from_json(bad_json)
def test_from_json_skip_invalid(fig1):
dict1 = fig1.to_dict()
# Set bad property name
dict1["data"][0]["marker"]["bogus"] = 123
# Set property with bad value
dict1["data"][0]["marker"]["size"] = -1
# Serialize to json
bad_json = json.dumps(dict1, **opts)
fig1_loaded = pio.from_json(bad_json, skip_invalid=True)
# Check loaded figure
assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
# read_json
# ---------
@pytest.mark.parametrize(
"fig_type_spec,fig_type",
[
("Figure", go.Figure),
(go.Figure, go.Figure),
("FigureWidget", go.FigureWidget),
(go.FigureWidget, go.FigureWidget),
],
)
def test_read_json_from_filelike(fig1, fig_type_spec, fig_type):
# Configure file-like mock
filemock = MagicMock()
del filemock.read_text
filemock.read.return_value = pio.to_json(fig1)
# read_json on mock file
fig1_loaded = pio.read_json(filemock, output_type=fig_type_spec)
# Check return type
assert isinstance(fig1_loaded, fig_type)
# Check loaded figure
assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
@pytest.mark.parametrize(
"fig_type_spec,fig_type",
[
("Figure", go.Figure),
(go.Figure, go.Figure),
("FigureWidget", go.FigureWidget),
(go.FigureWidget, go.FigureWidget),
],
)
def test_read_json_from_pathlib(fig1, fig_type_spec, fig_type):
# Configure pathlib.Path-like mock
filemock = MagicMock(spec=Path)
filemock.read_text.return_value = pio.to_json(fig1)
# read_json on mock file
fig1_loaded = pio.read_json(filemock, output_type=fig_type_spec)
# Check return type
assert isinstance(fig1_loaded, fig_type)
# Check loaded figure
assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
@pytest.mark.parametrize(
"fig_type_spec,fig_type",
[
("Figure", go.Figure),
(go.Figure, go.Figure),
("FigureWidget", go.FigureWidget),
(go.FigureWidget, go.FigureWidget),
],
)
def test_read_json_from_file_string(fig1, fig_type_spec, fig_type):
with tempfile.TemporaryDirectory() as dir_name:
# Write json file
path = os.path.join(dir_name, "fig1.json")
with open(path, "w") as f:
f.write(pio.to_json(fig1))
# read json from file as string
fig1_loaded = pio.read_json(path, output_type=fig_type_spec)
# Check return type
assert isinstance(fig1_loaded, fig_type)
# Check loaded figure
assert pio.to_json(fig1_loaded) == pio.to_json(fig1.to_dict())
# write_json
# ----------
@pytest.mark.parametrize("pretty", [True, False])
@pytest.mark.parametrize("remove_uids", [True, False])
def test_write_json_filelike(fig1, pretty, remove_uids):
# Configure file-like mock
filemock = MagicMock()
del filemock.write_text
# write_json to mock file
pio.write_json(fig1, filemock, pretty=pretty, remove_uids=remove_uids)
# check write contents
expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
filemock.write.assert_called_once_with(expected)
@pytest.mark.parametrize("pretty", [True, False])
@pytest.mark.parametrize("remove_uids", [True, False])
def test_write_json_pathlib(fig1, pretty, remove_uids):
# Configure file-like mock
filemock = MagicMock(spec=Path)
# write_json to mock file
pio.write_json(fig1, filemock, pretty=pretty, remove_uids=remove_uids)
# check write contents
expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
filemock.write_text.assert_called_once_with(expected)
@pytest.mark.parametrize("pretty", [True, False])
@pytest.mark.parametrize("remove_uids", [True, False])
def test_write_json_from_file_string(fig1, pretty, remove_uids):
with tempfile.TemporaryDirectory() as dir_name:
# Write json
path = os.path.join(dir_name, "fig1.json")
pio.write_json(fig1, path, pretty=pretty, remove_uids=remove_uids)
# Open as text file
with open(path, "r") as f:
result = f.read()
# Check contents that were written
expected = pio.to_json(fig1, pretty=pretty, remove_uids=remove_uids)
assert result == expected
def test_to_dict_empty_np_array_int64():
fig = go.Figure(
[
go.Bar(
x=np.array([], dtype="str"),
y=np.array([], dtype="int64"),
)
]
)
# to_dict() should not raise an exception
fig.to_dict()