-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_utils.py
50 lines (37 loc) · 1.58 KB
/
test_utils.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
from unittest import TestCase
from plotly.utils import PlotlyJSONEncoder, get_by_path, node_generator
import json as _json
class TestJSONEncoder(TestCase):
def test_nan_to_null(self):
array = [1, float("NaN"), float("Inf"), float("-Inf"), "platypus"]
result = _json.dumps(array, cls=PlotlyJSONEncoder)
expected_result = '[1, null, null, null, "platypus"]'
self.assertEqual(result, expected_result)
def test_invalid_encode_exception(self):
with self.assertRaises(TypeError):
_json.dumps({"a": {1}}, cls=PlotlyJSONEncoder)
class TestGetByPath(TestCase):
def test_get_by_path(self):
# should be able to traverse into a nested dict/list with key array
figure = {"data": [{}, {"marker": {"color": ["red", "blue"]}}]}
path = ("data", 1, "marker", "color")
value = get_by_path(figure, path)
expected_value = ["red", "blue"]
self.assertEqual(value, expected_value)
class TestNodeGenerator(TestCase):
def test_node_generator(self):
# should generate a (node, path) pair for each dict in a dict
node4 = {"h": 5}
node3 = {"g": 7}
node2 = {"e": node3}
node1 = {"c": node2, "d": ["blah"]}
node0 = {"a": node1, "b": 8}
expected_node_path_tuples = [
(node0, ()),
(node1, ("a",)),
(node2, ("a", "c")),
(node3, ("a", "c", "e")),
(node4, ("a", "c", "f")),
]
for i, item in enumerate(node_generator(node0)):
self.assertEqual(item, expected_node_path_tuples[i])