def test_delays_then_cancellation_in_2_updates(navitia_vj): """ Same test as above, but with nothing in the db, and with 2 updates """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') trip_update.stop_time_updates = [ StopTimeUpdate({'id': 'sa:1'}, departure_delay=timedelta(minutes=5), dep_status='update'), ] handle(real_time_update, [trip_update], 'kisio-digital') trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='delete') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') res = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == 'delete' assert len(trip_update.stop_time_updates) == 0 assert len(trip_update.real_time_updates) == 2
def test_cancellation_then_delay_in_2_updates(navitia_vj): """ same as test_cancellation_then_delay, but with a clear db and in 2 updates """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='delete') trip_update.stop_time_updates = [] real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') handle(real_time_update, [trip_update], 'kisio-digital') trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') trip_update.stop_time_updates = [ StopTimeUpdate({'id': 'sa:3'}, arrival_delay=timedelta(minutes=40), arr_status='update', order=2), ] res, _ = handle(real_time_update, [trip_update], 'kisio-digital') _check_cancellation_then_delay(res)
def create_trip_update(vj_id, trip_id, circulation_date, contributor_id=COTS_CONTRIBUTOR_ID): vj = VehicleJourney( { "trip": { "id": trip_id }, "stop_times": [{ "utc_arrival_time": datetime.time(8, 0), "stop_point": { "stop_area": { "timezone": "UTC" } } }], }, datetime.datetime.combine(circulation_date, datetime.time(7, 0)), datetime.datetime.combine(circulation_date, datetime.time(9, 0)), ) vj.id = vj_id trip_update = TripUpdate(vj=vj, contributor_id=contributor_id) db.session.add(vj) db.session.add(trip_update) return trip_update
def create_trip_update(vj_id, trip_id, circulation_date): trip_update = TripUpdate() vj = VehicleJourney( { 'trip': { 'id': trip_id }, 'stop_times': [{ 'utc_arrival_time': datetime.time(8, 0), 'stop_point': { 'stop_area': { 'timezone': 'UTC' } } }] }, utc.localize( datetime.datetime.combine(circulation_date, datetime.time(7, 0))), utc.localize( datetime.datetime.combine(circulation_date, datetime.time(9, 0)))) vj.id = vj_id trip_update.vj = vj db.session.add(vj) db.session.add(trip_update) return trip_update
def create_trip_update(vj_id, trip_id, circulation_date): trip_update = TripUpdate() vj = VehicleJourney({'trip': {'id': trip_id}}, circulation_date) vj.id = vj_id trip_update.vj = vj db.session.add(vj) db.session.add(trip_update) return trip_update
def purge(nb_day_to_keep): """ purge the database from old information """ logger = logging.getLogger(__name__) until = datetime.date.today() - datetime.timedelta(days=int(nb_day_to_keep)) logger.info('purge until %s', until) VehicleJourney.purge(until) db.session.commit()
def create_trip_update(vj_id, trip_id, circulation_date): trip_update = TripUpdate() vj = VehicleJourney({'trip': {'id': trip_id}, 'stop_times': [ {'arrival_time': datetime.time(8, 0), 'stop_point': {'stop_area': {'timezone': 'UTC'}}} ]}, circulation_date) vj.id = vj_id trip_update.vj = vj db.session.add(vj) db.session.add(trip_update) return trip_update
def create_trip_update(id, trip_id, circulation_date, stops, status='update'): trip_update = TripUpdate( VehicleJourney( { 'trip': { 'id': trip_id }, 'stop_times': [{ 'arrival_time': datetime.time(8, 10), 'stop_point': { 'stop_area': { 'timezone': 'UTC' } } }] }, circulation_date), status) trip_update.id = id for stop in stops: st = StopTimeUpdate({'id': stop['id']}, stop['departure'], stop['arrival']) st.arrival_status = stop['arrival_status'] st.departure_status = stop['departure_status'] trip_update.stop_time_updates.append(st) db.session.add(trip_update) return trip_update
def test_cancellation_then_delay(navitia_vj): """ we have a cancelled vj in the db, and we receive an update on the 3rd stoptime, at the end we have a delayed vj sa:1 sa:2 sa:3 VJ navitia 8:10 9:05-9:10 10:05 VJ in db - update kirin 8:10 9:05-9:10 10:45* """ with app.app_context(): vju = create_trip_update('70866ce8-0638-4fa1-8556-1ddfa22d09d3', 'vehicle_journey:1', datetime.date(2015, 9, 8), [], status='delete') rtu = RealTimeUpdate(None, 'ire') rtu.id = '10866ce8-0638-4fa1-8556-1ddfa22d09d3' rtu.trip_updates.append(vju) db.session.add(rtu) db.session.commit() with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') trip_update.stop_time_updates = [ StopTimeUpdate({'id': 'sa:3'}, arrival_delay=timedelta(minutes=40), arr_status='update'), ] res = handle(real_time_update, [trip_update], 'kisio-digital') _check_cancellation_then_delay(res)
def create_trip_update( id, trip_id, circulation_date, stops, status="update", contributor_id=GTFS_CONTRIBUTOR_ID ): trip_update = TripUpdate( VehicleJourney( { "trip": {"id": trip_id}, "stop_times": [ {"utc_arrival_time": datetime.time(8, 10), "stop_point": {"stop_area": {"timezone": "UTC"}}} ], }, datetime.datetime.combine(circulation_date, datetime.time(7, 10)), datetime.datetime.combine(circulation_date, datetime.time(9, 10)), ), contributor_id, status, ) trip_update.id = id for stop in stops: st = StopTimeUpdate({"id": stop["id"]}, stop["departure"], stop["arrival"]) st.arrival_status = stop["arrival_status"] st.departure_status = stop["departure_status"] trip_update.stop_time_updates.append(st) db.session.add(trip_update) return trip_update
def test_past_midnight(): """ integration of a past midnight """ navitia_vj = { "trip": {"id": "vehicle_journey:1"}, "stop_times": [ { "utc_arrival_time": datetime.time(22, 10), "utc_departure_time": datetime.time(22, 15), "stop_point": {"id": "sa:1"}, }, # arrive at sa:2 at 23:10 and leave the day after { "utc_arrival_time": datetime.time(23, 10), "utc_departure_time": datetime.time(2, 15), "stop_point": {"id": "sa:2", "stop_area": {"timezone": "UTC"}}, }, { "utc_arrival_time": datetime.time(3, 20), "utc_departure_time": datetime.time(3, 25), "stop_point": {"id": "sa:3", "stop_area": {"timezone": "UTC"}}, }, ], } with app.app_context(): contributor = model.Contributor( id=GTFS_CONTRIBUTOR_ID, navitia_coverage=None, connector_type=ConnectorType.gtfs_rt.value ) builder = kirin.poller_workers.gtfs_rt.KirinModelBuilder(contributor) vj = VehicleJourney( navitia_vj, datetime.datetime(2015, 9, 8, 21, 15, 0), datetime.datetime(2015, 9, 9, 4, 20, 0) ) trip_update = TripUpdate(vj, status="update", contributor_id=contributor.id) st = StopTimeUpdate({"id": "sa:2"}, departure_delay=timedelta(minutes=31), dep_status="update", order=1) real_time_update = make_rt_update(raw_data=None, contributor_id=contributor.id) trip_update.stop_time_updates.append(st) res, _ = handle(builder, real_time_update, [trip_update]) assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == "update" assert len(trip_update.stop_time_updates) == 3 assert trip_update.stop_time_updates[0].stop_id == "sa:1" assert trip_update.stop_time_updates[0].arrival == _dt("22:10") assert trip_update.stop_time_updates[0].departure == _dt("22:15") assert trip_update.stop_time_updates[1].stop_id == "sa:2" assert trip_update.stop_time_updates[1].arrival == _dt("23:10") assert trip_update.stop_time_updates[1].arrival_delay == timedelta(0) assert trip_update.stop_time_updates[1].departure == _dt("2:46", day=9) assert trip_update.stop_time_updates[1].departure_delay == timedelta(minutes=31) assert trip_update.stop_time_updates[2].stop_id == "sa:3" assert trip_update.stop_time_updates[2].arrival == _dt("3:51", day=9) assert trip_update.stop_time_updates[2].arrival_delay == timedelta(minutes=31) assert trip_update.stop_time_updates[2].departure == _dt("3:56", day=9) assert trip_update.stop_time_updates[2].departure_delay == timedelta(minutes=31)
def test_handle_new_trip_out_of_order(navitia_vj): """ We have one vj with only one stop time updated, but it's not the first so we have to reorder the stop times in the resulting trip_update """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') st = StopTimeUpdate({'id': 'sa:2'}, departure_delay=timedelta(minutes=40), dep_status='update', arrival_delay=timedelta(minutes=44), arr_status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') trip_update.stop_time_updates.append(st) res = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert len(trip_update.stop_time_updates) == 3 assert trip_update.stop_time_updates[0].stop_id == 'sa:1' assert trip_update.stop_time_updates[0].departure == _dt("8:10") assert trip_update.stop_time_updates[0].arrival == _dt("8:10") assert trip_update.stop_time_updates[1].stop_id == 'sa:2' assert trip_update.stop_time_updates[1].departure == _dt("9:50") assert trip_update.stop_time_updates[1].arrival == _dt("9:49") assert trip_update.stop_time_updates[2].stop_id == 'sa:3' assert trip_update.stop_time_updates[2].departure == _dt("10:05") assert trip_update.stop_time_updates[2].arrival == _dt("10:05")
def test_populate_pb_with_full_dataset(): """ VJ cancelation """ navitia_vj = { 'trip': { 'id': 'vehicle_journey:1' }, 'stop_times': [{ 'arrival_time': datetime.time(8, 10), 'stop_point': { 'stop_area': { 'timezone': 'UTC' } } }] } with app.app_context(): trip_update = TripUpdate() vj = VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)) trip_update.vj = vj trip_update.status = 'delete' trip_update.message = 'Message Test' real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') trip_update.contributor = 'kisio-digital' trip_update.company_id = 'keolis' trip_update.effect = 'DETOUR' real_time_update.trip_updates.append(trip_update) db.session.add(real_time_update) db.session.commit() feed_entity = convert_to_gtfsrt( real_time_update.trip_updates, gtfs_realtime_pb2.FeedHeader.FULL_DATASET) assert feed_entity.header.incrementality == gtfs_realtime_pb2.FeedHeader.FULL_DATASET assert feed_entity.header.gtfs_realtime_version == '1' pb_trip_update = feed_entity.entity[0].trip_update assert pb_trip_update.trip.trip_id == 'vehicle_journey:1' assert pb_trip_update.trip.start_date == '20150908' assert pb_trip_update.HasExtension(kirin_pb2.trip_message) == True assert pb_trip_update.Extensions[ kirin_pb2.trip_message] == 'Message Test' assert pb_trip_update.trip.schedule_relationship == gtfs_realtime_pb2.TripDescriptor.CANCELED assert pb_trip_update.trip.HasExtension(kirin_pb2.contributor) == True assert pb_trip_update.trip.Extensions[ kirin_pb2.contributor] == 'kisio-digital' assert pb_trip_update.trip.Extensions[kirin_pb2.company_id] == 'keolis' assert pb_trip_update.Extensions[ kirin_pb2.effect] == gtfs_realtime_pb2.Alert.DETOUR assert len(feed_entity.entity[0].trip_update.stop_time_update) == 0
def test_populate_pb_with_cancelation(): """ VJ cancelation """ navitia_vj = { 'trip': { 'id': 'vehicle_journey:1' }, 'stop_times': [{ 'utc_arrival_time': datetime.time(8, 10), 'stop_point': { 'stop_area': { 'timezone': 'UTC' } } }] } with app.app_context(): trip_update = TripUpdate() vj = VehicleJourney( navitia_vj, utc.localize(datetime.datetime(2015, 9, 8, 7, 10, 0)), utc.localize(datetime.datetime(2015, 9, 8, 11, 5, 0))) trip_update.vj = vj trip_update.status = 'delete' trip_update.message = 'Message Test' real_time_update = RealTimeUpdate(raw_data=None, connector='cots', contributor='realtime.cots') trip_update.contributor = 'kisio-digital' trip_update.company_id = 'sncf' trip_update.effect = 'REDUCED_SERVICE' real_time_update.trip_updates.append(trip_update) db.session.add(real_time_update) db.session.commit() feed_entity = convert_to_gtfsrt(real_time_update.trip_updates) assert feed_entity.header.incrementality == gtfs_realtime_pb2.FeedHeader.DIFFERENTIAL assert feed_entity.header.gtfs_realtime_version == '1' pb_trip_update = feed_entity.entity[0].trip_update assert pb_trip_update.trip.trip_id == 'vehicle_journey:1' assert pb_trip_update.trip.start_date == '20150908' assert pb_trip_update.HasExtension(kirin_pb2.trip_message) == True assert pb_trip_update.Extensions[ kirin_pb2.trip_message] == 'Message Test' assert pb_trip_update.trip.schedule_relationship == gtfs_realtime_pb2.TripDescriptor.CANCELED assert pb_trip_update.trip.HasExtension(kirin_pb2.contributor) == True assert pb_trip_update.trip.Extensions[ kirin_pb2.contributor] == 'kisio-digital' assert pb_trip_update.trip.Extensions[kirin_pb2.company_id] == 'sncf' assert pb_trip_update.Extensions[ kirin_pb2.effect] == gtfs_realtime_pb2.Alert.REDUCED_SERVICE assert len(feed_entity.entity[0].trip_update.stop_time_update) == 0
def test_populate_pb_with_cancelation(): """ VJ cancelation """ navitia_vj = { "trip": { "id": "vehicle_journey:1" }, "stop_times": [{ "utc_arrival_time": datetime.time(8, 10), "stop_point": { "stop_area": { "timezone": "UTC" } } }], } with app.app_context(): vj = VehicleJourney(navitia_vj, datetime.datetime(2015, 9, 8, 7, 10, 0), datetime.datetime(2015, 9, 8, 11, 5, 0)) trip_update = TripUpdate(vj=vj, contributor_id=COTS_CONTRIBUTOR_ID) trip_update.vj = vj trip_update.status = "delete" trip_update.message = "Message Test" real_time_update = make_rt_update( raw_data=None, connector_type=ConnectorType.cots.value, contributor_id=COTS_CONTRIBUTOR_ID) trip_update.company_id = "sncf" trip_update.effect = "REDUCED_SERVICE" real_time_update.trip_updates.append(trip_update) db.session.add(real_time_update) db.session.commit() feed_entity = convert_to_gtfsrt(real_time_update.trip_updates) assert feed_entity.header.incrementality == gtfs_realtime_pb2.FeedHeader.DIFFERENTIAL assert feed_entity.header.gtfs_realtime_version == "1" pb_trip_update = feed_entity.entity[0].trip_update assert pb_trip_update.trip.trip_id == "vehicle_journey:1" assert pb_trip_update.trip.start_date == "20150908" assert pb_trip_update.HasExtension(kirin_pb2.trip_message) is True assert pb_trip_update.Extensions[ kirin_pb2.trip_message] == "Message Test" assert pb_trip_update.trip.schedule_relationship == gtfs_realtime_pb2.TripDescriptor.CANCELED assert pb_trip_update.trip.HasExtension(kirin_pb2.contributor) is True assert pb_trip_update.trip.Extensions[ kirin_pb2.contributor] == COTS_CONTRIBUTOR_ID assert pb_trip_update.trip.Extensions[kirin_pb2.company_id] == "sncf" assert pb_trip_update.Extensions[ kirin_pb2.effect] == gtfs_realtime_pb2.Alert.REDUCED_SERVICE assert len(feed_entity.entity[0].trip_update.stop_time_update) == 0
def test_populate_pb_with_one_stop_time(): """ an easy one: we have one vj with only one stop time updated fill protobuf from trip_update Verify protobuf """ navitia_vj = { 'trip': { 'id': 'vehicle_journey:1' }, 'stop_times': [{ 'arrival_time': None, 'departure_time': datetime.time(8, 10), 'stop_point': { 'id': 'sa:1' } }, { 'arrival_time': datetime.time(9, 10), 'departure_time': None, 'stop_point': { 'id': 'sa:2' } }] } with app.app_context(): trip_update = TripUpdate() vj = VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)) trip_update.vj = vj st = StopTimeUpdate({'id': 'sa:1'}, departure=_dt("8:15"), arrival=None) real_time_update = RealTimeUpdate(raw_data=None, connector='ire') real_time_update.trip_updates.append(trip_update) trip_update.stop_time_updates.append(st) db.session.add(real_time_update) db.session.commit() feed_entity = convert_to_gtfsrt(real_time_update.trip_updates) assert feed_entity.header.incrementality == gtfs_realtime_pb2.FeedHeader.DIFFERENTIAL assert feed_entity.header.gtfs_realtime_version == '1' pb_trip_update = feed_entity.entity[0].trip_update assert pb_trip_update.trip.trip_id == 'vehicle_journey:1' assert pb_trip_update.trip.start_date == '20150908' assert pb_trip_update.trip.schedule_relationship == gtfs_realtime_pb2.TripDescriptor.SCHEDULED pb_stop_time = feed_entity.entity[0].trip_update.stop_time_update[0] assert pb_stop_time.arrival.time == 0 assert pb_stop_time.departure.time == to_posix_time(_dt("8:15")) assert pb_stop_time.stop_id == 'sa:1' assert pb_trip_update.HasExtension(kirin_pb2.trip_message) == False assert pb_trip_update.trip.HasExtension(kirin_pb2.contributor) == False
def test_multiple_delays_in_2_updates(navitia_vj): """ same test as test_multiple_delays, but with nothing in the db and with 2 trip updates """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') trip_update.stop_time_updates = [ StopTimeUpdate({'id': 'sa:1'}, departure_delay=timedelta(minutes=5), dep_status='update'), ] handle(real_time_update, [trip_update], 'kisio-digital') trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') trip_update.stop_time_updates = [ StopTimeUpdate({'id': 'sa:1'}, departure_delay=timedelta(minutes=10), dep_status='update'), StopTimeUpdate({'id': 'sa:2'}, arrival_delay=timedelta(minutes=2), arr_status='update'), ] res, _ = handle(real_time_update, [trip_update], 'kisio-digital') _check_multiples_delay(res) # we also check that there is what we want in the db db_trip_updates = res.query.from_self(TripUpdate).all() assert len(db_trip_updates) == 1 assert db_trip_updates[0].status == 'update' assert len(RealTimeUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 3
def create_trip_update(id, trip_id, circulation_date, stops, status='update'): trip_update = TripUpdate( VehicleJourney({'trip': { 'id': trip_id }}, circulation_date), status) trip_update.id = id for stop in stops: st = StopTimeUpdate({'id': stop['id']}, stop['departure'], stop['arrival']) st.arrival_status = stop['arrival_status'] st.departure_status = stop['departure_status'] trip_update.stop_time_updates.append(st) db.session.add(trip_update) return trip_update
def create_trip_update(vj_id, trip_id, circulation_date, contributor_id): vj = VehicleJourney( { "trip": {"id": trip_id}, "stop_times": [ {"utc_arrival_time": datetime.time(8, 0), "stop_point": {"stop_area": {"timezone": "UTC"}}} ], }, datetime.datetime.combine(circulation_date, datetime.time(7, 0)), datetime.datetime.combine(circulation_date, datetime.time(9, 0)), ) vj.id = vj_id trip_update = TripUpdate(vj=vj, contributor_id=contributor_id) # Add 3 StopTimeUpdate st1 = StopTimeUpdate({"id": "sa:1"}, None, None, order=0) trip_update.stop_time_updates.append(st1) st2 = StopTimeUpdate({"id": "sa:2"}, None, None, order=1) trip_update.stop_time_updates.append(st2) st3 = StopTimeUpdate({"id": "sa:3"}, None, None, order=2) trip_update.stop_time_updates.append(st3) db.session.add(vj) db.session.add(trip_update) return trip_update
def test_manage_consistency(navitia_vj): """ we receive an update for a vj already in the database sa:1 sa:2 sa:3 VJ navitia 08:10 09:05-09:10 10:05 update kirin - *10:15-09:20* - expected result 08:10-08:10 10:15-10:15 11:10-11:10 """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') st = StopTimeUpdate({'id': 'sa:2'}, arrival_delay=timedelta(minutes=70), dep_status='update', departure_delay=timedelta(minutes=10), arr_status='update', order=1) st.arrival_status = st.departure_status = 'update' real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') real_time_update.id = '30866ce8-0638-4fa1-8556-1ddfa22d09d3' trip_update.stop_time_updates.append(st) res, _ = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == 'update' assert len(trip_update.real_time_updates) == 1 assert len(trip_update.stop_time_updates) == 3 stu_map = {stu.stop_id: stu for stu in trip_update.stop_time_updates} assert 'sa:1' in stu_map assert stu_map['sa:1'].arrival == _dt("8:10") assert stu_map['sa:1'].departure == _dt("8:10") assert 'sa:2' in stu_map assert stu_map['sa:2'].arrival == _dt("10:15") assert stu_map['sa:2'].departure == _dt("10:15") assert 'sa:3' in stu_map assert stu_map['sa:3'].arrival == _dt("11:10") assert stu_map['sa:3'].departure == _dt("11:10")
def test_delays_then_cancellation(setup_database, navitia_vj): """ We have a delay on the first st of a vj in the db and we receive a cancellation on this vj, we should have a cancelled vj in the end sa:1 sa:2 sa:3 VJ navitia 8:10 9:05-9:10 10:05 VJ in db 8:15* 9:05-9:10 10:05 update kirin - """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='delete') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') res = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == 'delete' assert len(trip_update.stop_time_updates) == 0 assert len(trip_update.real_time_updates) == 2
def test_multiple_delays(setup_database, navitia_vj): """ We receive a delay on the first and second stoptimes of a vj, and there was already some delay on the first st of this vj sa:1 sa:2 sa:3 VJ navitia 8:10 9:05-9:10 10:05 VJ in db 8:15* 9:05-9:10 10:05 update kirin 8:20* *9:07-9:10 10:05 """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') trip_update.stop_time_updates = [ # Note: the delay is based of the navitia's vj StopTimeUpdate({'id': 'sa:1'}, departure_delay=timedelta(minutes=10), dep_status='update'), StopTimeUpdate({'id': 'sa:2'}, arrival_delay=timedelta(minutes=2), arr_status='update'), ] res, _ = handle(real_time_update, [trip_update], 'kisio-digital') _check_multiples_delay(res) # we also check that there is what we want in the db db_trip_updates = res.query.from_self(TripUpdate).all() assert len(db_trip_updates) == 2 for tu in db_trip_updates: assert tu.status == 'update' assert len( RealTimeUpdate.query.all()) == 3 # 2 already in db, one new update assert len(StopTimeUpdate.query.all()) == 6 # 3 st * 2 vj in the db
def test_populate_pb_with_cancelation(): """ VJ cancelation """ navitia_vj = {'trip': {'id': 'vehicle_journey:1'}} with app.app_context(): trip_update = TripUpdate() vj = VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)) trip_update.vj = vj trip_update.status = 'delete' trip_update.message = 'Message Test' real_time_update = RealTimeUpdate(raw_data=None, connector='ire') trip_update.contributor = 'kisio-digital' real_time_update.trip_updates.append(trip_update) db.session.add(real_time_update) db.session.commit() feed_entity = convert_to_gtfsrt(real_time_update.trip_updates) assert feed_entity.header.incrementality == gtfs_realtime_pb2.FeedHeader.DIFFERENTIAL assert feed_entity.header.gtfs_realtime_version == '1' pb_trip_update = feed_entity.entity[0].trip_update assert pb_trip_update.trip.trip_id == 'vehicle_journey:1' assert pb_trip_update.trip.start_date == '20150908' assert pb_trip_update.HasExtension(kirin_pb2.trip_message) == True assert pb_trip_update.Extensions[ kirin_pb2.trip_message] == 'Message Test' assert pb_trip_update.trip.schedule_relationship == gtfs_realtime_pb2.TripDescriptor.CANCELED assert pb_trip_update.trip.HasExtension(kirin_pb2.contributor) == True assert pb_trip_update.trip.Extensions[ kirin_pb2.contributor] == 'kisio-digital' assert len(feed_entity.entity[0].trip_update.stop_time_update) == 0
def test_handle_new_vj(): """an easy one: we have one vj with only one stop time updated""" navitia_vj = { 'trip': { 'id': 'vehicle_journey:1' }, 'stop_times': [{ 'arrival_time': None, 'departure_time': datetime.time(8, 10), 'stop_point': { 'id': 'sa:1', 'stop_area': { 'timezone': 'UTC' } } }, { 'arrival_time': datetime.time(9, 10), 'departure_time': None, 'stop_point': { 'id': 'sa:2', 'stop_area': { 'timezone': 'UTC' } } }] } with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') st = StopTimeUpdate({'id': 'sa:1'}, departure_delay=timedelta(minutes=5), dep_status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') trip_update.stop_time_updates.append(st) res = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == 'update' assert len(trip_update.stop_time_updates) == 2 assert trip_update.stop_time_updates[0].stop_id == 'sa:1' assert trip_update.stop_time_updates[0].departure == _dt("8:15") assert trip_update.stop_time_updates[0].arrival == _dt("8:15") assert trip_update.stop_time_updates[1].stop_id == 'sa:2' assert trip_update.stop_time_updates[1].departure == _dt("9:10") assert trip_update.stop_time_updates[1].arrival == _dt("9:10") # testing that RealTimeUpdate is persisted in db db_trip_updates = real_time_update.query.from_self(TripUpdate).all() assert len(db_trip_updates) == 1 assert db_trip_updates[0].status == 'update' db_st_updates = real_time_update.query.from_self( StopTimeUpdate).order_by('stop_id').all() assert len(db_st_updates) == 2 assert db_st_updates[0].stop_id == 'sa:1' assert db_st_updates[0].departure == _dt("8:15") assert db_st_updates[0].arrival == _dt("8:15") assert db_st_updates[0].trip_update_id == db_trip_updates[0].vj_id assert db_st_updates[1].stop_id == 'sa:2' assert db_st_updates[1].departure == _dt("9:10") assert db_st_updates[1].arrival == _dt("9:10") assert db_st_updates[1].trip_update_id == db_trip_updates[0].vj_id
def test_past_midnight(): """ integration of a past midnight """ navitia_vj = { "trip": { "id": "vehicle_journey:1" }, "stop_times": [ { "utc_arrival_time": datetime.time(22, 10), "utc_departure_time": datetime.time(22, 15), "stop_point": { "id": "sa:1" }, }, # arrive at sa:2 at 23:10 and leave the day after { "utc_arrival_time": datetime.time(23, 10), "utc_departure_time": datetime.time(2, 15), "stop_point": { "id": "sa:2", "stop_area": { "timezone": "UTC" } }, }, { "utc_arrival_time": datetime.time(3, 20), "utc_departure_time": datetime.time(3, 25), "stop_point": { "id": "sa:3", "stop_area": { "timezone": "UTC" } }, }, ], } with app.app_context(): vj = VehicleJourney(navitia_vj, datetime.datetime(2015, 9, 8, 21, 15, 0), datetime.datetime(2015, 9, 9, 4, 20, 0)) trip_update = TripUpdate(vj, status="update", contributor_id=COTS_CONTRIBUTOR_ID) st = StopTimeUpdate({"id": "sa:2"}, departure_delay=timedelta(minutes=31), dep_status="update", order=1) real_time_update = make_rt_update( raw_data=None, connector_type=ConnectorType.cots.value, contributor_id=COTS_CONTRIBUTOR_ID) trip_update.stop_time_updates.append(st) res, _ = handle(real_time_update, [trip_update], contributor_id=COTS_CONTRIBUTOR_ID, is_new_complete=False) assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == "update" assert len(trip_update.stop_time_updates) == 3 assert trip_update.stop_time_updates[0].stop_id == "sa:1" assert trip_update.stop_time_updates[0].arrival == _dt("22:10") assert trip_update.stop_time_updates[0].departure == _dt("22:15") assert trip_update.stop_time_updates[1].stop_id == "sa:2" assert trip_update.stop_time_updates[1].arrival == _dt("23:10") assert trip_update.stop_time_updates[1].arrival_delay == timedelta(0) assert trip_update.stop_time_updates[1].departure == _dt("2:46", day=9) assert trip_update.stop_time_updates[1].departure_delay == timedelta( minutes=31) assert trip_update.stop_time_updates[2].stop_id == "sa:3" assert trip_update.stop_time_updates[2].arrival == _dt("3:20", day=9) assert trip_update.stop_time_updates[2].arrival_delay == timedelta(0) assert trip_update.stop_time_updates[2].departure == _dt("3:25", day=9) assert trip_update.stop_time_updates[2].departure_delay == timedelta(0)
def _create_db_vj(navitia_vj): return VehicleJourney(navitia_vj, datetime.datetime(2015, 9, 8, 7, 10, 0), datetime.datetime(2015, 9, 8, 11, 5, 0))
def test_populate_pb_with_deleted_stop_time(): """ test protobuf for partial delete nav vj sa:1 * ---- * sa:2 * ---- * sa:3 * ---- * sa:4 we stop the vj from going to sa:3 and sa:4 sa:1 * ---- * sa:2 And we delay sa:1 of 5 minutes Note: in the message sent sa:2 departure will be removed as well as sa:3 departures/arrival and sa:4' arrival """ #we add another impacted stop time to the Model from datetime import time navitia_vj = { 'trip': { 'id': 'vehicle_journey:1' }, 'stop_times': [{ 'arrival_time': None, 'departure_time': time(8, 11), 'stop_point': { 'id': 'sa:1', 'stop_area': { 'timezone': 'Europe/Paris' } } }, { 'arrival_time': time(9, 10), 'departure_time': time(9, 11), 'stop_point': { 'id': 'sa:2', 'stop_area': { 'timezone': 'Europe/Paris' } } }, { 'arrival_time': time(10, 10), 'departure_time': time(10, 11), 'stop_point': { 'id': 'sa:3', 'stop_area': { 'timezone': 'Europe/Paris' } } }, { 'arrival_time': time(11, 10), 'departure_time': None, 'stop_point': { 'id': 'sa:4', 'stop_area': { 'timezone': 'Europe/Paris' } } }] } with app.app_context(): trip_update = TripUpdate() vj = VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)) trip_update.vj = vj real_time_update = RealTimeUpdate(raw_data=None, connector='ire', contributor='realtime.ire') real_time_update.trip_updates.append(trip_update) st = StopTimeUpdate({'id': 'sa:1'}, departure=_dt("8:15"), departure_delay=timedelta(minutes=5), arrival=None) trip_update.stop_time_updates.append(st) st = StopTimeUpdate( {'id': 'sa:2'}, arrival=_dt("9:10"), departure=_dt("9:11"), dep_status='delete', # we delete the departure message="bob's on the track") real_time_update.trip_updates[0].stop_time_updates.append(st) st = StopTimeUpdate( {'id': 'sa:3'}, # Note: we still send the departure/arrival for coherence arrival=_dt("10:10"), dep_status='delete', # we delete both departure and arrival departure=_dt("10:11"), arr_status='delete', message="bob's on the track") real_time_update.trip_updates[0].stop_time_updates.append(st) st = StopTimeUpdate( {'id': 'sa:4'}, arrival=_dt("11:10"), arr_status='delete', # we delete only the arrival departure=_dt("11:11"), message="bob's on the track") real_time_update.trip_updates[0].stop_time_updates.append(st) db.session.add(real_time_update) db.session.commit() feed_entity = convert_to_gtfsrt(real_time_update.trip_updates) assert feed_entity.header.incrementality == gtfs_realtime_pb2.FeedHeader.DIFFERENTIAL assert feed_entity.header.gtfs_realtime_version, '1' assert len(feed_entity.entity) == 1 pb_trip_update = feed_entity.entity[0].trip_update assert pb_trip_update.trip.trip_id == 'vehicle_journey:1' assert pb_trip_update.trip.start_date == '20150908' assert pb_trip_update.HasExtension(kirin_pb2.trip_message) is False assert pb_trip_update.trip.HasExtension(kirin_pb2.contributor) is False assert pb_trip_update.trip.schedule_relationship == gtfs_realtime_pb2.TripDescriptor.SCHEDULED assert len(pb_trip_update.stop_time_update) == 4 pb_stop_time = pb_trip_update.stop_time_update[0] assert pb_stop_time.stop_id == 'sa:1' assert pb_stop_time.arrival.time == 0 assert pb_stop_time.arrival.delay == 0 assert pb_stop_time.departure.time == to_posix_time(_dt("8:15")) assert pb_stop_time.departure.delay == 5 * 60 assert pb_stop_time.Extensions[kirin_pb2.stoptime_message] == "" pb_stop_time = pb_trip_update.stop_time_update[1] assert pb_stop_time.stop_id == 'sa:2' assert pb_stop_time.arrival.time == to_posix_time(_dt("9:10")) assert pb_stop_time.arrival.delay == 0 # the arrival at the stop is still scheduled, but the departure is skipped assert pb_stop_time.arrival.Extensions[kirin_pb2.stop_time_event_relationship] == \ gtfs_realtime_pb2.TripUpdate.StopTimeUpdate.SCHEDULED assert pb_stop_time.departure.time == to_posix_time(_dt("9:11")) assert pb_stop_time.departure.delay == 0 assert pb_stop_time.departure.Extensions[kirin_pb2.stop_time_event_relationship] == \ gtfs_realtime_pb2.TripUpdate.StopTimeUpdate.SKIPPED assert pb_stop_time.Extensions[ kirin_pb2.stoptime_message] == "bob's on the track" pb_stop_time = pb_trip_update.stop_time_update[2] assert pb_stop_time.stop_id == 'sa:3' # both departure and arrival are SKIPPED assert pb_stop_time.arrival.time == to_posix_time(_dt("10:10")) assert pb_stop_time.arrival.delay == 0 assert pb_stop_time.arrival.Extensions[kirin_pb2.stop_time_event_relationship] == \ gtfs_realtime_pb2.TripUpdate.StopTimeUpdate.SKIPPED assert pb_stop_time.departure.time == to_posix_time(_dt("10:11")) assert pb_stop_time.departure.delay == 0 assert pb_stop_time.departure.Extensions[kirin_pb2.stop_time_event_relationship] == \ gtfs_realtime_pb2.TripUpdate.StopTimeUpdate.SKIPPED assert pb_stop_time.Extensions[ kirin_pb2.stoptime_message] == "bob's on the track" pb_stop_time = pb_trip_update.stop_time_update[3] assert pb_stop_time.stop_id == 'sa:4' # only the arrival is skipped, the departure does not exists as it's the last stop assert pb_stop_time.arrival.time == to_posix_time(_dt("11:10")) assert pb_stop_time.arrival.delay == 0 assert pb_stop_time.arrival.Extensions[kirin_pb2.stop_time_event_relationship] == \ gtfs_realtime_pb2.TripUpdate.StopTimeUpdate.SKIPPED assert pb_stop_time.departure.time == to_posix_time(_dt("11:11")) assert pb_stop_time.departure.delay == 0 assert pb_stop_time.departure.Extensions[kirin_pb2.stop_time_event_relationship] == \ gtfs_realtime_pb2.TripUpdate.StopTimeUpdate.SCHEDULED assert pb_stop_time.Extensions[ kirin_pb2.stoptime_message] == "bob's on the track"
def test_simple_delay(navitia_vj): """Test on delay when there is nothing in the db""" with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') st = StopTimeUpdate({'id': 'sa:1'}, departure_delay=timedelta(minutes=10), dep_status='update', arrival_delay=timedelta(minutes=5), arr_status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') trip_update.stop_time_updates.append(st) res = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == 'update' assert len(trip_update.stop_time_updates) == 3 assert trip_update.stop_time_updates[0].stop_id == 'sa:1' assert trip_update.stop_time_updates[0].arrival == _dt( '8:20') # departure assert trip_update.stop_time_updates[0].arrival_delay == timedelta( minutes=5) assert trip_update.stop_time_updates[0].arrival_status == 'update' assert trip_update.stop_time_updates[0].departure == _dt( '8:20') # 8:10 + 10mn assert trip_update.stop_time_updates[0].departure_delay == timedelta( minutes=10) assert trip_update.stop_time_updates[0].departure_status == 'update' assert trip_update.stop_time_updates[1].stop_id == 'sa:2' assert trip_update.stop_time_updates[1].arrival == _dt('9:05') assert trip_update.stop_time_updates[1].arrival_delay == timedelta(0) assert trip_update.stop_time_updates[1].arrival_status == 'none' assert trip_update.stop_time_updates[1].departure == _dt('9:10') assert trip_update.stop_time_updates[1].departure_delay == timedelta(0) assert trip_update.stop_time_updates[1].departure_status == 'none' # testing that RealTimeUpdate is persisted in db db_trip_updates = real_time_update.query.from_self(TripUpdate).all() assert len(db_trip_updates) == 1 assert db_trip_updates[0].status == 'update' db_st_updates = real_time_update.query.from_self( StopTimeUpdate).order_by('stop_id').all() assert len(db_st_updates) == 3 assert db_st_updates[0].stop_id == 'sa:1' assert db_st_updates[0].arrival == _dt('8:20') # departure assert db_st_updates[0].arrival_delay == timedelta(minutes=5) assert db_st_updates[0].arrival_status == 'update' assert db_st_updates[0].departure == _dt('8:20') # 8:10 + 10mn assert db_st_updates[0].departure_delay == timedelta(minutes=10) assert db_st_updates[0].departure_status == 'update' assert db_st_updates[1].stop_id == 'sa:2' assert db_st_updates[1].arrival == _dt('9:05') assert db_st_updates[1].departure == _dt('9:10') assert db_st_updates[1].trip_update_id == db_trip_updates[0].vj_id assert db_st_updates[2].stop_id == 'sa:3'
def test_handle_update_vj(setup_database, navitia_vj): """ this time we receive an update for a vj already in the database sa:1 sa:2 sa:3 VJ navitia 8:10 9:05-9:10 10:05 VJ in db 8:15* 9:05-9:10 10:05 update kirin - *9:15-9:20* - """ with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') st = StopTimeUpdate({'id': 'sa:2'}, arrival_delay=timedelta(minutes=10), dep_status='update', departure_delay=timedelta(minutes=10), arr_status='update') st.arrival_status = st.departure_status = 'update' real_time_update = RealTimeUpdate(raw_data=None, connector='ire') real_time_update.id = '30866ce8-0638-4fa1-8556-1ddfa22d09d3' trip_update.stop_time_updates.append(st) res = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == 'update' assert len(trip_update.real_time_updates) == 2 assert len(trip_update.stop_time_updates) == 3 stu_map = {stu.stop_id: stu for stu in trip_update.stop_time_updates} assert 'sa:1' in stu_map assert stu_map['sa:1'].arrival == _dt("8:15") assert stu_map['sa:1'].departure == _dt("8:15") assert 'sa:2' in stu_map assert stu_map['sa:2'].arrival == _dt("9:15") assert stu_map['sa:2'].departure == _dt("9:20") assert 'sa:3' in stu_map assert stu_map['sa:3'].arrival == _dt("10:05") assert stu_map['sa:3'].departure == _dt("10:05") # testing that RealTimeUpdate is persisted in db db_trip_updates = TripUpdate.query.join(VehicleJourney).order_by( 'circulation_date').all() assert len(db_trip_updates) == 2 assert real_time_update.query.from_self( TripUpdate).all()[0].status == 'update' st_updates = real_time_update.query.from_self(StopTimeUpdate).order_by( 'stop_id').all() assert len(st_updates) == 6 # testing that trip update on 2015/09/07 is remaining correctly stored in db assert len(db_trip_updates[0].stop_time_updates) == 3 db_stu_map = { stu.stop_id: stu for stu in db_trip_updates[0].stop_time_updates } assert 'sa:1' in db_stu_map assert db_stu_map['sa:1'].arrival is None assert db_stu_map['sa:1'].departure == _dt("8:35", day=7) assert 'sa:2' in db_stu_map assert db_stu_map['sa:2'].arrival == _dt("9:35", day=7) assert db_stu_map['sa:2'].departure == _dt("9:40", day=7) assert 'sa:3' in db_stu_map assert db_stu_map['sa:3'].arrival == _dt("10:35", day=7) assert db_stu_map['sa:3'].departure is None # testing that trip update on 2015/09/08 is correctly merged and stored in db assert len(db_trip_updates[1].stop_time_updates) == 3 db_stu_map = { stu.stop_id: stu for stu in db_trip_updates[1].stop_time_updates } assert 'sa:1' in db_stu_map assert db_stu_map['sa:1'].arrival == _dt("8:15") assert db_stu_map['sa:1'].departure == _dt("8:15") assert 'sa:2' in db_stu_map assert db_stu_map['sa:2'].arrival == _dt("9:15") assert db_stu_map['sa:2'].departure == _dt("9:20") assert 'sa:3' in db_stu_map assert db_stu_map['sa:3'].arrival == _dt("10:05") assert db_stu_map['sa:3'].departure == _dt("10:05")
def test_past_midnight(): """ integration of a past midnight """ navitia_vj = { 'trip': { 'id': 'vehicle_journey:1' }, 'stop_times': [ { 'arrival_time': datetime.time(22, 10), 'departure_time': datetime.time(22, 15), 'stop_point': { 'id': 'sa:1', 'stop_area': { 'timezone': 'UTC' } } }, # arrive at sa:2 at 23:10 and leave the day after { 'arrival_time': datetime.time(23, 10), 'departure_time': datetime.time(2, 15), 'stop_point': { 'id': 'sa:2', 'stop_area': { 'timezone': 'UTC' } } }, { 'arrival_time': datetime.time(3, 20), 'departure_time': datetime.time(3, 25), 'stop_point': { 'id': 'sa:3', 'stop_area': { 'timezone': 'UTC' } } } ] } with app.app_context(): trip_update = TripUpdate(VehicleJourney(navitia_vj, datetime.date(2015, 9, 8)), status='update') st = StopTimeUpdate({'id': 'sa:2'}, departure_delay=timedelta(minutes=31), dep_status='update') real_time_update = RealTimeUpdate(raw_data=None, connector='ire') trip_update.stop_time_updates.append(st) res = handle(real_time_update, [trip_update], 'kisio-digital') assert len(res.trip_updates) == 1 trip_update = res.trip_updates[0] assert trip_update.status == 'update' assert len(trip_update.stop_time_updates) == 3 assert trip_update.stop_time_updates[0].stop_id == 'sa:1' assert trip_update.stop_time_updates[0].arrival == _dt("22:10") assert trip_update.stop_time_updates[0].departure == _dt("22:15") assert trip_update.stop_time_updates[1].stop_id == 'sa:2' assert trip_update.stop_time_updates[1].arrival == _dt("23:10") assert trip_update.stop_time_updates[1].arrival_delay == timedelta(0) assert trip_update.stop_time_updates[1].departure == _dt("2:46", day=9) assert trip_update.stop_time_updates[1].departure_delay == timedelta( minutes=31) assert trip_update.stop_time_updates[2].stop_id == 'sa:3' assert trip_update.stop_time_updates[2].arrival == _dt("3:20", day=9) assert trip_update.stop_time_updates[2].arrival_delay == timedelta(0) assert trip_update.stop_time_updates[2].departure == _dt("3:25", day=9) assert trip_update.stop_time_updates[2].departure_delay == timedelta(0)