@@ -42,30 +42,43 @@ def test_02_dag(self):
42
42
43
43
log .debug ("descendants_tree" )
44
44
tree = root .descendants_tree ()
45
- # {<ConcreteNode: # 5>: {<ConcreteNode: # 7>: {}}}
46
45
self .assertIn (a1 , tree )
47
46
self .assertEqual (len (tree ), 1 )
48
47
self .assertIn (b1 , tree [a1 ])
49
48
self .assertEqual (tree [a1 ][b1 ], {})
50
49
51
- log .debug ("descendants_ids" )
52
- l = root .descendants_ids ()
53
- self .assertEqual (l , [12 , 15 ])
50
+ log .debug ("descendants part 1" )
51
+ root_descendants = root .descendants ()
52
+ self .assertNotIn (root , root_descendants )
53
+ self .assertTrue (all (elem in root_descendants for elem in [a1 , b1 ]))
54
54
55
55
root .add_child (a2 )
56
56
a3 .add_parent (root )
57
57
a3 .add_child (b3 )
58
58
a3 .add_child (b4 )
59
59
b3 .add_child (c1 )
60
- log .debug ("descendants_ids" )
61
- l = root .descendants_ids ()
62
- self .assertEqual (l , [12 , 13 , 14 , 15 , 17 , 18 , 19 ])
63
-
60
+
61
+ log .debug ("descendants part 2" )
62
+ root_descendants = root .descendants ()
63
+ self .assertNotIn (root , root_descendants )
64
+ self .assertTrue (all (elem in root_descendants for elem in [a1 , a2 , a3 , b1 , b3 , b4 , c1 ]))
65
+
66
+ log .debug ("ancestors part 1" )
67
+ c1_ancestors = c1 .ancestors ()
68
+ self .assertNotIn (c1 , c1_ancestors )
69
+ self .assertNotIn (b4 , c1_ancestors )
70
+ self .assertTrue (all (elem in c1_ancestors for elem in [root , a3 , b3 ]))
71
+
64
72
a1 .add_child (b2 )
65
73
a2 .add_child (b2 )
66
74
b3 .add_child (c2 )
67
75
b4 .add_child (c1 )
68
-
76
+
77
+ log .debug ("ancestors part 2" )
78
+ c1_ancestors = c1 .ancestors ()
79
+ self .assertNotIn (c1 , c1_ancestors )
80
+ self .assertTrue (all (elem in c1_ancestors for elem in [root , a3 , b3 , b4 ]))
81
+
69
82
# Try to add a node that is already an ancestor
70
83
try :
71
84
b3 .add_parent (c1 )
@@ -117,12 +130,6 @@ def test_02_dag(self):
117
130
self .assertEqual (len (tree_from_leaf [b4 ][a3 ]), 1 )
118
131
119
132
# Check other ancestor methods
120
- log .debug ("ancestors_ids" )
121
- self .assertEqual (a1 .ancestors_ids (), [root .id ])
122
- log .debug ("ancestors_and_self_ids" )
123
- self .assertEqual (a1 .ancestors_and_self_ids (), [root .id , a1 .id ])
124
- log .debug ("self_and_ancestors_ids" )
125
- self .assertEqual (a1 .self_and_ancestors_ids (), [a1 .id , root .id ])
126
133
log .debug ("ancestors_and_self" )
127
134
self .assertEqual (a1 .ancestors_and_self ()[0 ], root )
128
135
log .debug ("ancestors_and_self" )
@@ -133,12 +140,6 @@ def test_02_dag(self):
133
140
self .assertEqual (a1 .self_and_ancestors ()[1 ], root )
134
141
135
142
# Check other descendant methods
136
- log .debug ("descendants_ids" )
137
- self .assertEqual (b4 .descendants_ids (), [c1 .id ])
138
- log .debug ("descendants_and_self_ids" )
139
- self .assertEqual (b4 .descendants_and_self_ids (), [c1 .id , b4 .id ])
140
- log .debug ("self_and_descendants_ids" )
141
- self .assertEqual (b4 .self_and_descendants_ids (), [b4 .id , c1 .id ])
142
143
log .debug ("descendants_and_self" )
143
144
self .assertEqual (b4 .descendants_and_self ()[0 ], c1 )
144
145
log .debug ("descendants_and_self" )
@@ -150,7 +151,7 @@ def test_02_dag(self):
150
151
151
152
# Check clan methods
152
153
log .debug ("clan_ids" )
153
- self .assertEqual ( a1 .clan_ids (), [root . id , a1 . id , b1 . id , b2 . id ] )
154
+ self .assertTrue ( all ( elem in a1 .clan () for elem in [root , a1 , b1 , b2 ]) )
154
155
log .debug ("clan" )
155
156
self .assertEqual (a1 .clan ()[0 ], root )
156
157
log .debug ("clan" )
@@ -162,38 +163,38 @@ def test_02_dag(self):
162
163
163
164
# Test additional fields for edge
164
165
self .assertEqual (b3 .children .through .objects .filter (child = c1 )[0 ].name , "b3 c1" )
165
- self .assertEqual (b3 .descendants_edges ().first (), NetworkEdge .objects .get (parent = b3 , child = c2 ))
166
+ self .assertEqual (b3 .descendants_edges ().first (), NetworkEdge .objects .get (parent = b3 , child = c1 ))
166
167
self .assertEqual (a1 .ancestors_edges ().first (), NetworkEdge .objects .get (parent = root , child = a1 ))
167
168
self .assertTrue (NetworkEdge .objects .get (parent = a1 , child = b2 ) in a1 .clan_edges ())
168
169
self .assertTrue (NetworkEdge .objects .get (parent = a1 , child = b1 ) in a1 .clan_edges ())
169
170
self .assertTrue (NetworkEdge .objects .get (parent = root , child = a1 ) in a1 .clan_edges ())
170
171
171
172
# Test shortest_path
172
- log .debug ("shortest_path x2" )
173
+ log .debug ("path x2" )
173
174
self .assertTrue (
174
- [p .name for p in root .shortest_path (c1 )] == ["root" , "a3" , "b3" , "c1" ]
175
- or [p .name for p in c1 .shortest_path (root , directional = False )]
175
+ [p .name for p in root .path (c1 )] == ["root" , "a3" , "b3" , "c1" ]
176
+ or [p .name for p in c1 .path (root , directional = False )]
176
177
== ["root" , "a3" , "b4" , "c1" ]
177
178
)
178
179
179
- log .debug ("shortest_path " )
180
+ log .debug ("path " )
180
181
try :
181
182
[p .name for p in c1 .shortest_path (root )]
182
183
except Exception as e :
183
184
self .assertRaises (NodeNotReachableException )
184
185
185
186
log .debug ("shortest_path x2" )
186
187
self .assertTrue (
187
- [p .name for p in c1 .shortest_path (root , directional = False )]
188
- == ["root " , "a3 " , "b3 " , "c1 " ]
189
- or [p .name for p in c1 .shortest_path (root , directional = False )]
190
- == ["root " , "a3 " , "b4 " , "c1 " ]
188
+ [p .name for p in c1 .path (root , directional = False )]
189
+ == ["c1 " , "b3 " , "a3 " , "root " ]
190
+ or [p .name for p in c1 .path (root , directional = False )]
191
+ == ["c1 " , "b4 " , "a3 " , "root " ]
191
192
)
192
193
193
194
log .debug ("get_leaves" )
194
- self .assertEqual ([p .name for p in root .get_leaves ()], ["b2" , "c1" , "c2" , "b1" ])
195
+ self .assertEqual ([p .name for p in root .leaves ()], ["b2" , "c1" , "c2" , "b1" ])
195
196
log .debug ("get_roots" )
196
- self .assertEqual ([p .name for p in c2 .get_roots ()], ["root" ])
197
+ self .assertEqual ([p .name for p in c2 .roots ()], ["root" ])
197
198
198
199
self .assertTrue (root .is_root ())
199
200
self .assertTrue (c1 .is_leaf ())
@@ -471,19 +472,20 @@ def test_02_dag(self):
471
472
log .debug ("Execution time in seconds: %s" % str (execution_time ))
472
473
473
474
# Count number of paths from start to end of graph
474
- start_time = time .time ()
475
- log .debug (
476
- "Paths through graph: : %s"
477
- % str (
478
- len (
479
- canal_root .path_ids_list (
480
- canal_leaf , max_depth = n + 1 , max_paths = 500000000
481
- )
482
- )
483
- )
484
- )
485
- execution_time = time .time () - start_time
486
- log .debug ("Execution time in seconds: %s" % str (execution_time ))
475
+ # NOTE: Does not work with current method of returning only a single path
476
+ # start_time = time.time()
477
+ # log.debug(
478
+ # "Paths through graph: : %s"
479
+ # % str(
480
+ # len(
481
+ # canal_root.path_ids_list(
482
+ # canal_leaf, max_depth=n + 1, max_paths=500000000
483
+ # )
484
+ # )
485
+ # )
486
+ # )
487
+ # execution_time = time.time() - start_time
488
+ # log.debug("Execution time in seconds: %s" % str(execution_time))
487
489
488
490
# Find distance from root to leaf
489
491
log .debug ("Distance: %s" % str (canal_root .distance (canal_leaf , max_depth = 100 )))
@@ -546,16 +548,17 @@ def run_test():
546
548
first = NetworkNode .objects .get (name = "0" )
547
549
last = NetworkNode .objects .get (pk = 2 * n - 1 )
548
550
549
- # Count number of paths from start to end of graph
550
- start_time = time .time ()
551
- log .debug (
552
- "Paths through graph: %s"
553
- % str (
554
- len (first .path_ids_list (last , max_depth = n + 1 , max_paths = 500000000 ))
555
- )
556
- )
557
- execution_time = time .time () - start_time
558
- log .debug ("Execution time in seconds: %s" % str (execution_time ))
551
+ # # Count number of paths from start to end of graph
552
+ # # NOTE: Does not work with current method of returning only a single path
553
+ # start_time = time.time()
554
+ # log.debug(
555
+ # "Paths through graph: %s"
556
+ # % str(
557
+ # len(first.path_ids_list(last, max_depth=n + 1, max_paths=500000000))
558
+ # )
559
+ # )
560
+ # execution_time = time.time() - start_time
561
+ # log.debug("Execution time in seconds: %s" % str(execution_time))
559
562
560
563
log .debug ("Distance" )
561
564
self .assertEqual (first .distance (last , max_depth = n ), n - 1 )
0 commit comments