Skip to content

Commit 75fc406

Browse files
committed
Add unit test for encode_as_list method.
1 parent 32aeb19 commit 75fc406

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

plotly/tests/test_optional/test_utils/test_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ def to_plotly_json(self):
4242
res = utils.PlotlyJSONEncoder.encode_as_plotly(ObjWithAttr())
4343
self.assertEqual(res, expected_res)
4444

45+
def test_encode_as_list(self):
46+
47+
# should *fail* when object doesn't have `tolist` method
48+
objs_without_attr = [
49+
1, 'one', {'a', 'set'}, {'a': 'dict'}, ['a', 'list']
50+
]
51+
for obj in objs_without_attr:
52+
self.assertRaises(utils.NotEncodable,
53+
utils.PlotlyJSONEncoder.encode_as_list, obj)
54+
55+
# should return without exception when obj has `tolist` attr
56+
expected_res = ['some', 'list']
57+
58+
class ObjWithAttr(object):
59+
60+
def tolist(self):
61+
return expected_res
62+
63+
res = utils.PlotlyJSONEncoder.encode_as_list(ObjWithAttr())
64+
self.assertEqual(res, expected_res)
65+
4566
## JSON encoding
4667
numeric_list = [1, 2, 3]
4768
np_list = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)])

0 commit comments

Comments
 (0)