Skip to content

Commit 00ed4ec

Browse files
committed
Extend testing.
1 parent bb42a01 commit 00ed4ec

File tree

1 file changed

+282
-0
lines changed

1 file changed

+282
-0
lines changed

tests/test_node_attach_detach.py

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
# -*- coding: utf-8 -*-
2+
from helper import assert_raises, eq_
3+
4+
from anytree import AnyNode, LoopError, Node, NodeMixin, PostOrderIter, PreOrderIter, RenderTree, TreeError
5+
6+
7+
class TNode(Node):
8+
9+
TRACKING = []
10+
11+
def _pre_detach(self, parent):
12+
"""Method call before detaching from `parent`."""
13+
self.TRACKING.append("_pre_detach(%r, %r)" % (self.name, parent.name))
14+
15+
def _post_detach(self, parent):
16+
"""Method call after detaching from `parent`."""
17+
self.TRACKING.append("_post_detach(%r, %r)" % (self.name, parent.name))
18+
19+
def _pre_attach(self, parent):
20+
"""Method call before attaching to `parent`."""
21+
self.TRACKING.append("_pre_attach(%r, %r)" % (self.name, parent.name))
22+
23+
def _post_attach(self, parent):
24+
"""Method call after attaching to `parent`."""
25+
self.TRACKING.append("_post_attach(%r, %r)" % (self.name, parent.name))
26+
27+
def _pre_detach_children(self, children):
28+
"""Method call before detaching `children`."""
29+
self.TRACKING.append("_pre_detach_children(%r, %r)" % (self.name, tuple(child.name for child in children)))
30+
31+
def _post_detach_children(self, children):
32+
"""Method call after detaching `children`."""
33+
self.TRACKING.append("_post_detach_children(%r, %r)" % (self.name, tuple(child.name for child in children)))
34+
35+
def _pre_attach_children(self, children):
36+
"""Method call before attaching `children`."""
37+
self.TRACKING.append("_pre_attach_children(%r, %r)" % (self.name, tuple(child.name for child in children)))
38+
39+
def _post_attach_children(self, children):
40+
"""Method call after attaching `children`."""
41+
self.TRACKING.append("_post_attach_children(%r, %r)" % (self.name, tuple(child.name for child in children)))
42+
43+
44+
def test_parent_child():
45+
"""A tree parent and child attributes."""
46+
root = TNode("root")
47+
s0 = TNode("sub0", parent=root)
48+
s0b = TNode("sub0B", parent=s0)
49+
s0a = TNode("sub0A", parent=s0)
50+
s1 = TNode("sub1", parent=root)
51+
s1a = TNode("sub1A", parent=s1)
52+
s1b = TNode("sub1B", parent=s1)
53+
s1c = TNode("sub1C", parent=s1)
54+
s1ca = TNode("sub1Ca", parent=s1c)
55+
56+
assert TNode.TRACKING == [
57+
"_pre_attach('sub0', 'root')",
58+
"_post_attach('sub0', 'root')",
59+
"_pre_attach('sub0B', 'sub0')",
60+
"_post_attach('sub0B', 'sub0')",
61+
"_pre_attach('sub0A', 'sub0')",
62+
"_post_attach('sub0A', 'sub0')",
63+
"_pre_attach('sub1', 'root')",
64+
"_post_attach('sub1', 'root')",
65+
"_pre_attach('sub1A', 'sub1')",
66+
"_post_attach('sub1A', 'sub1')",
67+
"_pre_attach('sub1B', 'sub1')",
68+
"_post_attach('sub1B', 'sub1')",
69+
"_pre_attach('sub1C', 'sub1')",
70+
"_post_attach('sub1C', 'sub1')",
71+
"_pre_attach('sub1Ca', 'sub1C')",
72+
"_post_attach('sub1Ca', 'sub1C')",
73+
]
74+
TNode.TRACKING.clear()
75+
76+
# change parent
77+
s1ca.parent = s0
78+
79+
# break tree into two
80+
s1.parent = None
81+
82+
# set to the same
83+
s1b.parent = s1
84+
85+
assert TNode.TRACKING == [
86+
"_pre_detach('sub1Ca', 'sub1C')",
87+
"_post_detach('sub1Ca', 'sub1C')",
88+
"_pre_attach('sub1Ca', 'sub0')",
89+
"_post_attach('sub1Ca', 'sub0')",
90+
"_pre_detach('sub1', 'root')",
91+
"_post_detach('sub1', 'root')",
92+
]
93+
TNode.TRACKING.clear()
94+
95+
96+
def test_detach_children():
97+
98+
root = TNode("root")
99+
s0 = TNode("sub0", parent=root)
100+
s0b = TNode("sub0B", parent=s0)
101+
s0a = TNode("sub0A", parent=s0)
102+
s1 = TNode("sub1", parent=root)
103+
s1a = TNode("sub1A", parent=s1)
104+
s1b = TNode("sub1B", parent=s1)
105+
s1c = TNode("sub1C", parent=s1)
106+
s1ca = TNode("sub1Ca", parent=s1c)
107+
108+
assert TNode.TRACKING == [
109+
"_pre_attach('sub0', 'root')",
110+
"_post_attach('sub0', 'root')",
111+
"_pre_attach('sub0B', 'sub0')",
112+
"_post_attach('sub0B', 'sub0')",
113+
"_pre_attach('sub0A', 'sub0')",
114+
"_post_attach('sub0A', 'sub0')",
115+
"_pre_attach('sub1', 'root')",
116+
"_post_attach('sub1', 'root')",
117+
"_pre_attach('sub1A', 'sub1')",
118+
"_post_attach('sub1A', 'sub1')",
119+
"_pre_attach('sub1B', 'sub1')",
120+
"_post_attach('sub1B', 'sub1')",
121+
"_pre_attach('sub1C', 'sub1')",
122+
"_post_attach('sub1C', 'sub1')",
123+
"_pre_attach('sub1Ca', 'sub1C')",
124+
"_post_attach('sub1Ca', 'sub1C')",
125+
]
126+
TNode.TRACKING.clear()
127+
128+
del s0.children
129+
130+
assert TNode.TRACKING == [
131+
"_pre_detach_children('sub0', ('sub0B', 'sub0A'))",
132+
"_pre_detach('sub0B', 'sub0')",
133+
"_post_detach('sub0B', 'sub0')",
134+
"_pre_detach('sub0A', 'sub0')",
135+
"_post_detach('sub0A', 'sub0')",
136+
"_post_detach_children('sub0', ('sub0B', 'sub0A'))",
137+
]
138+
TNode.TRACKING.clear()
139+
140+
del s1.children
141+
142+
assert TNode.TRACKING == [
143+
"_pre_detach_children('sub1', ('sub1A', 'sub1B', 'sub1C'))",
144+
"_pre_detach('sub1A', 'sub1')",
145+
"_post_detach('sub1A', 'sub1')",
146+
"_pre_detach('sub1B', 'sub1')",
147+
"_post_detach('sub1B', 'sub1')",
148+
"_pre_detach('sub1C', 'sub1')",
149+
"_post_detach('sub1C', 'sub1')",
150+
"_post_detach_children('sub1', ('sub1A', 'sub1B', 'sub1C'))",
151+
]
152+
TNode.TRACKING.clear()
153+
154+
155+
def test_children_setter():
156+
157+
root = TNode("root")
158+
s0 = TNode("sub0")
159+
s1 = TNode("sub0A")
160+
s0a = TNode("sub0B")
161+
162+
assert TNode.TRACKING == []
163+
TNode.TRACKING.clear()
164+
165+
root.children = [s0, s1]
166+
s0.children = [s0a]
167+
168+
assert TNode.TRACKING == [
169+
"_pre_detach_children('root', ())",
170+
"_post_detach_children('root', ())",
171+
"_pre_attach_children('root', ('sub0', 'sub0A'))",
172+
"_pre_attach('sub0', 'root')",
173+
"_post_attach('sub0', 'root')",
174+
"_pre_attach('sub0A', 'root')",
175+
"_post_attach('sub0A', 'root')",
176+
"_post_attach_children('root', ('sub0', 'sub0A'))",
177+
"_pre_detach_children('sub0', ())",
178+
"_post_detach_children('sub0', ())",
179+
"_pre_attach_children('sub0', ('sub0B',))",
180+
"_pre_attach('sub0B', 'sub0')",
181+
"_post_attach('sub0B', 'sub0')",
182+
"_post_attach_children('sub0', ('sub0B',))",
183+
]
184+
TNode.TRACKING.clear()
185+
186+
with assert_raises(LoopError, "Cannot set parent. TNode('/root/sub0') cannot be parent of itself."):
187+
s0.children = [s0]
188+
189+
# test whether tree is unchanged after LoopError
190+
assert TNode.TRACKING == [
191+
"_pre_detach_children('sub0', ('sub0B',))",
192+
"_pre_detach('sub0B', 'sub0')",
193+
"_post_detach('sub0B', 'sub0')",
194+
"_post_detach_children('sub0', ('sub0B',))",
195+
"_pre_attach_children('sub0', ('sub0',))",
196+
"_pre_detach_children('sub0', ())",
197+
"_post_detach_children('sub0', ())",
198+
"_pre_attach_children('sub0', ('sub0B',))",
199+
"_pre_attach('sub0B', 'sub0')",
200+
"_post_attach('sub0B', 'sub0')",
201+
"_post_attach_children('sub0', ('sub0B',))",
202+
]
203+
TNode.TRACKING.clear()
204+
205+
with assert_raises(LoopError, "Cannot set parent. TNode('/root/sub0') is parent of TNode('/root/sub0/sub0B')."):
206+
s0a.children = [s0]
207+
208+
# test whether tree is unchanged after LoopError
209+
assert TNode.TRACKING == [
210+
"_pre_detach_children('sub0B', ())",
211+
"_post_detach_children('sub0B', ())",
212+
"_pre_attach_children('sub0B', ('sub0',))",
213+
"_pre_detach_children('sub0B', ())",
214+
"_post_detach_children('sub0B', ())",
215+
"_pre_attach_children('sub0B', ())",
216+
"_post_attach_children('sub0B', ())",
217+
]
218+
TNode.TRACKING.clear()
219+
220+
root.children = [s0, s1]
221+
222+
assert TNode.TRACKING == [
223+
"_pre_detach_children('root', ('sub0', 'sub0A'))",
224+
"_pre_detach('sub0', 'root')",
225+
"_post_detach('sub0', 'root')",
226+
"_pre_detach('sub0A', 'root')",
227+
"_post_detach('sub0A', 'root')",
228+
"_post_detach_children('root', ('sub0', 'sub0A'))",
229+
"_pre_attach_children('root', ('sub0', 'sub0A'))",
230+
"_pre_attach('sub0', 'root')",
231+
"_post_attach('sub0', 'root')",
232+
"_pre_attach('sub0A', 'root')",
233+
"_post_attach('sub0A', 'root')",
234+
"_post_attach_children('root', ('sub0', 'sub0A'))",
235+
]
236+
TNode.TRACKING.clear()
237+
238+
s0.children = [s0a]
239+
240+
assert TNode.TRACKING == [
241+
"_pre_detach_children('sub0', ('sub0B',))",
242+
"_pre_detach('sub0B', 'sub0')",
243+
"_post_detach('sub0B', 'sub0')",
244+
"_post_detach_children('sub0', ('sub0B',))",
245+
"_pre_attach_children('sub0', ('sub0B',))",
246+
"_pre_attach('sub0B', 'sub0')",
247+
"_post_attach('sub0B', 'sub0')",
248+
"_post_attach_children('sub0', ('sub0B',))",
249+
]
250+
TNode.TRACKING.clear()
251+
252+
s0a.children = [s1]
253+
254+
assert TNode.TRACKING == [
255+
"_pre_detach_children('sub0B', ())",
256+
"_post_detach_children('sub0B', ())",
257+
"_pre_attach_children('sub0B', ('sub0A',))",
258+
"_pre_detach('sub0A', 'root')",
259+
"_post_detach('sub0A', 'root')",
260+
"_pre_attach('sub0A', 'sub0B')",
261+
"_post_attach('sub0A', 'sub0B')",
262+
"_post_attach_children('sub0B', ('sub0A',))",
263+
]
264+
TNode.TRACKING.clear()
265+
266+
root.children = [s0, s1]
267+
268+
assert TNode.TRACKING == [
269+
"_pre_detach_children('root', ('sub0',))",
270+
"_pre_detach('sub0', 'root')",
271+
"_post_detach('sub0', 'root')",
272+
"_post_detach_children('root', ('sub0',))",
273+
"_pre_attach_children('root', ('sub0', 'sub0A'))",
274+
"_pre_attach('sub0', 'root')",
275+
"_post_attach('sub0', 'root')",
276+
"_pre_detach('sub0A', 'sub0B')",
277+
"_post_detach('sub0A', 'sub0B')",
278+
"_pre_attach('sub0A', 'root')",
279+
"_post_attach('sub0A', 'root')",
280+
"_post_attach_children('root', ('sub0', 'sub0A'))",
281+
]
282+
TNode.TRACKING.clear()

0 commit comments

Comments
 (0)