def test_best_route(self): route = FindRoute('GRU', 'CDG', FILE) route.dijkstra() best_route = route.best_route() result_string_expected = ( f"best route: {route.source} - {' - '.join(route.path)}" f" > {route.shortest_distance[route.destination]}") self.assertEqual(best_route, result_string_expected)
def test_dijkstra_calculate_route(self): route = FindRoute('GRU', 'CDG', FILE) route.dijkstra() self.assertEqual(route.unseen_nodes, {}) self.assertEqual(route.shortest_distance, { 'GRU': 0, 'BRC': 10, 'SCL': 15, 'CDG': 40, 'ORL': 35 }) self.assertEqual(route.predecessor, { 'BRC': 'GRU', 'CDG': 'ORL', 'SCL': 'BRC', 'ORL': 'SCL' })
def test_best_route_logs(self): with self.assertLogs('backend', level='INFO') as log: route = FindRoute('GRU', 'CDG', FILE) route.dijkstra() route.best_route() self.assertIn('best route: GRU - BRC - SCL - ORL - CDG > 40', log.output[0])
def test_get_route_brc_cdg(self): route = FindRoute('BRC', 'CDG', FILE) route.dijkstra() route.best_route() self.assertEqual(route.result, { 'route': 'BRC - SCL - ORL - CDG', 'price': 30 })
def test_get_route_gru_cdg(self): route = FindRoute('GRU', 'CDG', FILE) route.dijkstra() route.best_route() self.assertEqual(route.result, { 'route': 'GRU - BRC - SCL - ORL - CDG', 'price': 40 })
def test_get_route_brc_cdg_with_lowercase(self): route = FindRoute('brc', 'cdg', FILE) route.dijkstra() route.best_route() self.assertEqual(route.result, { 'route': 'BRC - SCL - ORL - CDG', 'price': 30 })
class Views: def __init__(self, route_data=None, file_data=None): self.data = file_data or FILE self.route = FindRoute(file_data=self.data) self.route_data = route_data def routes(self): from travels.helpers import to_json routes = self.route.get_rows_from_file() obj = to_json(routes) return obj def new_route(self): source = self.route_data['body']['source'].upper() destination = self.route_data['body']['destination'].upper() price = self.route_data['body']['price'] if write_file(self.data, source, destination, price): return { 'source': source, 'destination': destination, 'price': price } raise ErrorWriteFile('Error saving data') def get_route(self): route = FindRoute( self.route_data['query_params']['source'].upper(), self.route_data['query_params']['destination'].upper(), self.data ) route.dijkstra() route.best_route() result = { 'route': f"{route.source} - {' - '.join(route.path)}", 'price': route.price } return result
def get_route(self): route = FindRoute( self.route_data['query_params']['source'].upper(), self.route_data['query_params']['destination'].upper(), self.data ) route.dijkstra() route.best_route() result = { 'route': f"{route.source} - {' - '.join(route.path)}", 'price': route.price } return result
def __init__(self, route_data=None, file_data=None): self.data = file_data or FILE self.route = FindRoute(file_data=self.data) self.route_data = route_data
def test_get_route_without_file(self): route = FindRoute('brc', 'cdg') route.dijkstra() route.best_route() self.assertEqual(route.result, {})
def test_get_route_without_destination(self): route = FindRoute('brc', '', FILE) route.dijkstra() route.best_route() self.assertEqual(route.result, {})
def test_get_route_without_source(self): route = FindRoute('', 'cdg', FILE) route.dijkstra() route.best_route() self.assertEqual(route.result, {})
def test_graph_from_file_with_no_exits_routes_graph_should_be_equal(self): route = FindRoute('ABC', 'DEF', FILE) graph = route.graph_from_file() self.assertEqual(graph, self.graph)
def test_graph_from_file(self): route = FindRoute(file_data=FILE) graph = route.graph_from_file() self.assertEqual(graph, self.graph)
def test_get_rows_from_file_without_file(self): route = FindRoute() rows = route.get_rows_from_file() self.assertEqual(rows, None)
def test_get_rows_from_file(self): route = FindRoute(file_data=FILE) rows = route.get_rows_from_file() self.assertEqual(len(rows), 7)