Skip to content

Commit

Permalink
Added missed black reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
JackAtOmenApps committed Mar 20, 2021
1 parent 72a23df commit 36cb662
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@

from django.test import TestCase
from django.core.exceptions import ValidationError
from django_postgresql_dag.exceptions import NodeNotReachableException, GraphModelsCannotBeParsedException, IncorrectUsageException
from django_postgresql_dag.transformations import _ordered_filter, edges_from_nodes_queryset, nodes_from_edges_queryset, nx_from_queryset, model_to_dict
from django_postgresql_dag.query_builders import AncestorQuery, DescendantQuery, UpwardPathQuery, DownwardPathQuery, ConnectedGraphQuery
from django_postgresql_dag.exceptions import (
NodeNotReachableException,
GraphModelsCannotBeParsedException,
IncorrectUsageException,
)
from django_postgresql_dag.transformations import (
_ordered_filter,
edges_from_nodes_queryset,
nodes_from_edges_queryset,
nx_from_queryset,
model_to_dict,
)
from django_postgresql_dag.query_builders import (
AncestorQuery,
DescendantQuery,
UpwardPathQuery,
DownwardPathQuery,
ConnectedGraphQuery,
)

from .models import NetworkNode, NetworkEdge, NodeSet, EdgeSet

Expand Down Expand Up @@ -58,7 +74,7 @@ def test_02_dag(self):
a3.add_child(b3)
a3.add_child(b4)
b3.add_child(c1)

log.debug("descendants part 2")
root_descendants = root.descendants()
self.assertNotIn(root, root_descendants)
Expand All @@ -69,17 +85,17 @@ def test_02_dag(self):
self.assertNotIn(c1, c1_ancestors)
self.assertNotIn(b4, c1_ancestors)
self.assertTrue(all(elem in c1_ancestors for elem in [root, a3, b3]))

a1.add_child(b2)
a2.add_child(b2)
b3.add_child(c2)
b4.add_child(c1)

log.debug("ancestors part 2")
c1_ancestors = c1.ancestors()
self.assertNotIn(c1, c1_ancestors)
self.assertTrue(all(elem in c1_ancestors for elem in [root, a3, b3, b4]))

# Try to add a node that is already an ancestor
try:
b3.add_parent(c1)
Expand Down Expand Up @@ -174,8 +190,7 @@ def test_02_dag(self):
log.debug("path x2")
self.assertTrue(
[p.name for p in root.path(c1)] == ["root", "a3", "b3", "c1"]
or [p.name for p in c1.path(root, directional=False)]
== ["root", "a3", "b4", "c1"]
or [p.name for p in c1.path(root, directional=False)] == ["root", "a3", "b4", "c1"]
)

log.debug("path")
Expand All @@ -186,10 +201,8 @@ def test_02_dag(self):

log.debug("shortest_path x2")
self.assertTrue(
[p.name for p in c1.path(root, directional=False)]
== ["c1", "b3", "a3", "root"]
or [p.name for p in c1.path(root, directional=False)]
== ["c1", "b4", "a3", "root"]
[p.name for p in c1.path(root, directional=False)] == ["c1", "b3", "a3", "root"]
or [p.name for p in c1.path(root, directional=False)] == ["c1", "b4", "a3", "root"]
)

log.debug("get_leaves")
Expand Down Expand Up @@ -227,15 +240,19 @@ def test_02_dag(self):
log.debug("ancestors")
self.assertEqual([p.name for p in c1.ancestors()], ["root", "a3", "b4"])
self.assertFalse(c1.is_island())

# Test is we can properly export to a NetworkX graph
log = logging.getLogger("test_02_networkx")
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"])
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"],
)
log.debug("Check attributes")
self.assertEqual(nx_out.graph, {"test": "test"})
self.assertEqual(nx_out.nodes[11], {'id': 11, 'name': 'root'})
self.assertEqual(nx_out.edges[11, 14], {'id': 4, 'name': 'root a3'})

self.assertEqual(nx_out.nodes[11], {"id": 11, "name": "root"})
self.assertEqual(nx_out.edges[11, 14], {"id": 4, "name": "root a3"})

"""
Simulate a basic irrigation canal network
Expand Down Expand Up @@ -456,14 +473,12 @@ def test_02_dag(self):
adjacency_list.append([f"SA{n}", f"SB{n}"])
adjacency_list.append([f"SA{n}", f"SC{n}"])


# Create and assign nodes to variables
log.debug("Start creating nodes")
for node in node_name_list2:
globals()[f"{node}"] = NetworkNode.objects.create(name=node)
log.debug("Done creating nodes")


log.debug("Connect nodes")
for connection in adjacency_list:
globals()[f"{connection[0]}"].add_child(globals()[f"{connection[1]}"])
Expand Down Expand Up @@ -508,7 +523,7 @@ def create_multilinked_nodes(shared_edge_count):
for _ in range(shared_edge_count):
child_node.add_parent(parent_node)

return child_node, parent_node
return child_node, parent_node

def delete_parents():
child_node, parent_node = create_multilinked_nodes(shared_edge_count)
Expand Down Expand Up @@ -554,7 +569,6 @@ def run_test():

n = 22 # Keep it an even number


log.debug("Start creating nodes")
for i in range(2 * n):
NetworkNode(pk=i, name=str(i)).save()
Expand Down Expand Up @@ -591,17 +605,15 @@ def run_test():
last = NetworkNode.objects.get(name=str(2 * n - 1))

path_exists = first.path_exists(last, max_depth=n)
log.debug(f"Path exists: {path_exists}")
self.assertTrue(path_exists, True)
log.debug(f"Path exists: {path_exists}")
self.assertTrue(path_exists, True)
self.assertEqual(first.distance(last, max_depth=n), n - 1)

log.debug(f"Node count: {NetworkNode.objects.count()}")
log.debug(f"Edge count: {NetworkEdge.objects.count()}")

# Connect the first-created node to the last-created node
NetworkNode.objects.get(pk=0).add_child(
NetworkNode.objects.get(pk=2 * n - 1)
)
NetworkNode.objects.get(pk=0).add_child(NetworkNode.objects.get(pk=2 * n - 1))

middle = NetworkNode.objects.get(pk=n - 1)
distance = first.distance(middle, max_depth=n)
Expand Down

0 comments on commit 36cb662

Please sign in to comment.