Skip to content

Commit cc51f2a

Browse files
committed
json encoding tests
1 parent ec16b45 commit cc51f2a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from nose.tools import raises
2+
from nose import with_setup
3+
from nose.plugins.attrib import attr
4+
5+
from datetime import datetime as dt
6+
import numpy as np
7+
import json
8+
9+
from plotly import utils
10+
from plotly.grid_objs import Column
11+
from plotly.graph_objs import Scatter3d, Figure, Data
12+
13+
## JSON encoding
14+
numeric_list = [1, 2, 3]
15+
np_list = np.array([1, 2, 3])
16+
mixed_list = [1, 'A', dt(2014, 1, 5)]
17+
18+
19+
def test_column_json_encoding():
20+
columns = [
21+
Column(numeric_list, 'col 1'),
22+
Column(mixed_list, 'col 2'),
23+
Column(np_list, 'col 3')
24+
]
25+
json_columns = json.dumps(columns, cls=utils._plotlyJSONEncoder)
26+
assert('[{"data": [1, 2, 3], "name": "col 1"}, '
27+
'{"data": [1, "A", "2014-01-05"], "name": "col 2"}, '
28+
'{"data": [1, 2, 3], "name": "col 3"}]' == json_columns)
29+
30+
31+
def test_figure_json_encoding():
32+
s = Scatter3d(x=numeric_list, y=np_list, z=mixed_list)
33+
data = Data([s])
34+
figure = Figure(data=data)
35+
36+
json.dumps(s, cls=utils._plotlyJSONEncoder)
37+
json.dumps(data, cls=utils._plotlyJSONEncoder)
38+
json.dumps(figure, cls=utils._plotlyJSONEncoder)

0 commit comments

Comments
 (0)