test_2.1.py
import unittest
from CSAir_json import *
from CSAir_helpers import *
import CSAir
import filecmp
class TestsTwoPointOne(unittest.TestCase):
def setUp(self):
self.graph = parse_json('valid.json')
print(self.graph)
def tearDown(self):
self.graph.clear()
def test_remove_node(self):
self.graph.remove_node("SCL")
nodes = self.graph.get_nodes()
assert len(nodes) == 1
assert nodes[0] == "LIM"
edges = self.graph.get_edges()
assert len(edges) == 0
def test_remove_edge(self):
edges = self.graph.get_edges()
assert self.graph.remove_edge(edges[0].get_source(), edges[0].get_destination()) is True
assert edges[0].get_source() == "LIM"
assert self.graph.remove_edge(edges[0].get_source(), edges[0].get_destination()) is True
assert len(self.graph.get_edges()) == 0
def test_get_edge_from_nodes(self):
assert self.graph.get_edge_from_nodes("LIM", "SCL") == "2453"
assert self.graph.get_edge_from_nodes("LIM", "AAA") is None
def test_merge(self):
graph_two = parse_json('../cmi_hub.json')
self.graph.merge(graph_two)
assert "CMI" in self.graph.get_nodes()
assert "LIM" in self.graph.get_nodes()
assert "SCL" in self.graph.get_nodes()
assert len(self.graph.get_nodes()) == 3
assert len(self.graph.get_edges()) == 20
def test_remove_route(self):
edges = self.graph.get_edges()
assert remove_route_helper(self.graph, edges[0].get_source(), edges[0].get_destination(), "y") is True
assert len(self.graph.get_edges()) == 0
def test_remove_route_two(self):
edges = self.graph.get_edges()
assert remove_route_helper(self.graph, edges[0].get_source(), edges[0].get_destination(), "n") is True
assert len(self.graph.get_edges()) == 1
def test_remove_route_invalid(self):
edges = self.graph.get_edges()
assert remove_route_helper(self.graph, edges[0].get_source(), "NOTVALID", "n") is False
assert len(self.graph.get_edges()) == 2
def test_merge_json(self):
assert merge_json(self.graph, '../cmi_hub.json') is True
assert "CMI" in self.graph.get_nodes()
assert "LIM" in self.graph.get_nodes()
assert "SCL" in self.graph.get_nodes()
assert len(self.graph.get_nodes()) == 3
assert len(self.graph.get_edges()) == 20
def test_merge_json_invalid(self):
assert merge_json(self.graph, 'NOTVALID') is False
assert "LIM" in self.graph.get_nodes()
assert "SCL" in self.graph.get_nodes()
assert len(self.graph.get_nodes()) == 2
assert len(self.graph.get_edges()) == 2
def test_merge_update_label(self):
assert merge_json(self.graph, 'valid2.json') is True
assert "LIM" in self.graph.get_nodes()
assert "SCL" in self.graph.get_nodes()
edges = self.graph.get_edges()
assert len(self.graph.get_nodes()) == 2
assert len(edges) == 2
print(edges)
assert edges[0] == str(2453)
assert edges[1] == str(245300)
def test_add_city(self):
code = "CMI"
name = "Champaign"
country = "US"
continent = "North America"
timezone = -6
latitude = "40N"
longitude = "88W"
population = 226000
region = 1
assert "Error" not in add_city_helper(self.graph, code, name, country, continent, timezone, latitude, longitude, population, region)
assert "CMI" in self.graph.get_nodes()
def test_add_city_invalid_pop(self):
code = "CMI"
name = "Champaign"
country = "US"
continent = "North America"
timezone = -6
latitude = "40N"
longitude = "88W"
population = -226000
region = 1
assert "Error" in add_city_helper(self.graph, code, name, country, continent, timezone, latitude, longitude, population, region)
assert "CMI" not in self.graph.get_nodes()
def test_add_city_invalid_alpha_pop(self):
code = "CMI"
name = "Champaign"
country = "US"
continent = "North America"
timezone = -6
latitude = "40N"
longitude = "88W"
population = "A"
region = 1
assert "Error" in add_city_helper(self.graph, code, name, country, continent, timezone, latitude, longitude, population, region)
assert "CMI" not in self.graph.get_nodes()
def test_add_city_invalid_coords(self):
code = "CMI"
name = "Champaign"
country = "US"
continent = "North America"
timezone = -6
latitude = "qwertyN"
longitude = "88S"
population = 226000
region = 1
assert "Error" in add_city_helper(self.graph, code, name, country, continent, timezone, latitude, longitude, population, region)
assert "CMI" not in self.graph.get_nodes()
def test_add_city_invalid_coords2(self):
code = "CMI"
name = "Champaign"
country = "US"
continent = "North America"
timezone = -6
latitude = "N"
longitude = "W"
population = 226000
region = 1
assert "Error" in add_city_helper(self.graph, code, name, country, continent, timezone, latitude, longitude, population, region)
assert "CMI" not in self.graph.get_nodes()
def test_add_route(self):
assert add_route_helper(self.graph, "LIM", "SCL", 555, "n") is True
edges = self.graph.get_edges()
assert edges[0] == str(2453)
assert edges[1] == str(555)
assert add_route_helper(self.graph, "LIM", "SCL", 555, "y") is True
edges = self.graph.get_edges()
assert edges[0] == str(555)
assert edges[1] == str(555)
assert add_route_helper(self.graph, "LIM", "CMI", 555, "y") is False
def test_update_routes(self):
edges = self.graph.get_edges()
assert edges[0].get_destination() == "LIM"
assert edges[1].get_source() == "LIM"
update_routes(self.graph, "CMI", "LIM")
edges = self.graph.get_edges()
assert len(edges) == 2
print(edges[1].get_source())
print(edges[1].get_destination())
print(edges[0].get_source())
print(edges[0].get_destination())
assert edges[0].get_destination() == "CMI"
assert edges[1].get_source() == "CMI"
def test_check_directed(self):
assert CSAir.check_directed(self.graph) is False
assert add_route_helper(self.graph, "LIM", "SCL", 555, "n") is True
assert CSAir.check_directed(self.graph) is True
assert add_route_helper(self.graph, "SCL", "LIM", 555, "n") is True
assert CSAir.check_directed(self.graph) is False
def test_save_json(self):
assert add_route_helper(self.graph, "SCL", "LIM", 245300, "y") is True
assert save_json(self.graph, 'out/out.json') is True
assert filecmp.cmp('out/out.json', 'expected/expected.json') is True
def test_save_json_invalid(self):
assert save_json(self.graph, 5) is False
def test_time_for_acceleration(self):
assert int(time_for_acceleration(375000)) == 3600
def test_dijkstra(self):
assert ['SCL', 'LIM'] == self.graph.dijkstra(self.graph._nodes[0], self.graph._nodes[1])
def test_dijkstra_YYZ_LAX(self):
merge_json(self.graph, '../map_data.json')
assert ['YYZ', 'CHI', 'LAX'] == self.graph.dijkstra("YYZ", "LAX")
def test_dijkstra_JNB_BUE(self):
merge_json(self.graph, '../map_data.json')
assert ['JNB', 'FIH', 'LOS', 'SAO', 'BUE'] == self.graph.dijkstra("JNB", "BUE")
def test_dijkstra_JNB_LAX(self):
merge_json(self.graph, '../map_data.json')
assert ['JNB', 'KRT', 'CAI', 'IST', 'ALG', 'MAD', 'NYC', 'YYZ', 'CHI', 'LAX'] == self.graph.dijkstra("JNB", "LAX")
def test_dijkstra_invalid(self):
assert self.graph.dijkstra("AAA", "LIM") is None
def test_seconds_to_formatted_time(self):
assert seconds_to_formatted_time(time_for_acceleration(375000)) == "1:00"
def test_calc_layover_time(self):
assert calc_layover_time(self.graph, "LIM") == 120*60
merge_json(self.graph, '../map_data.json')
assert calc_layover_time(self.graph, "YYZ") == 100*60
assert calc_layover_time(self.graph, "IST") == 70*60
def test_calc_leg_time(self):
assert int(calc_leg_time(400)) == 3840
assert int(calc_leg_time(399)) == 3830
assert int(calc_leg_time(600)) == 4800
if __name__ == '__main__':
unittest.main()