testGraph.py
import unittest
from Graph import DiGraph
class DiGraphTests(unittest.TestCase):
def setUp(self):
self.graph = DiGraph.DiGraph()
def tearDown(self):
self.graph.clear()
def test_add_and_get_node(self):
self.graph.add_node(5, "The number 5")
node = self.graph.find_node("The number 5")
assert node.get_object() == 5
assert node == "The number 5"
def test_add_and_get_nodes(self):
for i in range(0, 50):
self.graph.add_node(i, "The number " + str(i))
nodes = self.graph.get_nodes()
i = 0
for node in nodes:
#print(node)
assert node.get_object() == i
assert node == ("The number " + str(i))
i += 1
def test_edge(self):
self.graph.add_node(1, "CMI")
self.graph.add_node(2, "FPR")
self.graph.add_edge("FPR", "CMI", "999")
edges = self.graph.get_edges_for_node("FPR")
assert edges[0] == "999"
assert edges[0].get_source() == "FPR"
assert edges[0].get_destination() == "CMI"
edges = self.graph.get_edges()
assert edges[0] == "999"
assert edges[0].get_source() == "FPR"
assert edges[0].get_destination() == "CMI"
def test_find_invalid_node(self):
self.graph.add_node(1, "CMI")
node = self.graph.find_node("DOES NOT EXIST")
assert node is None
def test_add_duplicate_node(self):
self.graph.add_node(1, "CMI")
assert self.graph.add_node(5, "CMI") is False
if __name__ == '__main__':
unittest.main()