def test_get_best_path_from_location_invalid(): filepath = "files/input-file-test.csv" to_node, from_node = 'GRU', 'non_exist' route_domain = RouteDomain(filepath) with pytest.raises(AttributeError): best_path_response, total_cost_response = route_domain.best_path( to_node, from_node)
def exec_shell(filepath): ''' ''' try: route_domain = RouteDomain(filepath) except KeyError as e: return "File not supported! Use only files .txt or .csv." except FileNotFoundError as e: return "File not found." try: print("Please use - to separate the locations. Ex.: ONE - ANOTHER") from_node, to_node = list( map(str, input("please enter the route:").split('-'))) except ValueError as e: return "Invalid input." from_node = from_node.replace(" ", "").upper() to_node = to_node.replace(" ", "").upper() try: best_path, cost = route_domain.best_path(from_node, to_node) except ValueError as e: return "The nodes are not connected." except AttributeError as e: return "Please, send two valid locations to verify: origin and destination." response = f"{best_path} > ${cost}" return response
def test_get_best_path(): filepath = "files/input-file-test.csv" to_node, from_node = 'GRU', 'CDG' route_domain = RouteDomain(filepath) best_path_response, total_cost_response = route_domain.best_path( to_node, from_node) assert best_path_response == "GRU - BRC - SCL - ORL - CDG" assert total_cost_response == 40
def __init__(self): ''' For development purposes, this View always instantiates RotaDomain with the .csv file indicated in the challenge statement. If it is desired to use a .txt file instead, it will be necessary to change the file name in the project settings (rota_viagem / settings.py) ''' self.domain = RouteDomain(settings.FILE_EXAMPLE)
def test_insert_invalid_new_route(): filepath = "files/input-file-test.csv" routes = [{ "origin": 'NewOrigin', "end": 'NewDestination', "cost": "invalid" }] route_domain = RouteDomain(filepath) with pytest.raises(ValueError): route_domain.insert(routes)
def test_get_best_path_from_location_invalid(): filepath = "files/input-file-test.csv" routes = [{"origin": 'SCL', "end": 'ALM', "cost": 5}] to_node, from_node = 'ALM', 'CDG' route_domain = RouteDomain(filepath) updated = route_domain.insert(routes) with pytest.raises(ValueError): best_path_response, total_cost_response = route_domain.best_path( to_node, from_node)
def test_insert_new_route(): filepath = "files/input-file-test.csv" routes = [ { "origin": 'NewOrigin', "end": 'OtherDestination', "cost": 15 }, { "origin": 'AnotherOrigin', "end": 'OtherDestination', "cost": 11 }, ] route_domain = RouteDomain(filepath) assert route_domain.insert(routes) == True
class RouteView(APIView): """ This is the Route view. It is responsible for handling de API requests, and to redirect to the domain class responsible. """ def __init__(self): ''' For development purposes, this View always instantiates RotaDomain with the .csv file indicated in the challenge statement. If it is desired to use a .txt file instead, it will be necessary to change the file name in the project settings (rota_viagem / settings.py) ''' self.domain = RouteDomain(settings.FILE_EXAMPLE) def get(self, request): ''' The Get method queries the best route to reach a destination from a point, the application uses the values informed in the consumption .txt or .csv file and returns the result found. This method will always receive two queryparams: to and from. These params will define the origin and destination for the best path query. ''' try: from_route = self.request.query_params.get('from').upper() to_route = self.request.query_params.get('to').upper() route, total_cost = self.domain.best_path(from_route, to_route) response = BestPathSerializer({ 'route': route, 'total_cost': total_cost }) return Response(response.data, status=status.HTTP_200_OK) except ValueError as e: return Response({"message": 'The nodes are not connected.'}, status=status.HTTP_400_BAD_REQUEST) except AttributeError as e: print(e) return Response( { "message": "Please, send two valid locations to verify: origin and destination." }, status=status.HTTP_400_BAD_REQUEST) def post(self, request): ''' The post method allows one more connection to be added to the .txt or .csv file used. It is an analogy with the persistence functionality in a database. Requests must be in the following format: { "origin" : "GRU", "end" : "DSL", "cost" : 49 } ''' serializer = RouteInsertSerializer(data=request.data) if serializer.is_valid(): try: sucess = self.domain.insert( serializer.validated_data['routes']) return Response(status=status.HTTP_201_CREATED) except ValueError as e: return Response({"message": "Invalid route attributes."}, status=status.HTTP_400_BAD_REQUEST) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_create_route_domain_object(): filepath = "files/input-file-test.csv" route_domain = RouteDomain(filepath) assert route_domain.file == "files/input-file-test.csv"
def test_create_route_domain_with_inexistent_file(): filepath = "files/invalid-input-file-test.pdf" with pytest.raises(KeyError): route_domain = RouteDomain(filepath)
def test_create_route_domain_with_inexistent_file(): filepath = "files/invalid-input-file-test.csv" with pytest.raises(FileNotFoundError): route_domain = RouteDomain(filepath)