def test_no_route(self): """ Test no route has been founded """ data = { u"cities": {u"Madrid": [0, 0], u"Barcelona": [10, 10], u"Albacete": [3, 4]}, u"links": [[u"Madrid", u"Barcelona"]], } a_star = Cities_AStarSolver(CityMap.load(data)) self.assertEqual(a_star.route(from_city="Barcelona", target_city="Albacete"), (None, 0))
def test_save(self): ''' Test check that load and save processes don't modify input/output format ''' import tempfile, os, json citymap = CityMap.loadFromString(self.json_data) tmpfile = tempfile.mkstemp() citymap.save(tmpfile[1]) self.assertEqual(str(self.data), str(json.load(open(tmpfile[1])))) os.remove(tmpfile[1])
def test_exceptions(self): """ Test astar own exceptions are launched """ data = { u"cities": {u"Madrid": [0, 0], u"Barcelona": [10, 10], u"Albacete": [3, 4]}, u"links": [[u"Madrid", u"Barcelona"]], } a_star = Cities_AStarSolver(CityMap.load(data)) self.assertRaises(FromCityNotFound, a_star.route, from_city=None, target_city=None) self.assertRaises(GoalCityNotFound, a_star.route, from_city="Madrid", target_city=None)
def test_solve_2(self): """ Simple test solving another easy city map """ data = { u"cities": {u"Madrid": [0, 0], u"Barcelona": [10, 10], u"Albacete": [3, 4]}, u"links": [[u"Madrid", u"Barcelona"], [u"Madrid", u"Albacete"]], } short_route = ["Barcelona", "Madrid", "Albacete"] a_star = Cities_AStarSolver(CityMap.load(data)) check_res = (short_route, self.get_cost(a_star, short_route)) self.assertEqual(a_star.route(from_city="Barcelona", target_city="Albacete"), check_res)
def test_solve_3(self): """ Simple test solving a more complex city map """ data = { u"cities": {u"Madrid": [0, 0], u"Guadalajara": [1, 2], u"Albacete": [-1, -3], u"Murcia": [-1, -5]}, u"links": [ ["Madrid", "Guadalajara"], ["Madrid", "Albacete"], ["Guadalajara", "Murcia"], ["Albacete", "Murcia"], ["Guadalajara", "Albacete"], ], } short_route = ["Madrid", "Albacete", "Murcia"] a_star = Cities_AStarSolver(CityMap.load(data)) check_res = (short_route, self.get_cost(a_star, short_route)) self.assertEqual(a_star.route(from_city="Madrid", target_city="Murcia"), check_res)
def test_load_filename(self): ''' Test loading data from filename ''' citymap = CityMap.loadFromFile(self.input_filename) self.check_load_data(citymap)
def test_load_string(self): ''' Test loading data from string format. ''' citymap = CityMap.loadFromString(self.json_data) self.check_load_data(citymap)
def test_load_data(self): ''' Test input data ''' citymap = CityMap.load(self.data) self.check_load_data(citymap)
def test_load_instance(self): ''' Testing right construction with a valid input format ''' self.assertIsInstance(CityMap.load({'cities': {'Madrid': [0,0], 'Barcelona': [10,10.0]}, 'links': [['Madrid','Barcelona']]}), CityMap)