/
test_dictrenderer.py
116 lines (96 loc) · 3.59 KB
/
test_dictrenderer.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
import unittest
from datatree import Tree, n
from datatree.render.dictrender import DictTreeRenderer, NodeLossError
class test_DictRenderer(unittest.TestCase):
def test_nested_render(self):
tree = Tree()
with tree.root('root') as root:
root.name('Ponty Feeb')
root.place('Holey')
with root.books() as books:
books.one(1)
books.two(2)
actual = tree.render('dict')
expected = {'root': {
'name': 'Ponty Feeb',
'place': 'Holey',
'books': {
'two': 2,
'one': 1
},
}
}
self.assertDictEqual(actual, expected)
def test__children_distinct_names(self):
tree = Tree()
with tree.root() as root:
root.person('One')
root.person('Two')
root.person('Three')
render = DictTreeRenderer()
self.assertSetEqual(render._children_distinct_names(root.__children__), set(["person"]))
def test__children_distinct_names_are_different(self):
tree = Tree()
with tree.root() as root:
root.person('One')
root.different('Two')
root.strokes('Three')
render = DictTreeRenderer()
expected = set(["person", "different", "strokes"])
self.assertSetEqual(render._children_distinct_names(root.__children__), expected)
def test__children_distinct_names_large(self):
render = DictTreeRenderer()
tree = Tree()
with tree.root() as root:
root << [n('Node', i) for i in range(1000)]
expected = set(["Node"])
self.assertSetEqual(render._children_distinct_names(root.__children__), expected)
root.different('I am different')
expected.add('different')
self.assertSetEqual(render._children_distinct_names(root.__children__), expected)
def test_duplicate_nodes_conversion(self):
tree = Tree()
with tree.tale() as root:
root.level('absurd')
with root.people() as people:
people.person('Hobo')
people.person('Princess')
actual = tree.render('dict')
expected = {'tale': {
'level': 'absurd',
'people': ['Hobo', 'Princess']
}
}
self.assertDictEqual(actual, expected)
def test_duplicate_nodes_nodelosserror(self):
tree = Tree()
with tree.root('tale') as root:
root.level('absurd')
root.level('stupid')
root.handle('lame')
with self.assertRaises(NodeLossError):
tree.render('dict')
def test_render_option_allow_node_loss(self):
tree = Tree()
with tree.tale() as root:
root.level('absurd')
root.level('stupid')
root.handle('lame')
actual = tree.render('dict', allow_node_loss=True)
expected = {'tale': {
'level': 'stupid',
'handle': 'lame'
}
}
self.assertDictEqual(actual, expected)
def test_add_node_return(self):
tree = Tree()
root = tree.root()
self.assertEqual(root, tree.__children__[0])
def test_run_as_callable(self):
tree = Tree()
with tree.root() as root:
root.item(1)
actual = tree('dict')
expected = {'root': {'item': 1}}
self.assertDictEqual(actual, expected)