def test_save_and_loads(self): g = DiGraph() g.add_node(NodeData(0).id, pos=(1, 1, 2)) g.add_node(NodeData(3).id) g.add_node(NodeData(13).id) g.add_edge(0, 13, 8.9) g.add_edge(3, 13, 8.9) ga = GraphAlgo(g) g1 = DiGraph() g1.add_node(NodeData(0).id, pos=(1, 1, 2)) g1.add_node(NodeData(3).id) g1.add_node(NodeData(13).id) g1.add_edge(0, 13, 8.9) g1.add_edge(3, 13, 8.9) ga.save_to_json("save_test.json") ga1 = GraphAlgo(None) ga1.load_from_json("save_test.json") g2 = ga1.get_graph() self.assertEqual(g1.__str__(), g2.__str__(), "load return different graph")
def test_save_load_json(self): g = DiGraph() ga = GraphAlgo(g) ga.save_to_json("test1") ga.load_from_json("test1") assert ga.get_graph().comper(g) g = TestDiGraph.graphCreator(self, 20) g.add_edge(0, 1, 4.5) ga = GraphAlgo(g) ga.save_to_json("test2") ga.load_from_json("test1") print(ga.get_graph().__str__()) print(g.__str__()) assert not g.comper(ga.get_graph()) ga.load_from_json("test2") assert g.comper(ga.get_graph()) g = TestDiGraph.graphCreator1(self, 20, 50) ga = GraphAlgo(g) ga.save_to_json("test3") ga.load_from_json("test3") assert g.comper(ga.get_graph())
class TestGraphAlgo(unittest.TestCase): def setUp(self): self.graph = DiGraph() def test_get_graph(self): self.graph = DiGraph() for i in range(10): self.graph.add_node(i) graph_algo = GraphAlgo(self.graph) self.assertEqual(self.graph.__str__(), graph_algo.get_graph().__str__()) def test_load_from_json(self): self.graph = DiGraph() for i in range(4): self.graph.add_node(i) self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) self.graph.add_edge(1, 2, 1.3) self.graph.add_edge(1, 3, 1.8) self.graph.add_edge(2, 3, 1.1) graph_algo = GraphAlgo() graph_algo.load_from_json('../data/T0.json') self.assertEqual(self.graph, graph_algo.get_graph()) def test_save_to_json(self): self.graph = DiGraph() for i in range(4): self.graph.add_node(i) self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) self.graph.add_edge(1, 2, 1.3) self.graph.add_edge(1, 3, 1.8) self.graph.add_edge(2, 3, 1.1) graph_algo = GraphAlgo(self.graph) graph_algo.save_to_json('test.json') try: with open('test.json') as fp: saved_file = fp.read() fp.close() with open('../data/T0.json') as fp: T0 = fp.read() fp.close() self.assertEqual(saved_file, T0) finally: os.remove('test.json') def test_shortest_path(self): self.graph = DiGraph() for i in range(4): self.graph.add_node(i) self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) self.graph.add_edge(1, 2, 1.3) self.graph.add_edge(2, 3, 1.1) graph_algo = GraphAlgo(self.graph) shortest_path: List[list] = graph_algo.shortest_path(1, 3) self.assertEqual((2.4000000000000004, [1, 2, 3]), shortest_path) self.graph.add_edge(1, 3, 1.8) shortest_path: List[list] = graph_algo.shortest_path(1, 3) self.assertEqual((1.8, [1, 3]), shortest_path) def test_shortest_path_not_connected_nodes(self): self.graph = DiGraph() for i in range(4): self.graph.add_node(i) self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) self.graph.add_edge(1, 2, 1.3) self.graph.add_edge(1, 3, 1.8) self.graph.add_edge(2, 3, 1.1) self.graph.add_node(4) graph_algo = GraphAlgo(self.graph) shortest_path: List[list] = graph_algo.shortest_path(1, 4) self.assertEqual((float('inf'), []), shortest_path) def test_connected_component(self): self.graph = DiGraph() for i in range(5): self.graph.add_node(i) # 0 <-> 1 self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) # 2 self.graph.add_edge(1, 2, 1.3) # 3 <-> 4 self.graph.add_edge(4, 3, 1.1) self.graph.add_edge(3, 4, 1.1) algo = GraphAlgo(self.graph) scc: list = algo.connected_component(0) self.assertEqual([NodeData(0), NodeData(1)], scc) scc: list = algo.connected_component(1) self.assertEqual([NodeData(1), NodeData(0)], scc) scc: list = algo.connected_component(2) self.assertEqual([NodeData(2)], scc) scc: list = algo.connected_component(4) self.assertEqual([NodeData(4), NodeData(3)], scc) def test_connected_components(self): self.graph = DiGraph() for i in range(5): self.graph.add_node(i) # 0 <-> 1 self.graph.add_edge(0, 1, 1) self.graph.add_edge(1, 0, 1.1) # 2 self.graph.add_edge(1, 2, 1.3) # 3 <-> 4 self.graph.add_edge(4, 3, 1.1) self.graph.add_edge(3, 4, 1.1) algo = GraphAlgo(self.graph) scc: list = algo.connected_components() self.assertTrue(scc.__contains__([NodeData(2)])) self.assertTrue(scc.__contains__([NodeData(0), NodeData(1)])) self.assertTrue(scc.__contains__([NodeData(3), NodeData(4)])) def test_plot_graph(self): algo = GraphAlgo() algo.load_from_json('../data/A5') algo.plot_graph()