Debian Codegraph @

tests/test_node_attach_detach.py

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