예제 #1
0
    def testAll(self):
        graph = Graph()
        self.assertEqual(graph.continent, [], "Incorrect Continent Initialization")
        self.assertEqual(graph.nodes, [], "Incorrect Node Initialization")
        self.assertEqual(graph.edges, [], "Incorrect Edge Initialization")
        self.assertFalse(graph.addDualEdge(flight1Data), "Build route between inexistent city")
        graph.addNode(city1Data)
        graph.addNode(city2Data)
        graph.addNode(city3Data)
        graph.addNode(city4Data)
        graph.addNode(city5Data)
        graph.addNode(city6Data)
        graph.addNode(city7Data)
        graph.addNode(city8Data)
        self.assertEqual(len(graph.continent), 4, "Unmatched number of continents")
        self.assertEqual(len(graph.nodes), 8, "Unsuccessful node addition")
        self.assertTrue(graph.addDualEdge(flight1Data))
        self.assertTrue(graph.addDualEdge(flight2Data))
        self.assertTrue(graph.addDualEdge(flight3Data))
        self.assertEqual(graph.routeInfo(graph.edges[0], 1), [1227, 368.09999999999997, 2.936])
        self.assertEqual(graph.shortestPath("IST", "BGW").distanceFromStartVertex, 1612, "Incorrect Shortest Path method, simplest test")

        self.assertTrue(graph.removeRoute(flight2Data["ports"][0], flight2Data["ports"][1]))
        self.assertTrue(graph.addSingleEdge(flight2Data))
        self.assertFalse(graph.addSingleEdge(flight2Data))
        self.assertEqual(len(graph.edges), 6, "Unsuccessful edge addition")
        self.assertEqual(len(graph.nodes[0].get_edgeIn()), 2, "Unequal edgeIn count")           
        self.assertEqual(len(graph.nodes[0].get_edgeOut()), 2, "Unequal edgeOut count")
        self.assertEqual(graph.findRoute("ABC", "CDE"), None)
        self.assertEqual(graph.nodes[1].get_edgeIn(), [graph.findRoute(city1Data["code"], city2Data["code"]), graph.findRoute(city3Data["code"], city2Data["code"])], "Different outgoing cities")
        
        graph.addDualEdge(flight4Data)
        graph.addDualEdge(flight5Data)
        graph.addDualEdge(flight6Data)
        graph.addDualEdge(flight7Data)
        graph.addDualEdge(flight8Data)
        graph.addDualEdge(flight9Data)
        graph.addDualEdge(flight10Data)
        self.assertEqual(graph.flightDistance(), ["\nSAO - MAD: distance 8373\nMAD - SAO: distance 8373", "\nALG - MAD: distance 726\nMAD - ALG: distance 726", "2858.8"], "Incorrect output from flightDistance method")
        self.assertEqual(graph.cityPopulation(), ["SAO", "ALG", "10206250"], "Incorrect output from cityPopulation method")
        self.assertEqual(graph.hubCity(), "CAI", "Incorrect output from hugCity method")
        self.assertFalse(graph.removeCity("ABC"), "cityCode does not exist")
        self.assertEqual(graph.shortestPath("BGW", "SAO").distanceFromStartVertex, 12065, "Incorrect Shortest Path method, second test")
        
        self.assertTrue(graph.removeCity("SAO"), "Fail on city removal")
        self.assertEqual(len(graph.edges), 16, "removeCity without removing edges")   
        self.assertEqual(len(graph.continent), 3, "continent should not be removed")

        graph.addNode(city9Data)
        graph.addNode(city10Data)
        graph.addDualEdge(flight11Data)
        print(graph.routeInfo(graph.edges[17], 1))
        self.assertEqual(graph.routeInfo(graph.edges[17], 1), [343, 102.89999999999999, 2.0])
        self.assertTrue(graph.removeCity("LOS"), "Fail on city removal")
        self.assertEqual(len(graph.continent), 3, "removeCity without removing continent") 

        self.assertTrue(graph.removeCity("ALG"), "Fail on city removal")
        self.assertEqual(graph.shortestPath("BGW", "SAO"), Null, "disconnected city")