4
4
5
5
from django .test import TestCase
6
6
from django .core .exceptions import ValidationError
7
- from django_postgresql_dag .exceptions import NodeNotReachableException , GraphModelsCannotBeParsedException , IncorrectUsageException
8
- from django_postgresql_dag .transformations import _ordered_filter , edges_from_nodes_queryset , nodes_from_edges_queryset , nx_from_queryset , model_to_dict
9
- from django_postgresql_dag .query_builders import AncestorQuery , DescendantQuery , UpwardPathQuery , DownwardPathQuery , ConnectedGraphQuery
7
+ from django_postgresql_dag .exceptions import (
8
+ NodeNotReachableException ,
9
+ GraphModelsCannotBeParsedException ,
10
+ IncorrectUsageException ,
11
+ )
12
+ from django_postgresql_dag .transformations import (
13
+ _ordered_filter ,
14
+ edges_from_nodes_queryset ,
15
+ nodes_from_edges_queryset ,
16
+ nx_from_queryset ,
17
+ model_to_dict ,
18
+ )
19
+ from django_postgresql_dag .query_builders import (
20
+ AncestorQuery ,
21
+ DescendantQuery ,
22
+ UpwardPathQuery ,
23
+ DownwardPathQuery ,
24
+ ConnectedGraphQuery ,
25
+ )
10
26
11
27
from .models import NetworkNode , NetworkEdge , NodeSet , EdgeSet
12
28
@@ -58,7 +74,7 @@ def test_02_dag(self):
58
74
a3 .add_child (b3 )
59
75
a3 .add_child (b4 )
60
76
b3 .add_child (c1 )
61
-
77
+
62
78
log .debug ("descendants part 2" )
63
79
root_descendants = root .descendants ()
64
80
self .assertNotIn (root , root_descendants )
@@ -69,17 +85,17 @@ def test_02_dag(self):
69
85
self .assertNotIn (c1 , c1_ancestors )
70
86
self .assertNotIn (b4 , c1_ancestors )
71
87
self .assertTrue (all (elem in c1_ancestors for elem in [root , a3 , b3 ]))
72
-
88
+
73
89
a1 .add_child (b2 )
74
90
a2 .add_child (b2 )
75
91
b3 .add_child (c2 )
76
92
b4 .add_child (c1 )
77
-
93
+
78
94
log .debug ("ancestors part 2" )
79
95
c1_ancestors = c1 .ancestors ()
80
96
self .assertNotIn (c1 , c1_ancestors )
81
97
self .assertTrue (all (elem in c1_ancestors for elem in [root , a3 , b3 , b4 ]))
82
-
98
+
83
99
# Try to add a node that is already an ancestor
84
100
try :
85
101
b3 .add_parent (c1 )
@@ -174,8 +190,7 @@ def test_02_dag(self):
174
190
log .debug ("path x2" )
175
191
self .assertTrue (
176
192
[p .name for p in root .path (c1 )] == ["root" , "a3" , "b3" , "c1" ]
177
- or [p .name for p in c1 .path (root , directional = False )]
178
- == ["root" , "a3" , "b4" , "c1" ]
193
+ or [p .name for p in c1 .path (root , directional = False )] == ["root" , "a3" , "b4" , "c1" ]
179
194
)
180
195
181
196
log .debug ("path" )
@@ -186,10 +201,8 @@ def test_02_dag(self):
186
201
187
202
log .debug ("shortest_path x2" )
188
203
self .assertTrue (
189
- [p .name for p in c1 .path (root , directional = False )]
190
- == ["c1" , "b3" , "a3" , "root" ]
191
- or [p .name for p in c1 .path (root , directional = False )]
192
- == ["c1" , "b4" , "a3" , "root" ]
204
+ [p .name for p in c1 .path (root , directional = False )] == ["c1" , "b3" , "a3" , "root" ]
205
+ or [p .name for p in c1 .path (root , directional = False )] == ["c1" , "b4" , "a3" , "root" ]
193
206
)
194
207
195
208
log .debug ("get_leaves" )
@@ -227,15 +240,19 @@ def test_02_dag(self):
227
240
log .debug ("ancestors" )
228
241
self .assertEqual ([p .name for p in c1 .ancestors ()], ["root" , "a3" , "b4" ])
229
242
self .assertFalse (c1 .is_island ())
230
-
243
+
231
244
# Test is we can properly export to a NetworkX graph
232
245
log = logging .getLogger ("test_02_networkx" )
233
- nx_out = nx_from_queryset (c1 .ancestors_and_self (), graph_attributes_dict = {"test" : "test" }, node_attribute_fields_list = ["id" , "name" ], edge_attribute_fields_list = ["id" , "name" ])
246
+ nx_out = nx_from_queryset (
247
+ c1 .ancestors_and_self (),
248
+ graph_attributes_dict = {"test" : "test" },
249
+ node_attribute_fields_list = ["id" , "name" ],
250
+ edge_attribute_fields_list = ["id" , "name" ],
251
+ )
234
252
log .debug ("Check attributes" )
235
253
self .assertEqual (nx_out .graph , {"test" : "test" })
236
- self .assertEqual (nx_out .nodes [11 ], {'id' : 11 , 'name' : 'root' })
237
- self .assertEqual (nx_out .edges [11 , 14 ], {'id' : 4 , 'name' : 'root a3' })
238
-
254
+ self .assertEqual (nx_out .nodes [11 ], {"id" : 11 , "name" : "root" })
255
+ self .assertEqual (nx_out .edges [11 , 14 ], {"id" : 4 , "name" : "root a3" })
239
256
240
257
"""
241
258
Simulate a basic irrigation canal network
@@ -456,14 +473,12 @@ def test_02_dag(self):
456
473
adjacency_list .append ([f"SA{ n } " , f"SB{ n } " ])
457
474
adjacency_list .append ([f"SA{ n } " , f"SC{ n } " ])
458
475
459
-
460
476
# Create and assign nodes to variables
461
477
log .debug ("Start creating nodes" )
462
478
for node in node_name_list2 :
463
479
globals ()[f"{ node } " ] = NetworkNode .objects .create (name = node )
464
480
log .debug ("Done creating nodes" )
465
481
466
-
467
482
log .debug ("Connect nodes" )
468
483
for connection in adjacency_list :
469
484
globals ()[f"{ connection [0 ]} " ].add_child (globals ()[f"{ connection [1 ]} " ])
@@ -508,7 +523,7 @@ def create_multilinked_nodes(shared_edge_count):
508
523
for _ in range (shared_edge_count ):
509
524
child_node .add_parent (parent_node )
510
525
511
- return child_node , parent_node
526
+ return child_node , parent_node
512
527
513
528
def delete_parents ():
514
529
child_node , parent_node = create_multilinked_nodes (shared_edge_count )
@@ -554,7 +569,6 @@ def run_test():
554
569
555
570
n = 22 # Keep it an even number
556
571
557
-
558
572
log .debug ("Start creating nodes" )
559
573
for i in range (2 * n ):
560
574
NetworkNode (pk = i , name = str (i )).save ()
@@ -591,17 +605,15 @@ def run_test():
591
605
last = NetworkNode .objects .get (name = str (2 * n - 1 ))
592
606
593
607
path_exists = first .path_exists (last , max_depth = n )
594
- log .debug (f"Path exists: { path_exists } " )
595
- self .assertTrue (path_exists , True )
608
+ log .debug (f"Path exists: { path_exists } " )
609
+ self .assertTrue (path_exists , True )
596
610
self .assertEqual (first .distance (last , max_depth = n ), n - 1 )
597
611
598
612
log .debug (f"Node count: { NetworkNode .objects .count ()} " )
599
613
log .debug (f"Edge count: { NetworkEdge .objects .count ()} " )
600
614
601
615
# Connect the first-created node to the last-created node
602
- NetworkNode .objects .get (pk = 0 ).add_child (
603
- NetworkNode .objects .get (pk = 2 * n - 1 )
604
- )
616
+ NetworkNode .objects .get (pk = 0 ).add_child (NetworkNode .objects .get (pk = 2 * n - 1 ))
605
617
606
618
middle = NetworkNode .objects .get (pk = n - 1 )
607
619
distance = first .distance (middle , max_depth = n )
0 commit comments