def different_duration_test(): scenario = Scenario() response = response_pb2.Response() journey1 = response.journeys.add() journey1.arrival_date_time = str_to_time_stamp("20140422T0800") journey1.duration = 5 * 60 journey1.nb_transfers = 0 journey1.sections.add() journey1.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey1.sections[0].duration = 5 * 60 journey2 = response.journeys.add() journey2.arrival_date_time = str_to_time_stamp("20140422T0800") journey2.duration = 3 * 60 journey2.nb_transfers = 0 journey2.sections.add() journey2.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey2.sections[0].duration = 3 * 60 scenario.sort_journeys(response, 'arrival_time') assert response.journeys[0].arrival_date_time == str_to_time_stamp( "20140422T0800") assert response.journeys[1].arrival_date_time == str_to_time_stamp( "20140422T0800") assert response.journeys[0].duration == 3 * 60 assert response.journeys[1].duration == 5 * 60
def here_basic_routing_test(valid_here_routing_response): origin = make_pt_object(type_pb2.POI, 2.439938, 48.572841) destination = make_pt_object(type_pb2.STOP_AREA, 2.440548, 48.57307) fallback_extremity = PeriodExtremity(str_to_time_stamp('20161010T152000'), True) response = Here._read_response( response=valid_here_routing_response, mode='walking', origin=origin, destination=destination, fallback_extremity=fallback_extremity, request={'datetime': str_to_time_stamp('20170621T174600')}, ) assert response.status_code == 200 assert response.response_type == response_pb2.ITINERARY_FOUND assert len(response.journeys) == 1 assert response.journeys[0].duration == 588 assert len(response.journeys[0].sections) == 1 section = response.journeys[0].sections[0] assert section.type == response_pb2.STREET_NETWORK assert section.length == 4012 assert section.duration == 588 assert section.destination == destination assert section.origin == origin assert section.begin_date_time == str_to_time_stamp('20161010T152000') assert section.end_date_time == section.begin_date_time + section.duration
def tranfers_cri_test(): journeys = [] dates = [ "20131107T100000", "20131107T150000", "20131107T050000", "20131107T100000", "20131107T150000", "20131107T050000", ] transfers = [4, 3, 8, 1, 1, 2] for i in range(6): journey = response_pb2.Journey() journey.nb_transfers = transfers[i] journey.arrival_date_time = str_to_time_stamp(dates[i]) journeys.append(journey) best = qualifier.min_from_criteria( journeys, [qualifier.transfers_crit, qualifier.arrival_crit]) # the transfert criterion is first, and then if 2 journeys have # the same nb_transfers, we compare the dates assert best.nb_transfers == 1 assert best.arrival_date_time == str_to_time_stamp("20131107T100000")
def direct_path_geovelo_zero_test(): instance = MagicMock() geovelo = Geovelo(instance=instance, service_url='http://bob.com') resp_json = direct_path_response_zero() origin = make_pt_object(type_pb2.ADDRESS, lon=2, lat=48, uri='refStart1') destination = make_pt_object(type_pb2.ADDRESS, lon=2, lat=48, 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=true&geometry=true' '&single_result=true&bike_stations=false&objects_as_ids=true&', json=resp_json, ) geovelo_resp = geovelo.direct_path_with_fp( instance, 'bike', origin, destination, fallback_extremity, MOCKED_REQUEST, None, 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 == 0 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('20161010T152000') 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 == 0 assert geovelo_resp.journeys[0].sections[0].length == 0 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[0].name == "voie pietonne" assert geovelo_resp.journeys[0].sections[0].street_network.path_items[0].direction == 0 assert geovelo_resp.journeys[0].sections[0].street_network.path_items[0].length == 0 assert geovelo_resp.journeys[0].sections[0].street_network.path_items[0].duration == 0
def different_nb_transfers_test(): response = response_pb2.Response() journey1 = response.journeys.add() journey1.arrival_date_time = str_to_time_stamp("20140422T0800") journey1.duration = 25 * 60 journey1.nb_transfers = 1 journey1.sections.add() journey1.sections.add() journey1.sections.add() journey1.sections.add() journey1.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey1.sections[0].duration = 5 * 60 journey1.sections[1].type = response_pb2.TRANSFER journey1.sections[1].duration = 3 * 60 journey1.sections[2].type = response_pb2.WAITING journey1.sections[2].duration = 2 * 60 journey1.sections[3].type = response_pb2.PUBLIC_TRANSPORT journey1.sections[3].duration = 15 * 60 journey2 = response.journeys.add() journey2.arrival_date_time = str_to_time_stamp("20140422T0800") journey2.duration = 25 * 60 journey2.nb_transfers = 0 journey2.sections.add() journey2.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey2.sections[0].duration = 25 * 60 sort_journeys(response, 'arrival_time', True) assert response.journeys[0].arrival_date_time == str_to_time_stamp("20140422T0800") assert response.journeys[1].arrival_date_time == str_to_time_stamp("20140422T0800") assert response.journeys[0].duration == 25 * 60 assert response.journeys[1].duration == 25 * 60 assert response.journeys[0].nb_transfers == 0 assert response.journeys[1].nb_transfers == 1
def sort_destineo_date_test(): """ the bss journey must be first since it his on first day """ response = response_pb2.Response() journey_tc3 = response.journeys.add() journey_tc3.type = "rapid" journey_tc3.departure_date_time = str_to_time_stamp('20141103T123000') section = journey_tc3.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking section = journey_tc3.sections.add() section.type = response_pb2.PUBLIC_TRANSPORT section = journey_tc3.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking journey_tc1 = response.journeys.add() journey_tc1.type = "rapid" journey_tc1.departure_date_time = str_to_time_stamp('20141103T110000') section = journey_tc1.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking section = journey_tc1.sections.add() section.type = response_pb2.PUBLIC_TRANSPORT section = journey_tc1.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking journey_bss_and_tc = response.journeys.add() journey_bss_and_tc.departure_date_time = str_to_time_stamp( '20141102T120000') journey_bss_and_tc.type = "rapid" section = journey_bss_and_tc.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking section = journey_bss_and_tc.sections.add() section.type = response_pb2.BSS_RENT section = journey_bss_and_tc.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Bike section = journey_bss_and_tc.sections.add() section.type = response_pb2.BSS_PUT_BACK section = journey_bss_and_tc.sections.add() section.type = response_pb2.PUBLIC_TRANSPORT section = journey_bss_and_tc.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking scenario = destineo.Scenario() scenario._custom_sort_journeys(response, clockwise=True, timezone='Africa/Abidjan') assert response.journeys[0] == journey_bss_and_tc assert response.journeys[1] == journey_tc1 assert response.journeys[2] == journey_tc3
def sort_destineo_date_timezone_test(): response = response_pb2.Response() journey_tc3 = response.journeys.add() journey_tc3.type = "rapid" journey_tc3.departure_date_time = str_to_time_stamp('20141104T113000') section = journey_tc3.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking section = journey_tc3.sections.add() section.type = response_pb2.PUBLIC_TRANSPORT section = journey_tc3.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking journey_tc1 = response.journeys.add() journey_tc1.type = "rapid" journey_tc1.departure_date_time = str_to_time_stamp('20141104T110000') section = journey_tc1.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking section = journey_tc1.sections.add() section.type = response_pb2.PUBLIC_TRANSPORT section = journey_tc1.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking #at paris it's 20141104T003000 so it's the same day journey_bss_and_tc = response.journeys.add() journey_bss_and_tc.departure_date_time = str_to_time_stamp( '20141103T233000') journey_bss_and_tc.type = "rapid" section = journey_bss_and_tc.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking section = journey_bss_and_tc.sections.add() section.type = response_pb2.BSS_RENT section = journey_bss_and_tc.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Bike section = journey_bss_and_tc.sections.add() section.type = response_pb2.BSS_PUT_BACK section = journey_bss_and_tc.sections.add() section.type = response_pb2.PUBLIC_TRANSPORT section = journey_bss_and_tc.sections.add() section.type = response_pb2.STREET_NETWORK section.street_network.mode = response_pb2.Walking scenario = destineo.Scenario() scenario._custom_sort_journeys(response, clockwise=True, timezone='Europe/Paris') assert response.journeys[0] == journey_tc1 assert response.journeys[1] == journey_tc3 assert response.journeys[2] == journey_bss_and_tc
def different_duration_non_pt_test(): scenario = Scenario() response = response_pb2.Response() journey1 = response.journeys.add() journey1.arrival_date_time = str_to_time_stamp("20140422T0800") journey1.duration = 25 * 60 journey1.nb_transfers = 1 journey1.sections.add() journey1.sections.add() journey1.sections.add() journey1.sections.add() journey1.sections.add() journey1.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey1.sections[0].duration = 5 * 60 journey1.sections[1].type = response_pb2.TRANSFER journey1.sections[1].duration = 3 * 60 journey1.sections[2].type = response_pb2.WAITING journey1.sections[2].duration = 2 * 60 journey1.sections[3].type = response_pb2.PUBLIC_TRANSPORT journey1.sections[3].duration = 15 * 60 journey1.sections[4].type = response_pb2.STREET_NETWORK journey1.sections[4].duration = 10 * 60 journey2 = response.journeys.add() journey2.arrival_date_time = str_to_time_stamp("20140422T0800") journey2.duration = 25 * 60 journey2.nb_transfers = 1 journey2.sections.add() journey2.sections.add() journey2.sections.add() journey2.sections.add() journey2.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey2.sections[0].duration = 5 * 60 journey2.sections[1].type = response_pb2.TRANSFER journey2.sections[1].duration = 3 * 60 journey2.sections[2].type = response_pb2.WAITING journey2.sections[2].duration = 2 * 60 journey2.sections[3].type = response_pb2.PUBLIC_TRANSPORT journey2.sections[3].duration = 15 * 60 scenario.sort_journeys(response, 'arrival_time') assert response.journeys[0].arrival_date_time == str_to_time_stamp( "20140422T0800") assert response.journeys[1].arrival_date_time == str_to_time_stamp( "20140422T0800") assert response.journeys[0].duration == 25 * 60 assert response.journeys[1].duration == 25 * 60 assert response.journeys[0].nb_transfers == 1 assert response.journeys[1].nb_transfers == 1 #We want to have journey2 in first, this is the one with 4 sections assert len(response.journeys[0].sections) == 4 assert len(response.journeys[1].sections) == 5
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_with_fp('bike', origin, destination, fallback_extremity, None, 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 check_basic_journeys_request(journeys_req): assert len(journeys_req.origin) == 1 assert journeys_req.origin[0].place == "Kisio Digital" assert journeys_req.origin[0].access_duration == 42 assert len(journeys_req.destination) == 1 assert journeys_req.destination[0].place == "Somewhere" assert journeys_req.destination[0].access_duration == 666 assert journeys_req.night_bus_filter_max_factor == 1.5 assert journeys_req.night_bus_filter_base_factor == 900 assert len(journeys_req.datetimes) == 1 assert journeys_req.datetimes[0] == str_to_time_stamp("20120614T080000") assert journeys_req.clockwise is True assert journeys_req.realtime_level == type_pb2.BASE_SCHEDULE assert journeys_req.max_duration == 86400 assert journeys_req.max_transfers == 10 assert journeys_req.wheelchair is False assert journeys_req.max_extra_second_pass == 0 assert journeys_req.forbidden_uris == [] assert journeys_req.allowed_id == [] assert journeys_req.direct_path_duration == 0 assert journeys_req.bike_in_pt is False assert journeys_req.min_nb_journeys == 0 assert journeys_req.timeframe_duration == 0 assert journeys_req.depth == 1 assert journeys_req.isochrone_center.place == "" assert journeys_req.isochrone_center.access_duration == 0
def create_journey_with_pt_and_crowfly(): journey = response_pb2.Journey() journey.departure_date_time = str_to_time_stamp("20180618T060500") journey.duration = 0 journey.nb_transfers = 1 add_whole_pt_section(journey, journey.departure_date_time) # Car crow_fly origin = make_pt_object(type_pb2.ADDRESS, 0.0, 0.0, "Chez tonton") destination = make_pt_object(type_pb2.ADDRESS, 1.0, 1.0, "Chez tata") add_section( journey, origin, destination, 5 * 60, journey.sections[-1].end_date_time, response_pb2.CROW_FLY, response_pb2.Car, None, ) journey.arrival_date_time = journey.sections[-1].end_date_time assert len(journey.sections) == 5 assert journey.duration == 6600 assert journey.arrival_date_time == journey.departure_date_time + journey.duration return journey
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_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 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 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 qualifier_nontransport_duration_with_tc_test(): journey = response_pb2.Journey() journey.arrival_date_time = str_to_time_stamp('20140825T113224') journey.duration = 2620 journey.nb_transfers = 2 journey.sections.add() journey.sections.add() journey.sections.add() journey.sections.add() journey.sections.add() journey.sections.add() journey.sections[0].type = response_pb2.CROW_FLY journey.sections[0].duration = 796 journey.sections[1].type = response_pb2.PUBLIC_TRANSPORT journey.sections[1].duration = 328 journey.sections[2].type = response_pb2.TRANSFER journey.sections[2].duration = 418 journey.sections[3].type = response_pb2.WAITING journey.sections[3].duration = 719 journey.sections[4].type = response_pb2.PUBLIC_TRANSPORT journey.sections[4].duration = 175 journey.sections[-1].type = response_pb2.STREET_NETWORK journey.sections[-1].duration = 864 assert qualifier.get_nontransport_duration(journey) == 2797
def create_dummy_journey(): journey = response_pb2.Journey() journey.arrival_date_time = str_to_time_stamp("20140422T0800") journey.duration = 25 * 60 journey.nb_transfers = 1 s = journey.sections.add() s.type = response_pb2.PUBLIC_TRANSPORT s.origin.uri = "stop_point_1" s.destination.uri = "stop_point_2" s.vehicle_journey.uri = "vj_toto" s.duration = 5 * 60 s = journey.sections.add() s.type = response_pb2.TRANSFER s.duration = 3 * 60 s = journey.sections.add() s.type = response_pb2.WAITING s.duration = 2 * 60 s = journey.sections.add() s.type = response_pb2.PUBLIC_TRANSPORT s.origin.uri = "stop_point_3" s.destination.uri = "stop_point_4" s.duration = 15 * 60 s = journey.sections.add() s.type = response_pb2.STREET_NETWORK s.duration = 10 * 60 return journey
def test_matrix(valid_here_matrix): instance = MagicMock() instance.walking_speed = 1.12 here = Here(instance=instance, service_base_url='bob.com', app_id='toto', app_code='tata') origin = make_pt_object(type_pb2.ADDRESS, 2.439938, 48.572841) destination = make_pt_object(type_pb2.ADDRESS, 2.440548, 48.57307) with requests_mock.Mocker() as req: req.get(requests_mock.ANY, json=valid_here_matrix, status_code=200) response = here.get_street_network_routing_matrix( [origin], [destination, destination, destination], mode='walking', max_duration=42, request={'datetime': str_to_time_stamp('20170621T174600')}, ) assert response.rows[0].routing_response[0].duration == 440 assert response.rows[0].routing_response[ 0].routing_status == response_pb2.reached assert response.rows[0].routing_response[1].duration == -1 assert response.rows[0].routing_response[ 1].routing_status == response_pb2.unreached assert response.rows[0].routing_response[2].duration == 701 assert response.rows[0].routing_response[ 2].routing_status == response_pb2.reached
def standard_choice_test(): journeys = [] # the first is the worst one journey_worst = response_pb2.Journey() journey_worst.arrival_date_time = str_to_time_stamp("20131107T161200") journey_worst.sections.add() journey_worst.sections[0].type = response_pb2.STREET_NETWORK journey_worst.sections[0].street_network.mode = response_pb2.Car journeys.append(journey_worst) # arrive later but no car journey_not_good = response_pb2.Journey() journey_not_good.arrival_date_time = str_to_time_stamp("20131107T171200") journey_not_good.sections.add() journey_not_good.sections[0].type = response_pb2.STREET_NETWORK journey_not_good.sections[0].street_network.mode = response_pb2.Bike journeys.append(journey_not_good) # this is the standard journey_1 = response_pb2.Journey() journey_1.arrival_date_time = str_to_time_stamp("20131107T151200") journey_1.sections.add() journey_1.sections[0].type = response_pb2.STREET_NETWORK journey_1.sections[0].street_network.mode = response_pb2.Bike journeys.append(journey_1) # a better journey, but using car journey_2 = response_pb2.Journey() journey_2.arrival_date_time = str_to_time_stamp("20131107T151000") journey_2.sections.add() journey_2.sections[0].type = response_pb2.STREET_NETWORK journey_2.sections[0].street_network.mode = response_pb2.Car journeys.append(journey_2) standard = qualifier.choose_standard(journeys, qualifier.arrival_crit) print(qualifier.has_car(standard)) print("standard ", standard.arrival_date_time) assert standard == journey_1
def choose_standard_pt_car(): journeys = [] journey1 = response_pb2.Journey() journey1.arrival_date_time = str_to_time_stamp("20141120T170000") journey1.sections.add() journey1.sections[0].type = response_pb2.STREET_NETWORK journey1.sections[0].street_ne
def test_journey_request_current_time(): origin = {"Kisio Digital": 42} destination = {"Somewhere": 666} datetime = str_to_time_stamp("20120614T080000") journey_parameters = JourneyParameters(current_datetime=123456789) req = create_journeys_request(origin, destination, datetime, True, journey_parameters, False) assert req._current_datetime == 123456789
def create_response_with_crow_fly(start_date, end_date, origin, destination): response = response_pb2.Response() journey = response.journeys.add() journey.departure_date_time = str_to_time_stamp(start_date) journey.arrival_date_time = str_to_time_stamp(end_date) journey.duration = 5 * 60 journey.nb_transfers = 0 s = journey.sections.add() s.type = response_pb2.CROW_FLY s.origin.CopyFrom(origin) s.duration = 5 * 60 s.begin_date_time = journey.departure_date_time s.end_date_time = s.begin_date_time + s.duration s.destination.CopyFrom(destination) s.street_network.mode = response_pb2.Walking return response
def test_departure_sort(): """ we want to sort by departure hour, then by duration """ j1 = response_pb2.Journey() j1.departure_date_time = str_to_time_stamp('20151005T071000') j1.arrival_date_time = str_to_time_stamp('20151005T081900') j1.duration = j1.arrival_date_time - j1.departure_date_time j1.nb_transfers = 0 j2 = response_pb2.Journey() j2.departure_date_time = str_to_time_stamp('20151005T072200') j2.arrival_date_time = str_to_time_stamp('20151005T083500') j2.duration = j2.arrival_date_time - j2.departure_date_time j2.nb_transfers = 0 j3 = response_pb2.Journey() j3.departure_date_time = str_to_time_stamp('20151005T074500') j3.arrival_date_time = str_to_time_stamp('20151005T091200') j3.duration = j3.arrival_date_time - j3.departure_date_time j3.nb_transfers = 0 j4 = response_pb2.Journey() j4.departure_date_time = str_to_time_stamp('20151005T074500') j4.arrival_date_time = str_to_time_stamp('20151005T091100') j4.duration = j4.arrival_date_time - j4.departure_date_time j4.nb_transfers = 0 j5 = response_pb2.Journey() j5.departure_date_time = str_to_time_stamp('20151005T074500') j5.arrival_date_time = str_to_time_stamp('20151005T090800') j5.duration = j5.arrival_date_time - j5.departure_date_time j5.nb_transfers = 0 result = [j1, j2, j3, j4, j5] random.shuffle(result) compartor = DepartureJourneySorter(True) result.sort(compartor) eq_(result[0], j1) eq_(result[1], j2) eq_(result[2], j5) eq_(result[3], j4) eq_(result[4], j3)
def test_arrival_sort(): """ we want to sort by arrival hour, then by duration """ j1 = response_pb2.Journey() j1.departure_date_time = str_to_time_stamp('20151005T071000') j1.arrival_date_time = str_to_time_stamp('20151005T081900') j1.duration = j1.arrival_date_time - j1.departure_date_time j1.nb_transfers = 0 j2 = response_pb2.Journey() j2.departure_date_time = str_to_time_stamp('20151005T072200') j2.arrival_date_time = str_to_time_stamp('20151005T083500') j2.duration = j2.arrival_date_time - j2.departure_date_time j2.nb_transfers = 0 j3 = response_pb2.Journey() j3.departure_date_time = str_to_time_stamp('20151005T074500') j3.arrival_date_time = str_to_time_stamp('20151005T091200') j3.duration = j3.arrival_date_time - j3.departure_date_time j3.nb_transfers = 0 j4 = response_pb2.Journey() j4.departure_date_time = str_to_time_stamp('20151005T075000') j4.arrival_date_time = str_to_time_stamp('20151005T091200') j4.duration = j4.arrival_date_time - j4.departure_date_time j4.nb_transfers = 0 j5 = response_pb2.Journey() j5.departure_date_time = str_to_time_stamp('20151005T075500') j5.arrival_date_time = str_to_time_stamp('20151005T091200') j5.duration = j5.arrival_date_time - j5.departure_date_time j5.nb_transfers = 0 result = [j1, j2, j3, j4, j5] random.shuffle(result) comparator = ArrivalJourneySorter(True) result.sort(comparator) assert result[0] == j1 assert result[1] == j2 assert result[2] == j5 assert result[3] == j4 assert result[4] == j3
def previous_journey_test(): """ In the default scenario, the previous journey is one minute before the first 'rapid' if we get one""" builder = ResponseBuilder(default_date=datetime.date(2016, 10, 10))\ .journey(type='rapid', departure='T1200', arrival='T1500')\ .journey(type='fastest', departure='T1100', arrival='T1700')\ .journey(type='non_pt_walk', departure='T1000', arrival='T1534')\ .journey(type='car', departure='T1300', arrival='T1534') scenario = default.Scenario() eq_(scenario.previous_journey_datetime(builder.get_journeys()), str_to_time_stamp('20161010T145900'))
def previous_journey_test_no_rapid(): """ In the default scenario, if we don't get a rapid, the previous journey is one minute before the tardiest journey """ builder = ResponseBuilder(default_date=datetime.date(2016, 10, 10))\ .journey(type='fastest', departure='T1100', arrival='T1700')\ .journey(type='non_pt_walk', departure='T1000', arrival='T1534')\ .journey(type='car', departure='T2000', arrival='T1534') scenario = default.Scenario() eq_(scenario.previous_journey_datetime(builder.get_journeys()), str_to_time_stamp('20161010T165900'))
def next_journey_test(): """ In the default scenario, the next journey is one minute after the first 'rapid' if we get one""" builder = ResponseBuilder(default_date=datetime.date(2016, 10, 10))\ .journey(type='rapid', departure='T1200', arrival='T1500')\ .journey(type='fastest', departure='T1100', arrival='T1700')\ .journey(type='non_pt_walk', departure='T1000', arrival='T1534')\ .journey(type='car', departure='T1300', arrival='T1534') scenario = default.Scenario() assert scenario.next_journey_datetime( builder.get_journeys(), clockwise=True) == str_to_time_stamp('20161010T120100')
def create_journeys_request_test(): origin = {"Kisio Digital": 42} destination = {"Somewhere": 666} journey_parameters = JourneyParameters() datetime = str_to_time_stamp("20120614T080000") planner = Kraken(None) req = planner._create_journeys_request(origin, destination, datetime, True, journey_parameters, False) assert req.requested_api == type_pb2.pt_planner check_basic_journeys_request(req.journeys)
def different_departure_times_test(): response = response_pb2.Response() journey1 = response.journeys.add() journey1.departure_date_time = str_to_time_stamp("20140422T0800") journey1.duration = 5 * 60 journey1.nb_transfers = 0 journey1.sections.add() journey1.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey1.sections[0].duration = 5 * 60 journey2 = response.journeys.add() journey2.departure_date_time = str_to_time_stamp("20140422T0758") journey2.duration = 2 * 60 journey2.nb_transfers = 0 journey2.sections.add() journey2.sections[0].type = response_pb2.PUBLIC_TRANSPORT journey2.sections[0].duration = 2 * 60 sort_journeys(response, 'departure_time', True) assert response.journeys[0].departure_date_time == str_to_time_stamp("20140422T0758") assert response.journeys[1].departure_date_time == str_to_time_stamp("20140422T0800")
def fill_section(self, stat_section, resp_section, previous_section): if 'departure_date_time' in resp_section: stat_section.departure_date_time = utils.str_to_time_stamp( resp_section['departure_date_time']) if 'arrival_date_time' in resp_section: stat_section.arrival_date_time = utils.str_to_time_stamp( resp_section['arrival_date_time']) if 'duration' in resp_section: stat_section.duration = resp_section['duration'] if 'mode' in resp_section: stat_section.mode = get_mode(resp_section['mode'], previous_section) if 'type' in resp_section: stat_section.type = resp_section['type'] if 'from' in resp_section: section_from = resp_section['from'] self.fill_section_from(stat_section, section_from) if 'to' in resp_section: section_to = resp_section['to'] self.fill_section_to(stat_section, section_to) stat_section.vehicle_journey_id = self.get_section_link( resp_section, 'vehicle_journey') stat_section.line_id = self.get_section_link(resp_section, 'line') stat_section.route_id = self.get_section_link(resp_section, 'route') stat_section.network_id = self.get_section_link( resp_section, 'network') stat_section.physical_mode_id = self.get_section_link( resp_section, 'physical_mode') stat_section.commercial_mode_id = self.get_section_link( resp_section, 'commercial_mode') self.fill_section_display_informations(stat_section, resp_section)