def trip_1(add_model, route_1_1, stop_1_1, stop_1_2, stop_1_3, stop_1_4, feed_1_1_update_1): stop_times = [ models.TripStopTime( arrival_time=datetime.datetime(2018, 11, 2, 10, 0, 0), stop=stop_1_1, stop_sequence=1, ), models.TripStopTime( arrival_time=datetime.datetime(2018, 11, 2, 10, 1, 0), stop=stop_1_2, stop_sequence=2, ), models.TripStopTime( arrival_time=datetime.datetime(2018, 11, 2, 10, 2, 0), stop=stop_1_3, stop_sequence=3, ), models.TripStopTime( arrival_time=datetime.datetime(2018, 11, 2, 10, 3, 0), stop=stop_1_4, stop_sequence=4, ), ] return add_model( models.Trip( pk=401, id="402", route=route_1_1, stop_times=stop_times, current_stop_sequence=1, source=feed_1_1_update_1, ))
def test_old_trips__include_selectively(time_dot_time): stop_times = [ models.TripStopTime(arrival_time=TIME_1), models.TripStopTime(arrival_time=TIME_2), ] time_dot_time.return_value = TIME_3.timestamp() stop_time_filter = stopservice._TripStopTimeFilter("7.5", "10", "0") assert stop_time_filter.remove(stop_times[0], DIRECTION) is True assert stop_time_filter.remove(stop_times[1], DIRECTION) is False
def test_direction(time_dot_time): stop_times = [ models.TripStopTime(arrival_time=TIME_2), models.TripStopTime(arrival_time=TIME_3), ] time_dot_time.return_value = TIME_1.timestamp() stop_time_filter = stopservice._TripStopTimeFilter("0", "0", "1") assert stop_time_filter.remove(stop_times[0], DIRECTION) is False assert stop_time_filter.remove(stop_times[1], DIRECTION) is True
def trip_for_vehicle(add_model, system_1, route_1_1, stop_1_1, stop_1_2, stop_1_3, feed_1_1_update_1): return add_model( models.Trip( id="trip_id", route=route_1_1, current_stop_sequence=2, stop_times=[ models.TripStopTime(stop_sequence=1, stop=stop_1_1), models.TripStopTime(stop_sequence=2, stop=stop_1_2), models.TripStopTime(stop_sequence=3, stop=stop_1_3), ], source=feed_1_1_update_1, ))
def test_direction_name_matcher__match(direction_rule, expected): matcher = stopservice._DirectionNameMatcher([direction_rule]) stop_time = models.TripStopTime(stop_pk=1, trip=models.Trip()) actual = matcher.match(stop_time) assert expected == actual
def test_old_trips__include_when_no_lower_bound(time_dot_time): stop_time = models.TripStopTime(arrival_time=TIME_1) time_dot_time.return_value = TIME_4.timestamp() stop_time_filter = stopservice._TripStopTimeFilter(None, "10", "0") assert stop_time_filter.remove(stop_time, DIRECTION) is False
def test_get_in_route_by_id( monkeypatch, route_1_model, route_1_small_view, trip_1_model, trip_1_view, stop_1_model, stop_1_small_view, ): monkeypatch.setattr(tripqueries, "get_in_route_by_id", lambda *args, **kwargs: trip_1_model) monkeypatch.setattr(alertqueries, "get_trip_pk_to_active_alerts", lambda *args, **kwargs: {}) stop_time = models.TripStopTime(stop_sequence=1) stop_time.stop = stop_1_model trip_1_model.stop_times = [stop_time] expected = trip_1_view expected.stop_times = [views.TripStopTime.from_model(stop_time)] expected.alerts = [] expected.vehicle = None expected.route = route_1_small_view expected.stop_times[0].stop = stop_1_small_view actual = tripservice.get_in_route_by_id(trip_1_model.route.system.id, trip_1_model.route.id, trip_1_model.id) assert expected == actual
def test_old_trips__exclude(time_dot_time): stop_time = models.TripStopTime(arrival_time=TIME_1) time_dot_time.return_value = TIME_4.timestamp() stop_time_filter = stopservice._TripStopTimeFilter("0", "10", "0") assert stop_time_filter.remove(stop_time, DIRECTION) is True
def test_build_trip_stop_time_response(): system = models.System(id=SYSTEM_ID) stop = models.Stop(system=system) stop.id = STOP_ONE_ID trip = models.Trip(current_stop_sequence=1) trip.pk = TRIP_PK trip.id = TRIP_ID trip_stop_time = models.TripStopTime(arrival_time=TIME_1, departure_time=TIME_2, stop_sequence=1) trip_stop_time.trip = trip trip_stop_time.stop = stop route = models.Route(system=system) route.id = ROUTE_ID trip.route = route last_stop = models.Stop(system=system, id=STOP_TWO_ID, name=STOP_TWO_NAME) last_stop.id = STOP_TWO_ID expected = views.TripStopTime( arrival=views._TripStopTimeEvent(time=TIME_1, delay=None, uncertainty=None), departure=views._TripStopTimeEvent(time=TIME_2, delay=None, uncertainty=None), track=None, future=True, stop_sequence=1, direction=DIRECTION_NAME, trip=views.Trip( id=TRIP_ID, direction_id=None, started_at=None, updated_at=None, vehicle=None, _route_id=ROUTE_ID, _system_id=SYSTEM_ID, route=views.Route(id=ROUTE_ID, color=None, _system_id=SYSTEM_ID), last_stop=views.Stop(id=STOP_TWO_ID, name=STOP_TWO_NAME, _system_id=SYSTEM_ID), ), ) actual = stopservice._build_trip_stop_time_response( trip_stop_time, DIRECTION_NAME, {trip.pk: last_stop}) assert expected == actual
def test_get_in_system_by_id(monkeypatch): stop_one = models.Stop( pk=STOP_ONE_PK, id=STOP_ONE_ID, system=models.System(id=SYSTEM_ID), ) stop_time_one = models.TripStopTime( pk=TRIP_STOP_TIME_ONE_PK, arrival_time=datetime.datetime(2000, 1, 1, 0, 0, 0), ) stop_time_two = models.TripStopTime( pk=TRIP_STOP_TIME_TWO_PK, arrival_time=datetime.datetime(2137, 1, 1, 0, 0, 0), ) child_stops = mock.MagicMock() parent_stop = mock.MagicMock() monkeypatch.setattr(stopqueries, "get_in_system_by_id", lambda *args: stop_one) monkeypatch.setattr(stopqueries, "list_all_transfers_at_stops", lambda *args: []) monkeypatch.setattr(stopqueries, "list_all_stops_in_stop_tree", lambda *args: [stop_one]) monkeypatch.setattr(stopqueries, "list_direction_rules_for_stops", lambda *args: []) monkeypatch.setattr( stopqueries, "list_stop_time_updates_at_stops", lambda *args, **kwargs: [stop_time_one, stop_time_two], ) monkeypatch.setattr(tripqueries, "get_trip_pk_to_last_stop_map", mock.MagicMock()) monkeypatch.setattr(servicemapmanager, "build_stop_pk_to_service_maps_response", mock.MagicMock()) monkeypatch.setattr(stopservice._DirectionNameMatcher, "match", lambda *args: DIRECTION_NAME) monkeypatch.setattr(stopservice._DirectionNameMatcher, "all_names", lambda *args: [DIRECTION_NAME]) fake_stop_tree_response = views.Stop( id=STOP_TWO_ID, name=None, _system_id=SYSTEM_ID, child_stops=child_stops, parent_stop=parent_stop, ) monkeypatch.setattr(stopservice, "_build_stop_tree_response", lambda *args: fake_stop_tree_response) fake_trip_stop_time_response = mock.MagicMock() monkeypatch.setattr( stopservice, "_build_trip_stop_time_response", lambda *args: fake_trip_stop_time_response, ) monkeypatch.setattr( alertqueries, "get_stop_pk_to_active_alerts", lambda *args, **kwargs: {STOP_ONE_PK: []}, ) expected = views.StopLarge( id=STOP_ONE_ID, name=None, latitude=None, longitude=None, url=None, _system_id=SYSTEM_ID, parent_stop=parent_stop, child_stops=child_stops, directions=[DIRECTION_NAME], stop_times=[fake_trip_stop_time_response], alerts=[], transfers=[], ) actual = stopservice.get_in_system_by_id(SYSTEM_ID, STOP_ONE_ID, exclude_trips_before=1) assert expected == actual