testCSAir.py
import unittest
import sys
import filecmp
from CSAir import *
class CSAir_tests(unittest.TestCase):
#def setUp(self):
# self.graph = DiGraph.DiGraph()
def setUp(self):
self.graph = parse_json('valid.json')
def tearDown(self):
self.graph.clear()
def test_parse_notfound(self):
assert parse_json('notafile') is False
def test_parse_invalid_json(self):
assert parse_json('invalid.json') is False
def test_parse(self):
graph = parse_json('valid.json')
assert graph is not False
nodes = graph.get_nodes()
#for node in nodes:
assert nodes[0] == "SCL"
assert nodes[1] == "LIM"
edges = graph.get_edges()
assert edges[0].get_source() == "SCL"
assert edges[1].get_source() == "LIM"
#def test_url(self):
# url = "http://www.gcmap.com/map?P=SCL-LIM%0D%0ALIM-SCL%0D%0A"
# assert url == generate_map_url(self.graph)
def test_list(self):
temp = sys.stdout
sys.stdout = open('out/list_test.txt', 'w')
list_locations(self.graph)
sys.stdout.close()
sys.stdout = temp
assert filecmp.cmp('out/list_test.txt', 'expected/list_expected.txt') is True
def test_lookup(self):
assert "Code: SCL" in lookup(self.graph, "SCL")
assert "Population: 6000000" in lookup(self.graph, "SCL")
def test_invalid_lookup(self):
assert "not found" in lookup(self.graph, "NOTVALID")
def test_longest(self):
temp = sys.stdout
sys.stdout = open('out/longest_test.txt', 'w')
print(print_longest(self.graph))
sys.stdout.close()
sys.stdout = temp
assert filecmp.cmp('out/longest_test.txt', 'expected/longest_expected.txt') is True
def test_shortest(self):
temp = sys.stdout
sys.stdout = open('out/shortest_test.txt', 'w')
print(print_shortest(self.graph))
sys.stdout.close()
sys.stdout = temp
assert filecmp.cmp('out/shortest_test.txt', 'expected/shortest_expected.txt') is True
def test_other_prints(self):
temp = sys.stdout
sys.stdout = open('out/other_test.txt', 'w')
print(print_average(self.graph))
print(print_smallest_city(self.graph))
print(print_largest_city(self.graph))
print(print_average_city(self.graph))
sys.stdout.close()
sys.stdout = temp
assert filecmp.cmp('out/other_test.txt', 'expected/other_expected.txt') is True
def test_hubs(self):
temp = sys.stdout
sys.stdout = open('out/hubs_test.txt', 'w')
print(print_hubs(self.graph))
sys.stdout.close()
sys.stdout = temp
assert filecmp.cmp('out/hubs_test.txt', 'expected/hubs_expected.txt') is True
def test_continents(self):
temp = sys.stdout
sys.stdout = open('out/continents_test.txt', 'w')
print(print_continents(self.graph))
sys.stdout.close()
sys.stdout = temp
assert filecmp.cmp('out/continents_test.txt', 'expected/continents_expected.txt') is True
if __name__ == '__main__':
unittest.main()