Skip to content

Commit 26537d8

Browse files
Updated tests to work with latest updates
Still more work to do in order to cover new and upcoming functionality
1 parent 1433dc0 commit 26537d8

File tree

2 files changed

+59
-101
lines changed

2 files changed

+59
-101
lines changed

tests/dag_output.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/test.py

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,43 @@ def test_02_dag(self):
4242

4343
log.debug("descendants_tree")
4444
tree = root.descendants_tree()
45-
# {<ConcreteNode: # 5>: {<ConcreteNode: # 7>: {}}}
4645
self.assertIn(a1, tree)
4746
self.assertEqual(len(tree), 1)
4847
self.assertIn(b1, tree[a1])
4948
self.assertEqual(tree[a1][b1], {})
5049

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]))
5454

5555
root.add_child(a2)
5656
a3.add_parent(root)
5757
a3.add_child(b3)
5858
a3.add_child(b4)
5959
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+
6472
a1.add_child(b2)
6573
a2.add_child(b2)
6674
b3.add_child(c2)
6775
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+
6982
# Try to add a node that is already an ancestor
7083
try:
7184
b3.add_parent(c1)
@@ -117,12 +130,6 @@ def test_02_dag(self):
117130
self.assertEqual(len(tree_from_leaf[b4][a3]), 1)
118131

119132
# 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])
126133
log.debug("ancestors_and_self")
127134
self.assertEqual(a1.ancestors_and_self()[0], root)
128135
log.debug("ancestors_and_self")
@@ -133,12 +140,6 @@ def test_02_dag(self):
133140
self.assertEqual(a1.self_and_ancestors()[1], root)
134141

135142
# 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])
142143
log.debug("descendants_and_self")
143144
self.assertEqual(b4.descendants_and_self()[0], c1)
144145
log.debug("descendants_and_self")
@@ -150,7 +151,7 @@ def test_02_dag(self):
150151

151152
# Check clan methods
152153
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]))
154155
log.debug("clan")
155156
self.assertEqual(a1.clan()[0], root)
156157
log.debug("clan")
@@ -162,38 +163,38 @@ def test_02_dag(self):
162163

163164
# Test additional fields for edge
164165
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))
166167
self.assertEqual(a1.ancestors_edges().first(), NetworkEdge.objects.get(parent=root, child=a1))
167168
self.assertTrue(NetworkEdge.objects.get(parent=a1, child=b2) in a1.clan_edges())
168169
self.assertTrue(NetworkEdge.objects.get(parent=a1, child=b1) in a1.clan_edges())
169170
self.assertTrue(NetworkEdge.objects.get(parent=root, child=a1) in a1.clan_edges())
170171

171172
# Test shortest_path
172-
log.debug("shortest_path x2")
173+
log.debug("path x2")
173174
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)]
176177
== ["root", "a3", "b4", "c1"]
177178
)
178179

179-
log.debug("shortest_path")
180+
log.debug("path")
180181
try:
181182
[p.name for p in c1.shortest_path(root)]
182183
except Exception as e:
183184
self.assertRaises(NodeNotReachableException)
184185

185186
log.debug("shortest_path x2")
186187
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"]
191192
)
192193

193194
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"])
195196
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"])
197198

198199
self.assertTrue(root.is_root())
199200
self.assertTrue(c1.is_leaf())
@@ -471,19 +472,20 @@ def test_02_dag(self):
471472
log.debug("Execution time in seconds: %s" % str(execution_time))
472473

473474
# 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))
487489

488490
# Find distance from root to leaf
489491
log.debug("Distance: %s" % str(canal_root.distance(canal_leaf, max_depth=100)))
@@ -546,16 +548,17 @@ def run_test():
546548
first = NetworkNode.objects.get(name="0")
547549
last = NetworkNode.objects.get(pk=2 * n - 1)
548550

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))
559562

560563
log.debug("Distance")
561564
self.assertEqual(first.distance(last, max_depth=n), n - 1)

0 commit comments

Comments
 (0)