def format_url_func_with_different_ptobject_test(): instance = MagicMock() instance.walking_speed = 1 instance.bike_speed = 2 valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) valhalla.costing_options = None for ptObject_type in (type_pb2.STOP_POINT, type_pb2.STOP_AREA, type_pb2.ADDRESS, type_pb2.ADMINISTRATIVE_REGION, type_pb2.POI): origin = make_pt_object(ptObject_type, 1.0, 1.0) destination = make_pt_object(ptObject_type, 2.0, 2.0) data = valhalla._make_request_arguments("car", origin, [destination], MOCKED_REQUEST) assert json.loads(data) == json.loads(''' { "locations": [ { "lat": 1, "type": "break", "lon": 1 }, { "lat": 2, "type": "break", "lon": 2 } ], "costing": "auto", "directions_options": { "units": "kilometers" } }''')
def direct_path_func_with_valid_response_valhalla_test(): instance = MagicMock() instance.walking_speed = 1.12 valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) resp_json = response_valid() origin = make_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = make_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) fallback_extremity = PeriodExtremity(str_to_time_stamp('20161010T152000'), True) with requests_mock.Mocker() as req: req.post('http://bob.com/route', json=resp_json) valhalla_response = valhalla.direct_path('walking', origin, destination, fallback_extremity, MOCKED_REQUEST) assert valhalla_response.status_code == 200 assert valhalla_response.response_type == response_pb2.ITINERARY_FOUND assert len(valhalla_response.journeys) == 1 assert valhalla_response.journeys[0].duration == 6 assert len(valhalla_response.journeys[0].sections) == 1 assert valhalla_response.journeys[0].sections[0].type == response_pb2.STREET_NETWORK assert valhalla_response.journeys[0].sections[0].type == response_pb2.STREET_NETWORK assert valhalla_response.journeys[0].sections[0].length == 52 assert valhalla_response.journeys[0].sections[0].destination == destination
def format_url_func_with_car_mode_test(): instance = MagicMock() instance.walking_speed = 1 instance.bike_speed = 2 origin = make_pt_object(type_pb2.ADDRESS, 1.0, 1.0) destination = make_pt_object(type_pb2.ADDRESS, 2.0, 2.0) valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) valhalla.costing_options = None data = valhalla._make_request_arguments("car", origin, [destination], MOCKED_REQUEST) assert json.loads(data) == json.loads(''' { "locations": [ { "lat": 1, "type": "break", "lon": 1 }, { "lat": 2, "type": "break", "lon": 2 } ], "costing": "auto", "directions_options": { "units": "kilometers" } }''')
def make_data_test(): origins = [make_pt_object(type_pb2.ADDRESS, lon=2, lat=48.2, uri='refStart1')] destinations = [make_pt_object(type_pb2.ADDRESS, lon=3, lat=48.3, uri='refEnd1'), make_pt_object(type_pb2.ADDRESS, lon=4, lat=48.4, uri='refEnd2')] data = Geovelo._make_request_arguments_isochrone(origins, destinations) assert json.loads(json.dumps(data)) == json.loads('''{ "starts":[[48.2,2, "refStart1"]], "ends":[[48.3,3, "refEnd1"], [48.4,4, "refEnd2"]] }''')
def format_url_func_invalid_mode_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) with pytest.raises(InvalidArguments) as excinfo: origin = make_pt_object(type_pb2.ADDRESS, 1.0, 1.0) destination = make_pt_object(type_pb2.ADDRESS, 2.0, 2.0) valhalla._make_request_arguments("bob", origin, [destination], MOCKED_REQUEST) assert '400: Bad Request' == str(excinfo.value) assert 'InvalidArguments' == str(excinfo.typename)
def format_coord_func_valid_coord_test(): pt_object = make_pt_object(type_pb2.ADDRESS, 1.12, 13.15) coord = Valhalla._format_coord(pt_object) coord_res = {'lat': pt_object.address.coord.lat, 'type': 'break', 'lon': pt_object.address.coord.lon} assert len(coord) == 3 for key, value in coord_res.items(): assert coord[key] == value
def get_response_func_with_unknown_exception_test(): resp_json = response_valid() origin = make_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = make_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) fallback_extremity = PeriodExtremity(str_to_time_stamp('20161010T152000'), True) response = Valhalla._get_response(resp_json, 'walking', origin, destination, fallback_extremity) assert response.status_code == 200 assert response.response_type == response_pb2.ITINERARY_FOUND assert len(response.journeys) == 1 assert response.journeys[0].duration == 6 assert len(response.journeys[0].sections) == 1 assert response.journeys[0].sections[0].type == response_pb2.STREET_NETWORK assert response.journeys[0].sections[0].type == response_pb2.STREET_NETWORK assert response.journeys[0].sections[0].length == 52 assert response.journeys[0].sections[0].destination == destination
def direct_path_func_with_no_response_valhalla_test(): instance = MagicMock() instance.walking_speed = 1.12 valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) origin = make_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = make_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) fallback_extremity = PeriodExtremity(str_to_time_stamp('20161010T152000'), True) with requests_mock.Mocker() as req: req.post('http://bob.com/route', json={'error_code': 442}, status_code=400) valhalla_response = valhalla.direct_path('walking', origin, destination, fallback_extremity, MOCKED_REQUEST) assert valhalla_response.status_code == 200 assert valhalla_response.response_type == response_pb2.NO_SOLUTION assert len(valhalla_response.journeys) == 0
def direct_path_func_with_status_code_400_response_valhalla_test(): instance = MagicMock() instance.walking_speed = 1.12 valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) origin = make_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = make_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) fallback_extremity = PeriodExtremity(str_to_time_stamp('20161010T152000'), True) with requests_mock.Mocker() as req: with pytest.raises(TechnicalError) as excinfo: req.post('http://bob.com/route', json={'error_code': 42}, status_code=400) valhalla.direct_path('walking', origin, destination, fallback_extremity, MOCKED_REQUEST) assert str(excinfo.value) == '500: Internal Server Error' assert str(excinfo.value.data['message']) == 'Valhalla service unavailable, impossible to query : http://bob.com/route' assert str(excinfo.typename) == 'TechnicalError'
def one_to_many_valhalla_test(): instance = MagicMock() instance.walking_speed = 1.12 valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) origin = make_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = make_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) response = { 'one_to_many': [ [ { 'time': 0 }, { 'time': 42 }, { 'time': None }, { 'time': 1337 }, ] ] } with requests_mock.Mocker() as req: req.post('http://bob.com/one_to_many', json=response, status_code=200) valhalla_response = valhalla.get_street_network_routing_matrix( [origin], [destination, destination, destination], 'walking', 42, MOCKED_REQUEST) assert valhalla_response.rows[0].routing_response[0].duration == 42 assert valhalla_response.rows[0].routing_response[0].routing_status == response_pb2.reached assert valhalla_response.rows[0].routing_response[1].duration == -1 assert valhalla_response.rows[0].routing_response[1].routing_status == response_pb2.unknown assert valhalla_response.rows[0].routing_response[2].duration == 1337 assert valhalla_response.rows[0].routing_response[2].routing_status == response_pb2.reached
def isochrone_geovelo_test(): instance = MagicMock() geovelo = Geovelo(instance=instance, service_url='http://bob.com') resp_json = isochrone_response_valid() origins = [make_pt_object(type_pb2.ADDRESS, lon=2, lat=48.2, uri='refStart1')] destinations = [make_pt_object(type_pb2.ADDRESS, lon=3, lat=48.3, uri='refEnd1'), make_pt_object(type_pb2.ADDRESS, lon=4, lat=48.4, uri='refEnd2')] with requests_mock.Mocker() as req: req.post('http://bob.com/api/v2/routes_m2m', json=resp_json, status_code=200) geovelo_response = geovelo.get_street_network_routing_matrix( origins, destinations, 'bike', 13371337, None) assert geovelo_response.rows[0].routing_response[0].duration == 1051 assert geovelo_response.rows[0].routing_response[0].routing_status == response_pb2.reached assert geovelo_response.rows[0].routing_response[1].duration == 1656 assert geovelo_response.rows[0].routing_response[1].routing_status == response_pb2.reached
def direct_path_geovelo_test(): instance = MagicMock() geovelo = Geovelo(instance=instance, service_url='http://bob.com') resp_json = direct_path_response_valid() origin = make_pt_object(type_pb2.ADDRESS, lon=2, lat=48.2, uri='refStart1') destination = make_pt_object(type_pb2.ADDRESS, lon=3, lat=48.3, uri='refEnd1') fallback_extremity = PeriodExtremity(str_to_time_stamp('20161010T152000'), False) with requests_mock.Mocker() as req: req.post('http://bob.com/api/v2/computedroutes?instructions=true&elevations=false&geometry=true' '&single_result=true&bike_stations=false&objects_as_ids=true&', json=resp_json) geovelo_resp = geovelo.direct_path('bike', origin, destination, fallback_extremity, None) assert geovelo_resp.status_code == 200 assert geovelo_resp.response_type == response_pb2.ITINERARY_FOUND assert len(geovelo_resp.journeys) == 1 assert geovelo_resp.journeys[0].duration == 3155 # 52min35s assert len(geovelo_resp.journeys[0].sections) == 1 assert geovelo_resp.journeys[0].arrival_date_time == str_to_time_stamp('20161010T152000') assert geovelo_resp.journeys[0].departure_date_time == str_to_time_stamp('20161010T142725') assert geovelo_resp.journeys[0].sections[0].type == response_pb2.STREET_NETWORK assert geovelo_resp.journeys[0].sections[0].type == response_pb2.STREET_NETWORK assert geovelo_resp.journeys[0].sections[0].duration == 3155 assert geovelo_resp.journeys[0].sections[0].length == 11393 assert geovelo_resp.journeys[0].sections[0].street_network.coordinates[2].lon == 2.314258 assert geovelo_resp.journeys[0].sections[0].street_network.coordinates[2].lat == 48.887428 assert geovelo_resp.journeys[0].sections[0].origin == origin assert geovelo_resp.journeys[0].sections[0].destination == destination assert geovelo_resp.journeys[0].sections[0].street_network.path_items[1].name == "Rue Jouffroy d'Abbans" assert geovelo_resp.journeys[0].sections[0].street_network.path_items[1].direction == 0 assert geovelo_resp.journeys[0].sections[0].street_network.path_items[1].length == 40 assert geovelo_resp.journeys[0].sections[0].street_network.path_items[1].duration == 144
def pt_object_summary_test(): summary = Geovelo._pt_object_summary_isochrone(make_pt_object(type_pb2.ADDRESS, lon=1.12, lat=13.15, uri='toto')) assert summary == [13.15, 1.12, 'toto']
def format_coord_func_invalid_api_test(): with pytest.raises(ApiNotFound) as excinfo: Valhalla._format_coord(make_pt_object(type_pb2.ADDRESS, 1.12, 13.15), 'aaa') assert '404: Not Found' in str(excinfo.value) assert 'ApiNotFound' in str(excinfo.typename)