示例#1
0
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
示例#2
0
def test_fix_route_ids(route_id, actual_route_id, valid_trip):
    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), ),
            )
        ],
    )

    expected_trip = parse.Trip(
        id=TRIP_ID,
        route_id=actual_route_id,
        direction_id=False,
    )

    parser = SubwayTripsParser()
    parser.load_content(message.SerializeToString())
    actual_trips = list(parser.get_trips())

    if valid_trip:
        assert [expected_trip] == actual_trips
    else:
        assert [] == actual_trips
示例#3
0
def test_vehicle__move_between_trips_attached(
    db_session,
    add_model,
    system_1,
    route_1_1,
    previous_update,
    current_update,
    trip_for_vehicle,
    stop_1_3,
):
    vehicle = add_model(
        models.Vehicle(
            id="vehicle_id",
            system=system_1,
            source=previous_update,
            trip=trip_for_vehicle,
        ))

    importdriver.run_import(
        current_update.pk,
        ParserForTesting([
            parse.Trip(id="trip_id_2", route_id=route_1_1.id),
            parse.Vehicle(id="vehicle_id", trip_id="trip_id_2"),
        ]),
    )

    db_session.refresh(trip_for_vehicle)
    new_trip = (db_session.query(
        models.Trip).filter(models.Trip.id == "trip_id_2").one_or_none())

    assert trip_for_vehicle.vehicle is None
    assert new_trip.vehicle == vehicle
示例#4
0
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
示例#5
0
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
示例#6
0
def test_trip__invalid_route(db_session, system_1, route_1_1, current_update):
    new_trip = parse.Trip(id="trip",
                          route_id="unknown_route",
                          direction_id=True)

    importdriver.run_import(current_update.pk, ParserForTesting([new_trip]))

    all_trips = db_session.query(models.Trip).all()

    assert [] == all_trips
示例#7
0
def test_trip__stop_time_reconciliation(
    db_session,
    add_model,
    system_1,
    route_1_1,
    previous_update,
    current_update,
    old_stop_time_data,
    new_stop_time_data,
    expected_stop_time_data,
    feed_1_1_update_1,
):
    stop_pk_to_stop = {}
    all_stop_ids = set(trip_stop_time.stop_id
                       for trip_stop_time in itertools.chain(
                           old_stop_time_data, new_stop_time_data))
    for stop_id in all_stop_ids:
        stop = add_model(
            models.Stop(
                id=stop_id,
                system=system_1,
                type=models.Stop.Type.STATION,
                source=feed_1_1_update_1,
            ))
        stop_pk_to_stop[stop.pk] = stop

    trip = parse.Trip(
        id="trip",
        route_id=route_1_1.id,
        direction_id=True,
        stop_times=old_stop_time_data,
    )

    importdriver.run_import(previous_update.pk, ParserForTesting([trip]))

    trip.stop_times = new_stop_time_data

    importdriver.run_import(current_update.pk, ParserForTesting([trip]))

    actual_stop_times = [
        convert_trip_stop_time_model_to_parse(trip_stop_time, stop_pk_to_stop)
        for trip_stop_time in db_session.query(models.Trip).all()[0].stop_times
    ]

    assert expected_stop_time_data == actual_stop_times
示例#8
0
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)
示例#9
0
def test_trip__route_from_schedule(db_session, add_model, system_1, route_1_1,
                                   current_update, feed_1_1_update_1):
    add_model(
        models.ScheduledTrip(
            id="trip",
            route=route_1_1,
            service=add_model(
                models.ScheduledService(id="service",
                                        system=system_1,
                                        source=feed_1_1_update_1)),
        ))

    new_trip = parse.Trip(id="trip", route_id=None, direction_id=True)

    importdriver.run_import(current_update.pk, ParserForTesting([new_trip]))

    all_trips = db_session.query(models.Trip).all()

    assert 1 == len(all_trips)
    assert "trip" == all_trips[0].id
    assert route_1_1 == all_trips[0].route
示例#10
0
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])
示例#11
0
def test_direction(existing_direction_id, mta_direction,
                   expected_direction_id):
    trip = gtfs.TripDescriptor(trip_id=TRIP_ID)
    if existing_direction_id is not None:
        expected_direction_id = existing_direction_id
        trip.direction_id = expected_direction_id
    if mta_direction is not None:
        trip.Extensions[trip._extensions_by_number[
            gtfs.MTA_EXTENSION_ID]].direction = mta_direction.value

    message = gtfs.FeedMessage(
        header=gtfs.FeedHeader(gtfs_realtime_version="2.0"),
        entity=[
            gtfs.FeedEntity(id="1", trip_update=gtfs.TripUpdate(trip=trip))
        ],
    )

    expected_trip = parse.Trip(id=TRIP_ID, direction_id=expected_direction_id)

    parser = SubwayTripsParser()
    parser.load_content(message.SerializeToString())
    actual_trips = list(parser.get_trips())

    assert [expected_trip] == actual_trips
示例#12
0
    assert set(map(fields_to_compare, current)) == set(
        map(fields_to_compare,
            db_session.query(entity_type).all()))
    verify_stats(actual_counts, expected_counts)


@pytest.mark.parametrize(
    "parsed_type",
    [
        new_agency,
        new_route,
        new_stop,
        parse.ScheduledService.create_empty("scheduled_service"),
        parse.DirectionRule(id="direction_rule", name="direction_rule"),
        parse.Trip(id="trip_id"),
        new_vehicle,
        new_alert,
    ],
)
def test_duplicate_ids(current_update, parsed_type, route_1_1, stop_1_1):
    if isinstance(parsed_type, parse.Trip):
        parsed_type.route_id = route_1_1.id
    if isinstance(parsed_type, parse.DirectionRule):
        parsed_type.stop_id = stop_1_1.id

    actual_counts = importdriver.run_import(
        current_update.pk, ParserForTesting([parsed_type, parsed_type]))

    verify_stats(actual_counts, (1, 0, 0))
示例#13
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,
                    )
                ],
            ),
        ],
    ]