Skip to content

Commit 32aeb19

Browse files
committed
Add unit test for encode_as_plotly method.
1 parent d6bc06c commit 32aeb19

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

plotly/tests/test_optional/test_utils/test_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from nose import with_setup
33
from nose.plugins.attrib import attr
44

5+
from unittest import TestCase
6+
57
from datetime import datetime as dt
68
import datetime
79
import numpy as np
@@ -16,6 +18,30 @@
1618
from plotly.matplotlylib import Exporter, PlotlyRenderer
1719
from plotly.plotly import plot
1820

21+
22+
class TestJSONEncoder(TestCase):
23+
24+
def test_encode_as_plotly(self):
25+
26+
# should *fail* when object doesn't have `to_plotly_json` attribute
27+
objs_without_attr = [
28+
1, 'one', {'a', 'set'}, {'a': 'dict'}, ['a', 'list']
29+
]
30+
for obj in objs_without_attr:
31+
self.assertRaises(utils.NotEncodable,
32+
utils.PlotlyJSONEncoder.encode_as_plotly, obj)
33+
34+
# should return without exception when obj has `to_plotly_josn` attr
35+
expected_res = 'wedidit'
36+
37+
class ObjWithAttr(object):
38+
39+
def to_plotly_json(self):
40+
return expected_res
41+
42+
res = utils.PlotlyJSONEncoder.encode_as_plotly(ObjWithAttr())
43+
self.assertEqual(res, expected_res)
44+
1945
## JSON encoding
2046
numeric_list = [1, 2, 3]
2147
np_list = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)])

0 commit comments

Comments
 (0)