def test_invert_stop_id_direction_in_bushwick(route_id, stop_id, expected_stop_id): message = gtfs.FeedMessage( header=gtfs.FeedHeader(gtfs_realtime_version="2.0"), entity=[ gtfs.FeedEntity( id="1", trip_update=gtfs.TripUpdate( trip=gtfs.TripDescriptor(trip_id=TRIP_ID, route_id=route_id), stop_time_update=[ gtfs.TripUpdate.StopTimeUpdate(stop_id=stop_id) ], ), ) ], ) expected_trip = parse.Trip( id=TRIP_ID, route_id=route_id, direction_id=False, stop_times=[parse.TripStopTime(stop_id=expected_stop_id)], ) parser = SubwayTripsParser() parser.load_content(message.SerializeToString()) actual_trips = list(parser.get_trips()) assert [expected_trip] == actual_trips
def test_track(scheduled_track, actual_track, expected_track): stop_time = gtfs.TripUpdate.StopTimeUpdate(stop_id=STOP_ID) mta_ext = stop_time.Extensions[stop_time._extensions_by_number[ gtfs.MTA_EXTENSION_ID]] if scheduled_track is not None: mta_ext.scheduled_track = scheduled_track if actual_track is not None: mta_ext.actual_track = actual_track message = gtfs.FeedMessage( header=gtfs.FeedHeader(gtfs_realtime_version="2.0"), entity=[ gtfs.FeedEntity( id="1", trip_update=gtfs.TripUpdate( trip=gtfs.TripDescriptor(trip_id=TRIP_ID), stop_time_update=[stop_time], ), ) ], ) expected_trip = parse.Trip( id=TRIP_ID, direction_id=False, stop_times=[parse.TripStopTime(stop_id=STOP_ID, track=expected_track)], ) parser = SubwayTripsParser() parser.load_content(message.SerializeToString()) actual_trips = list(parser.get_trips()) assert [expected_trip] == actual_trips
def test_parse_trips__transiter_extension(): gtfs = transiter_gtfs_rt_pb2 stop_time_extension_key = gtfs.TripUpdate.StopTimeUpdate._extensions_by_number[ gtfsrealtime.TRANSITER_EXTENSION_ID ] input_stop_time_update = gtfs.TripUpdate.StopTimeUpdate(stop_id=STOP_ID) additional_data = input_stop_time_update.Extensions[stop_time_extension_key] additional_data.track = TRACK trip_message = gtfs.FeedMessage( header=gtfs.FeedHeader(gtfs_realtime_version="2.0"), entity=[ gtfs.FeedEntity( id=TRIP_ID, trip_update=gtfs.TripUpdate( trip=gtfs.TripDescriptor(trip_id=TRIP_ID), stop_time_update=[input_stop_time_update], ), ) ], ) expected_trip = parse.Trip( id=TRIP_ID, stop_times=[parse.TripStopTime(stop_id=STOP_ID, track=TRACK)] ) parser = gtfsrealtime.GtfsRealtimeParser() parser.load_content(trip_message.SerializeToString()) actual_trips = list(parser.get_trips()) assert [expected_trip] == actual_trips
def test_trip__invalid_stops_in_stop_times(db_session, system_1, route_1_1, stop_1_1, current_update): new_trip = parse.Trip( id="trip", route_id=route_1_1.id, direction_id=True, stop_times=[ parse.TripStopTime(stop_id=stop_1_1.id, stop_sequence=2), parse.TripStopTime(stop_id=stop_1_1.id + "blah_bla", stop_sequence=3), ], ) importdriver.run_import(current_update.pk, ParserForTesting([new_trip])) all_trips = db_session.query(models.Trip).all() assert 1 == len(all_trips) assert 1 == len(all_trips[0].stop_times)
def trip_stop_time(stop_sequence, stop_id, arrival_time, future=None): if future is None: return parse.TripStopTime( stop_sequence=stop_sequence, stop_id=stop_id, arrival_time=arrival_time, ) return TripStopTimeWithFuture( stop_sequence=stop_sequence, stop_id=stop_id, arrival_time=arrival_time, future=future, )
def test_clean_all_good(): trip_cleaners = [mock.MagicMock() for __ in range(3)] for cleaner in trip_cleaners: cleaner.return_value = True stop_event_cleaners = [mock.MagicMock() for __ in range(3)] gtfs_cleaner = utils.TripDataCleaner(trip_cleaners, stop_event_cleaners) trip = parse.Trip( id="trip", route_id="L", direction_id=True, stop_times=[parse.TripStopTime(stop_id="L03")], ) clean_trips = gtfs_cleaner.clean([trip]) assert [trip] == clean_trips for cleaner in trip_cleaners: cleaner.assert_called_once_with(trip) for cleaner in stop_event_cleaners: cleaner.assert_called_once_with(trip, trip.stop_times[0])
def build_test_parse_trip_params(gtfs): return [ [ # Check nullable fields gtfs.TripUpdate(trip=gtfs.TripDescriptor(trip_id=TRIP_ID)), parse.Trip(id=TRIP_ID), ], [ # Check fields in the trip descriptor gtfs.TripUpdate( trip=gtfs.TripDescriptor( trip_id=TRIP_ID, route_id=ROUTE_ID, direction_id=TRIP_DIRECTION_ID, schedule_relationship=SCHEDULE_RELATIONSHIP.value, ) ), parse.Trip( id=TRIP_ID, route_id=ROUTE_ID, direction_id=TRIP_DIRECTION_ID, schedule_relationship=SCHEDULE_RELATIONSHIP, ), ], [ # Check start time field in the trip descriptor gtfs.TripUpdate( trip=gtfs.TripDescriptor(trip_id=TRIP_ID, start_time="11:22:33") ), parse.Trip( id=TRIP_ID, start_time=datetime.datetime.now().replace( hour=11, minute=22, second=33, microsecond=0 ), ), ], [ # Check start time and date field in the trip descriptor gtfs.TripUpdate( trip=gtfs.TripDescriptor( trip_id=TRIP_ID, start_date="19900326", start_time="11:22:33" ) ), parse.Trip( id=TRIP_ID, start_time=datetime.datetime(1990, 3, 26, 11, 22, 33) ), ], [ # Check fields in the trip update gtfs.TripUpdate( trip=gtfs.TripDescriptor(trip_id=TRIP_ID), timestamp=int(TIME_1.timestamp()), delay=DELAY, ), parse.Trip(id=TRIP_ID, updated_at=TIME_1, delay=DELAY), ], [ # Check nullable fields in StopTimeUpdate gtfs.TripUpdate( trip=gtfs.TripDescriptor(trip_id=TRIP_ID), stop_time_update=[ gtfs.TripUpdate.StopTimeUpdate( stop_id=STOP_ID, arrival=gtfs.TripUpdate.StopTimeEvent( time=int(TIME_1.timestamp()) ), ) ], ), parse.Trip( id=TRIP_ID, stop_times=[parse.TripStopTime(stop_id=STOP_ID, arrival_time=TIME_1)], ), ], [ # Check all fields in StopTimeUpdate gtfs.TripUpdate( trip=gtfs.TripDescriptor(trip_id=TRIP_ID), stop_time_update=[ gtfs.TripUpdate.StopTimeUpdate( stop_id=STOP_ID, stop_sequence=STOP_SEQUENCE, schedule_relationship=SCHEDULE_RELATIONSHIP_STOP_TIME.value, arrival=gtfs.TripUpdate.StopTimeEvent( time=int(TIME_1.timestamp()), delay=DELAY, uncertainty=UNCERTAINTY_1, ), departure=gtfs.TripUpdate.StopTimeEvent( time=int(TIME_2.timestamp()), delay=DELAY_2, uncertainty=UNCERTAINTY_2, ), ) ], ), parse.Trip( id=TRIP_ID, stop_times=[ parse.TripStopTime( stop_id=STOP_ID, stop_sequence=STOP_SEQUENCE, schedule_relationship=SCHEDULE_RELATIONSHIP_STOP_TIME, arrival_time=TIME_1, arrival_delay=DELAY, arrival_uncertainty=UNCERTAINTY_1, departure_time=TIME_2, departure_delay=DELAY_2, departure_uncertainty=UNCERTAINTY_2, ) ], ), ], ]