CSAir

.idea
    .name 13 B
    Assignment2.0.iml 295 B
    dictionaries
       lantay77.xml 357 B
    encodings.xml 171 B
    misc.xml 348 B
    modules.xml 289 B
    runConfigurations
       Server.xml 1008 B
       Unittests_in_Tests.xml 1 KB
    scopes
       scope_settings.xml 143 B
    vcs.xml 189 B
    workspace.xml 59 KB
CSAir.py 11 KB
CSAir_helpers.py 9 KB
CSAir_json.py 7 KB
CSAir_server.py 5 KB
Graph
    DiGraph.py 9 KB
    Edge.py 518 B
    Node.py 360 B
Location.py 1 KB
ManualTestPlan.odt 441 KB
ManualTestPlan.pdf 1029 KB
Query.py 2 KB
README 499 B
Server.py 8 KB
Tests
    expected
       brief_print_expected.txt 13 B
       continents_expected.txt 58 B
       expected.json 1 KB
       hubs_expected.txt 70 B
       list_expected.txt 28 B
       longest_expected.txt 36 B
       other_expected.txt 205 B
       print_expected.txt 130 B
       shortest_expected.txt 37 B
    invalid.json 15 KB
    out
       brief_print_test.txt 13 B
       continents_test.txt 58 B
       hubs_test.txt 70 B
       list_test.txt 28 B
       longest_test.txt 36 B
       other_test.txt 205 B
       out.json 1 KB
       print_test.txt 130 B
       shortest_test.txt 37 B
    testCSAir.py 3 KB
    testFunctionsForServer.py 1 KB
    testGraph.py 1 KB
    testLocation.py 938 B
    testServer.py 6 KB
    test_2.1.py 8 KB
    valid.json 871 B
    valid2.json 582 B
add_city_post.py 800 B
changes 633 B
cmi_hub.json 1 KB
map_data.json 15 KB
map_data_r2.json 15 KB
static
    bootstrap.js 59 KB
    css
       bootstrap-theme.css 20 KB
       bootstrap-theme.css.map 22 KB
       bootstrap-theme.min.css 18 KB
       bootstrap.css 129 KB
       bootstrap.css.map 215 KB
       bootstrap.min.css 106 KB
       starter-template.css 186 B
    fonts
       glyphicons-halflings-regular.eot 19 KB
       glyphicons-halflings-regular.svg 61 KB
       glyphicons-halflings-regular.ttf 40 KB
       glyphicons-halflings-regular.woff 22 KB
    js
       bootstrap.js 59 KB
       bootstrap.min.js 31 KB
templates
    city.html 13 KB
    routes.html 10 KB
uploads
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()