def test_parsing_routes_with_bad_request( caplog, bad_request_google_directions_api_response): with caplog.at_level(logging.INFO): data = google_directions.parse_routes( bad_request_google_directions_api_response, 'ahmyHzvYkCvCuCdDcBrB') assert_semantically_equal(data, {}) assert_logging_warning_caught_with_message_containing( caplog, 'Request was not successful.')
def test_parsing_routes_with_a_good_response(google_directions_api_response, generated_request): data = google_directions.parse_routes(google_directions_api_response, generated_request['path_polyline']) assert_semantically_equal( data, { 'google_speed': 3.7183098591549295, 'google_polyline': 'ahmyHzvYkCvCuCdDcBrB' })
def test_parsing_routes_with_multiple_routes_selects_the_one_closest(mocker, google_directions_api_response, generated_request): mocker.patch.object(Response, 'json', return_value={'routes': [1, 2]}) mocker.patch.object(google_directions, 'parse_route', side_effect=[ {'google_speed': 2, 'google_polyline': 'ahmyHzvYeDz@m@bIqDp@'}, {'google_speed': 3.7183098591549295, 'google_polyline': 'ahmyHzvYkCvCuCdDcBrB'}]) data = google_directions.parse_routes(google_directions_api_response, generated_request['path_polyline']) assert_semantically_equal(data, {'google_speed': 3.7183098591549295, 'google_polyline': 'ahmyHzvYkCvCuCdDcBrB', 'polyline_proximity': 1.306345084680333})
def test_parsing_routes_with_a_bad_response( caplog, request_denied_google_directions_api_response, generated_request): with caplog.at_level(logging.INFO): data = google_directions.parse_routes( request_denied_google_directions_api_response, generated_request['path_polyline']) assert_semantically_equal(data, {}) assert_logging_warning_caught_with_message_containing( caplog, 'Error message: The provided API key is invalid.')
def test_parsing_routes_with_multiple_legs_response( google_directions_api_response_multiple_legs, generated_request): data = google_directions.parse_routes( google_directions_api_response_multiple_legs, generated_request['path_polyline']) assert_semantically_equal( data, { 'google_speed': 2.3636363636363638, 'google_polyline': 'ekmyH~nYbBzFblahblah' })
def test_parsing_routes_with_a_good_response(google_directions_api_response, generated_request): legs = google_directions_api_response.json()['routes'][0]['legs'] expected_speed = sum([leg['distance']['value'] for leg in legs]) / sum( [leg['duration_in_traffic']['value'] for leg in legs]) data = google_directions.parse_routes(google_directions_api_response, generated_request['path_polyline']) assert_semantically_equal(data, { 'google_speed': expected_speed, 'google_polyline': 'ahmyHzvYkCvCuCdDcBrB' })
def test_parsing_routes_with_multiple_legs_response( google_directions_api_response_multiple_legs, generated_request): legs = google_directions_api_response_multiple_legs.json( )['routes'][0]['legs'] expected_speed = sum([leg['distance']['value'] for leg in legs]) / sum( [leg['duration_in_traffic']['value'] for leg in legs]) data = google_directions.parse_routes( google_directions_api_response_multiple_legs, generated_request['path_polyline']) assert_semantically_equal(data, { 'google_speed': expected_speed, 'google_polyline': 'ekmyH~nYbBzFblahblah' })
def test_parsing_routes_with_a_response_without_traffic_info( google_directions_api_response_without_traffic_info, generated_request, caplog): legs = google_directions_api_response_without_traffic_info.json( )['routes'][0]['legs'] expected_speed = sum([leg['distance']['value'] for leg in legs]) / sum( [leg['duration']['value'] for leg in legs]) data = google_directions.parse_routes( google_directions_api_response_without_traffic_info, generated_request['path_polyline']) assert_semantically_equal(data, { 'google_speed': expected_speed, 'google_polyline': 'ahmyHzvYkCvCuCdDcBrB' }) assert_logging_warning_caught_with_message_containing( caplog, 'duration_in_traffic was not found')