def direct_path_func_with_valid_response_valhalla_test(): instance = MagicMock() instance.walking_speed = 1.12 valhalla = Valhalla(instance=instance, url='http://bob.com', costing_options={'bib': 'bom'}) resp_json = response_valid() origin = get_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = get_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) with requests_mock.Mocker() as req: url = 'http://bob.com/route?json={"costing_options": {"pedestrian": {"walking_speed": 4.032000000000001}, ' \ '"bib": "bom"}, "locations": [{"lat": 48.572841, "type": "break", "lon": 2.439938}, ' \ '{"lat": 48.57307, "type": "break", "lon": 2.440548}], "costing": "pedestrian", ' \ '"directions_options": {"units": "kilometers"}}&api_key=None' req.get(url, json=resp_json) valhalla_response = valhalla.direct_path( 'walking', origin, destination, str_to_time_stamp('20161010T152000'), True) 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 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_with_fp('walking', origin, destination, fallback_extremity, MOCKED_REQUEST, None) 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_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 format_url_func_with_walking_mode_test(): instance = MagicMock() instance.walking_speed = 1 instance.bike_speed = 2 origin = get_pt_object(type_pb2.ADDRESS, 1.0, 1.0) destination = get_pt_object(type_pb2.ADDRESS, 2.0, 2.0) valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) data = valhalla._make_data("walking", origin, [destination], MOCKED_REQUEST) assert json.loads(data) == json.loads('''{ "costing_options": { "pedestrian": { "walking_speed": 3.6 }, "bib": "bom" }, "locations": [ { "lat": 1, "type": "break", "lon": 1 }, { "lat": 2, "type": "break", "lon": 2 } ], "costing": "pedestrian", "directions_options": { "units": "kilometers" } }''')
def get_speed_func_test(): instance = MagicMock() instance.walking_speed = 1 instance.bike_speed = 2 valhalla = Valhalla(instance=instance, url='http://bob.com') assert valhalla._get_speed('pedestrian') == 3.6 * 1 assert valhalla._get_speed('bicycle') == 3.6 * 2
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 sources_to_targets_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 = { 'sources_to_targets': [[{ 'time': 42 }, { 'time': None }, { 'time': 1337 }]] } with requests_mock.Mocker() as req: req.post('http://bob.com/sources_to_targets', json=response, status_code=200) valhalla_response = valhalla.get_street_network_routing_matrix( instance, [origin], [destination, destination, destination], 'walking', 42, MOCKED_REQUEST, None) 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 call_valhalla_func_with_circuit_breaker_error_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) valhalla.breaker = MagicMock() valhalla.breaker.call = MagicMock(side_effect=pybreaker.CircuitBreakerError()) assert valhalla._call_valhalla(valhalla.service_url) == None
def get_valhalla_mode_valid_mode_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) map_mode = {"walking": "pedestrian", "car": "auto", "bike": "bicycle"} for kraken_mode, valhalla_mode in map_mode.items(): assert valhalla._get_valhalla_mode(kraken_mode) == valhalla_mode
def get_costing_options_func_with_unkown_costing_options_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) assert valhalla._get_costing_options('bib', MOCKED_REQUEST) == { 'bib': 'bom' }
def format_coord_func_invalid_pt_object_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, url='http://bob.com', costing_options={'bib': 'bom'}) with pytest.raises(InvalidArguments) as excinfo: valhalla._format_coord(MagicMock()) assert '400: Bad Request' in str(excinfo.value)
def call_valhalla_func_with_unknown_exception_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) valhalla.breaker = MagicMock() valhalla.breaker.call = MagicMock(side_effect=ValueError()) assert valhalla._call_valhalla(valhalla.service_url) == None
def get_valhalla_mode_invalid_mode_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) with pytest.raises(InvalidArguments) as excinfo: valhalla._get_valhalla_mode('bss') assert '400: Bad Request' == str(excinfo.value) assert 'InvalidArguments' == str(excinfo.typename)
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' in str(excinfo.value) assert 'InvalidArguments' == str(excinfo.typename)
def format_coord_func_invalid_api_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) with pytest.raises(ApiNotFound) as excinfo: valhalla._format_coord(get_pt_object(type_pb2.ADDRESS, 1.12, 13.15), 'aaa') assert '404: Not Found' in str(excinfo.value) assert 'ApiNotFound' in str(excinfo.typename)
def direct_path_func_without_response_valhalla_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) valhalla.breaker = MagicMock() valhalla._call_valhalla = MagicMock(return_value=None) valhalla._make_request_arguments = MagicMock(return_value=None) with pytest.raises(TechnicalError) as excinfo: valhalla._direct_path(None, None, None, None, None, None, None, None) assert '500 Internal Server Error' in str(excinfo.value) assert 'TechnicalError' == str(excinfo.typename)
def format_url_func_invalid_mode_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, url='http://bob.com', costing_options={'bib': 'bom'}) with pytest.raises(InvalidArguments) as excinfo: origin = get_pt_object(type_pb2.ADDRESS, 1.0, 1.0) destination = get_pt_object(type_pb2.ADDRESS, 2.0, 2.0) valhalla._format_url("bob", origin, destination) assert '400: Bad Request' == str(excinfo.value) assert 'InvalidArguments' == str(excinfo.typename)
def format_coord_func_valid_coord_one_to_many_test(): instance = MagicMock() pt_object = get_pt_object(type_pb2.ADDRESS, 1.12, 13.15) valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) coord = valhalla._format_coord(pt_object, 'one_to_many') coord_res = { 'lat': pt_object.address.coord.lat, 'lon': pt_object.address.coord.lon } assert len(coord) == 2 for key, value in coord_res.items(): assert coord[key] == value
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_with_fp( instance, 'walking', origin, destination, fallback_extremity, MOCKED_REQUEST, None, None ) 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() valhalla = Valhalla(instance=instance, url='http://bob.com', costing_options={'bib': 'bom'}) valhalla.breaker = MagicMock() response = requests.Response() response.status_code = 400 response.url = valhalla.service_url valhalla._call_valhalla = MagicMock(return_value=response) valhalla._format_url = MagicMock(return_value=valhalla.service_url) nav_resp = valhalla.direct_path(None, None, None, None, None) assert response.status_code == nav_resp.status_code assert nav_resp.error.message == 'Valhalla service unavailable, impossible to query : http://bob.com'
def get_costing_options_func_test(): instance = MagicMock() instance.walking_speed = 1 instance.bike_speed = 2 valhalla = Valhalla(instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}) pedestrian = valhalla._get_costing_options('pedestrian', MOCKED_REQUEST) assert len(pedestrian) == 2 assert 'pedestrian' in pedestrian assert 'walking_speed' in pedestrian['pedestrian'] assert pedestrian['pedestrian']['walking_speed'] == 3.6 * 1 assert 'bib' in pedestrian bicycle = valhalla._get_costing_options('bicycle', MOCKED_REQUEST) assert len(bicycle) == 2 assert 'bicycle' in bicycle assert 'cycling_speed' in bicycle['bicycle'] assert bicycle['bicycle']['cycling_speed'] == 3.6 * 2 assert 'bib' in bicycle
def status_test(): valhalla = Valhalla( instance=None, service_url='http://bob.com', id=u"tata-é$~#@\"*!'`§èû", modes=["walking", "bike", "car"], timeout=23, ) status = valhalla.status() assert len(status) == 5 assert status['id'] == u'tata-é$~#@"*!\'`§èû' assert status['class'] == "Valhalla" assert status['modes'] == ["walking", "bike", "car"] assert status['timeout'] == 23 assert len(status['circuit_breaker']) == 3 assert status['circuit_breaker']['current_state'] == 'closed' assert status['circuit_breaker']['fail_counter'] == 0 assert status['circuit_breaker']['reset_timeout'] == 60
def format_url_func_with_car_mode_test(): instance = MagicMock() instance.walking_speed = 1 instance.bike_speed = 2 origin = get_pt_object(type_pb2.ADDRESS, 1.0, 1.0) destination = get_pt_object(type_pb2.ADDRESS, 2.0, 2.0) valhalla = Valhalla(instance=instance, url='http://bob.com', costing_options={'bib': 'bom'}) valhalla.costing_options = None assert valhalla._format_url("car", origin, destination) == 'http://bob.com/route?json=' \ '{"locations": [{"lat": 1.0, "type": "break", "lon": 1.0}, ' \ '{"lat": 2.0, "type": "break", "lon": 2.0}], "costing": "auto", ' \ '"directions_options": {"units": "kilometers"}}&' \ 'api_key=None'
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_with_fp( instance, 'walking', origin, destination, fallback_extremity, MOCKED_REQUEST, None, None ) assert '500 Internal Server Error' in str(excinfo.value) assert ( str(excinfo.value.data['message']) == 'Valhalla service unavailable, impossible to query : http://bob.com/route' ) assert str(excinfo.typename) == 'TechnicalError'
def get_response_func_with_unknown_exception_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, url='http://bob.com', costing_options={'bib': 'bom'}) resp_json = response_valid() origin = get_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = get_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) response = valhalla._get_response(resp_json, 'walking', origin, destination, str_to_time_stamp('20161010T152000')) 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 get_costing_options_func_with_empty_costing_options_test(): instance = MagicMock() valhalla = Valhalla(instance=instance, service_url='http://bob.com') valhalla.costing_options = None assert valhalla._get_costing_options('bib', MOCKED_REQUEST) == None
def sources_to_targets_valhalla_with_park_cost_test(): """ test with a parking cost, we add a different cost on car and bike and no cost on walking """ instance = MagicMock() instance.walking_speed = 1.12 valhalla = Valhalla( instance=instance, service_url='http://bob.com', costing_options={'bib': 'bom'}, mode_park_cost={ 'bike': 30, # 30s for the bike 'car': 5 * 60 # 5 mn for the car # and no park section for walking }, ) origin = make_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = make_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) response = { 'sources_to_targets': [[{ 'time': 42 }, { 'time': None }, { 'time': 1337 }]] } with requests_mock.Mocker() as req: req.post('http://bob.com/sources_to_targets', json=response, status_code=200) # it changes nothing for walking valhalla_response = valhalla.get_street_network_routing_matrix( instance, [origin], [destination, destination, destination], mode='walking', max_duration=42, request=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 # for bike every reached point have an additional 30s valhalla_response = valhalla.get_street_network_routing_matrix( instance, [origin], [destination, destination, destination], mode='bike', max_duration=42, request=MOCKED_REQUEST, ) assert valhalla_response.rows[0].routing_response[ 0].duration == 42 + 30 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 + 30 assert valhalla_response.rows[0].routing_response[ 2].routing_status == response_pb2.reached # for car every reached point have an additional 5mn valhalla_response = valhalla.get_street_network_routing_matrix( instance, [origin], [destination, destination, destination], mode='car', max_duration=42, request=MOCKED_REQUEST, ) assert valhalla_response.rows[0].routing_response[ 0].duration == 42 + 5 * 60 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 + 5 * 60 assert valhalla_response.rows[0].routing_response[ 2].routing_status == response_pb2.reached
def decode_func_test(): valhalla = Valhalla(instance=None, url='http://bob.com') assert valhalla._decode('qyss{Aco|sCF?kBkHeJw[') == [[2.439938, 48.572841], [2.439938, 48.572837], [2.440088, 48.572891], [2.440548, 48.57307]]