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 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 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 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) 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 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( instance, '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 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 __init__(self, instance, service_url, timeout=10, api_key=None, **kwargs): Valhalla.__init__(self, instance, service_url, timeout, api_key, **kwargs)
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 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 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_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 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 __init__(self, instance, service_url, modes=[], id='valhalla', timeout=10, api_key=None, **kwargs): Valhalla.__init__(self, instance, service_url, modes, id, timeout, api_key, **kwargs)
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 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 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 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 get_response_with_0_duration_park_test(): """even if the mode_park_cost is 0, if it's not None we got a park section""" 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, StreetNetworkPathType.BEGINNING_FALLBACK, mode_park_cost=0, ) 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) == 2 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].duration == 6 assert response.journeys[0].sections[0].destination == destination assert response.journeys[0].sections[1].type == response_pb2.PARK assert response.journeys[0].sections[1].length == 0 assert response.journeys[0].sections[ 1].duration == 0 # very quick park section assert response.journeys[0].sections[1].origin == destination assert response.journeys[0].sections[1].destination == destination
def get_response_with_leave_parking_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, StreetNetworkPathType.ENDING_FALLBACK, mode_park_cost=5 * 60, ) assert response.status_code == 200 assert response.response_type == response_pb2.ITINERARY_FOUND assert len(response.journeys) == 1 assert response.journeys[0].duration == 6 + 5 * 60 assert len(response.journeys[0].sections) == 2 assert response.journeys[0].sections[0].type == response_pb2.LEAVE_PARKING assert response.journeys[0].sections[0].length == 0 assert response.journeys[0].sections[0].duration == 5 * 60 assert response.journeys[0].sections[0].origin == origin assert response.journeys[0].sections[0].destination == origin assert response.journeys[0].sections[1].type == response_pb2.STREET_NETWORK assert response.journeys[0].sections[1].length == 52 assert response.journeys[0].sections[1].duration == 6 assert response.journeys[0].sections[1].destination == destination
def get_valhalla_mode_valid_mode_test(): 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 format_coord_func_valid_coord_sources_to_targets_test(): pt_object = make_pt_object(type_pb2.ADDRESS, 1.12, 13.15) coord = Valhalla._format_coord(pt_object, 'sources_to_targets') 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 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_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