Exemplo n.º 1
0
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()
        vj = VehicleJourney(navitia_vj, datetime.date(2015, 9, 8))
        trip_update.vj = vj
        st = StopTimeUpdate({'id': 'sa:2'}, departure=_dt("9:50"), arrival=_dt("9:49"))
        real_time_update = RealTimeUpdate(raw_data=None, connector='ire')
        trip_update.stop_time_updates.append(st)
        res = handle(real_time_update, [trip_update])

        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 == None

        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 == None
        assert trip_update.stop_time_updates[2].arrival == _dt("10:05")
Exemplo n.º 2
0
def test_populate_pb_with_cancelation():
    """
    VJ cancelation
    """
    navitia_vj = {'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'
        real_time_update = RealTimeUpdate(raw_data=None, connector='ire')
        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)

        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.CANCELED

        assert len(feed_entity.entity[0].trip_update.stop_time_update) == 0
Exemplo n.º 3
0
def test_populate_pb_with_full_dataset():
    """
    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, 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 len(feed_entity.entity[0].trip_update.stop_time_update) == 0
Exemplo n.º 4
0
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
Exemplo n.º 5
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
Exemplo n.º 6
0
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
Exemplo n.º 7
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
def test_populate_pb_with_two_stop_time():
    """
    an easy one: we have one vj with only one stop time updated
    fill protobuf from trip_update
    Verify protobuf
    """

    #we add another impacted stop time to the Model
    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)

        st = StopTimeUpdate({'id': 'sa:2'},
                            departure=_dt("8:21"),
                            arrival=_dt("8:20"),
                            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) == False
        assert pb_trip_update.trip.HasExtension(kirin_pb2.contributor) == False
        assert pb_trip_update.trip.schedule_relationship == gtfs_realtime_pb2.TripDescriptor.SCHEDULED

        assert len(pb_trip_update.stop_time_update) == 2

        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.departure.time == to_posix_time(_dt("8:15"))
        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("8:20"))
        assert pb_stop_time.departure.time == to_posix_time(_dt("8:21"))
        assert pb_stop_time.Extensions[kirin_pb2.stoptime_message] == "bob's on the track"
Exemplo n.º 11
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
Exemplo n.º 12
0
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
Exemplo n.º 13
0
def create_trip_update(id, trip_id, circulation_date, stops):
    trip_update = TripUpdate()
    trip_update.id = id
    vj = VehicleJourney({'id': trip_id}, circulation_date)
    trip_update.vj = vj
    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(vj)
    db.session.add(trip_update)
    return trip_update
Exemplo n.º 14
0
def test_handle_new_vj():
    """an easy one: we have one vj with only one stop time updated"""
    navitia_vj = {'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
        trip_update.status = 'update'
        st = StopTimeUpdate({'id': 'sa:1'}, departure=_dt("8:15"), arrival=None)
        real_time_update = RealTimeUpdate(raw_data=None, connector='ire')
        trip_update.stop_time_updates.append(st)
        res = handle(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) == 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 == None

        assert trip_update.stop_time_updates[1].stop_id == 'sa:2'
        assert trip_update.stop_time_updates[1].departure == None
        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 == None
        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 == None
        assert db_st_updates[1].arrival == _dt("9:10")
        assert db_st_updates[1].trip_update_id == db_trip_updates[0].vj_id
Exemplo n.º 15
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 = {'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)

        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'
Exemplo n.º 16
0
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
Exemplo n.º 17
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"
Exemplo n.º 18
0
def test_populate_pb_with_two_stop_time():
    """
    an easy one: we have one vj with only one stop time updated
    fill protobuf from trip_update
    Verify protobuf
    """

    #we add another impacted stop time to the Model
    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': 'Europe/Paris'
                }
            }
        }, {
            'arrival_time': datetime.time(9, 10),
            'departure_time': None,
            'stop_point': {
                'id': 'sa:2',
                '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'},
                            departure=_dt("8:21"),
                            departure_delay=timedelta(minutes=-40),
                            arrival=_dt("8:20"),
                            arrival_delay=timedelta(minutes=-40),
                            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) == 2

        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("8:20"))
        assert pb_stop_time.arrival.delay == -40 * 60
        assert pb_stop_time.departure.time == to_posix_time(_dt("8:21"))
        assert pb_stop_time.departure.delay == -40 * 60
        assert pb_stop_time.Extensions[
            kirin_pb2.stoptime_message] == "bob's on the track"
Exemplo n.º 19
0
def test_handle_update_vj(setup_database, navitia_vj):
    """
    this time we receive an update for a vj already in the database
    """
    with app.app_context():
        trip_update = TripUpdate()
        vj = VehicleJourney(navitia_vj, datetime.date(2015, 9, 8))
        trip_update.status = 'update'
        trip_update.vj = vj
        st = StopTimeUpdate({'id': 'sa:2'}, departure=_dt("9:20"), arrival=_dt("9:15"))
        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])

        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 == None
        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 ==  None


        # 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 == 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 ==  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 == None
        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 ==  None
Exemplo n.º 20
0
def test_populate_pb_for_added_trip():
    """
    For an added trip we don't call navitia to get a vj instead we create and initialize one vj only with 'id'
    Fill protobuf from trip_update
    Verify protobuf
    """
    navitia_vj = make_navitia_empty_vj('vehicle_journey:1')

    with app.app_context():
        trip_update = TripUpdate()
        vj = VehicleJourney(
            navitia_vj,
            utc_since_dt=utc.localize(datetime.datetime(2015, 9, 8, 5, 10, 0)),
            utc_until_dt=utc.localize(datetime.datetime(2015, 9, 8, 8, 10, 0)),
            vj_start_dt=utc.localize(datetime.datetime(2015, 9, 8, 5, 10, 0)))
        trip_update.vj = vj
        trip_update.status = 'add'
        trip_update.effect = 'ADDITIONAL_SERVICE'
        trip_update.physical_mode_id = 'physical_mode:LongDistanceTrain'
        real_time_update = RealTimeUpdate(raw_data=None,
                                          connector='cots',
                                          contributor='realtime.cots')
        real_time_update.trip_updates.append(trip_update)
        st = StopTimeUpdate({'id': 'sa:1'},
                            departure=_dt("8:15"),
                            departure_delay=timedelta(minutes=5),
                            arrival=None,
                            arr_status='none',
                            dep_status='add')
        trip_update.stop_time_updates.append(st)

        st = StopTimeUpdate({'id': 'sa:2'},
                            departure=_dt("8:21"),
                            departure_delay=timedelta(minutes=-40),
                            arrival=_dt("8:20"),
                            arrival_delay=timedelta(minutes=-40),
                            message="bob's on the track",
                            arr_status='add',
                            dep_status='none')
        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'
        assert len(feed_entity.entity) == 1
        pb_trip_update = feed_entity.entity[0].trip_update
        assert pb_trip_update.trip.trip_id == 'OCE:SN: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.HasExtension(kirin_pb2.company_id) is False
        assert pb_trip_update.HasExtension(kirin_pb2.effect) is True
        assert pb_trip_update.Extensions[
            kirin_pb2.effect] == gtfs_realtime_pb2.Alert.ADDITIONAL_SERVICE
        assert pb_trip_update.vehicle.Extensions[
            kirin_pb2.physical_mode_id] == 'physical_mode:LongDistanceTrain'

        assert len(pb_trip_update.stop_time_update) == 2

        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] == ""
        assert pb_stop_time.arrival.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.SCHEDULED
        assert pb_stop_time.departure.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.ADDED

        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("8:20"))
        assert pb_stop_time.arrival.delay == -40 * 60
        assert pb_stop_time.departure.time == to_posix_time(_dt("8:21"))
        assert pb_stop_time.departure.delay == -40 * 60
        assert pb_stop_time.Extensions[
            kirin_pb2.stoptime_message] == "bob's on the track"
        assert pb_stop_time.arrival.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.ADDED
        assert pb_stop_time.departure.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.SCHEDULED
Exemplo n.º 21
0
def test_populate_pb_for_added_trip():
    """
    For an added trip we don't call navitia to get a vj instead we create and initialize one vj only with 'id'
    Fill protobuf from trip_update
    Verify protobuf
    """
    navitia_vj = make_navitia_empty_vj("vehicle_journey:1")

    with app.app_context():
        vj = VehicleJourney(
            navitia_vj,
            since_dt=datetime.datetime(2015, 9, 8, 5, 10, 0),
            until_dt=datetime.datetime(2015, 9, 8, 8, 10, 0),
            vj_start_dt=datetime.datetime(2015, 9, 8, 5, 10, 0),
        )
        trip_update = TripUpdate(vj=vj, contributor_id=COTS_CONTRIBUTOR_ID)
        trip_update.vj = vj
        trip_update.status = "add"
        trip_update.effect = "ADDITIONAL_SERVICE"
        trip_update.physical_mode_id = "physical_mode:LongDistanceTrain"
        real_time_update = make_rt_update(
            raw_data=None,
            connector_type=ConnectorType.cots.value,
            contributor_id=COTS_CONTRIBUTOR_ID)
        real_time_update.trip_updates.append(trip_update)
        st = StopTimeUpdate(
            {"id": "sa:1"},
            departure=_dt("8:15"),
            departure_delay=timedelta(minutes=5),
            arrival=None,
            arr_status="none",
            dep_status="add",
        )
        trip_update.stop_time_updates.append(st)

        st = StopTimeUpdate(
            {"id": "sa:2"},
            departure=_dt("8:21"),
            departure_delay=timedelta(minutes=-40),
            arrival=_dt("8:20"),
            arrival_delay=timedelta(minutes=-40),
            message="bob's on the track",
            arr_status="add",
            dep_status="none",
        )
        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"
        assert len(feed_entity.entity) == 1
        pb_trip_update = feed_entity.entity[0].trip_update
        assert pb_trip_update.trip.trip_id == "OCE:SN: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 True
        assert pb_trip_update.trip.HasExtension(kirin_pb2.company_id) is False
        assert pb_trip_update.HasExtension(kirin_pb2.effect) is True
        assert pb_trip_update.Extensions[
            kirin_pb2.effect] == gtfs_realtime_pb2.Alert.ADDITIONAL_SERVICE
        assert pb_trip_update.vehicle.Extensions[
            kirin_pb2.physical_mode_id] == "physical_mode:LongDistanceTrain"

        assert len(pb_trip_update.stop_time_update) == 2

        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] == ""
        assert pb_stop_time.arrival.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.SCHEDULED
        assert pb_stop_time.departure.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.ADDED

        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("8:20"))
        assert pb_stop_time.arrival.delay == -40 * 60
        assert pb_stop_time.departure.time == to_posix_time(_dt("8:21"))
        assert pb_stop_time.departure.delay == -40 * 60
        assert pb_stop_time.Extensions[
            kirin_pb2.stoptime_message] == "bob's on the track"
        assert pb_stop_time.arrival.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.ADDED
        assert pb_stop_time.departure.Extensions[
            kirin_pb2.stop_time_event_status] == kirin_pb2.SCHEDULED