def test_list_all_in_geographical_bounds(system_1, add_model, lat, lon, stop_1_1, feed_1_1_update_1): stop_1 = add_model( models.Stop( id="id_1", type=models.Stop.Type.STATION, latitude=0, longitude=0, system=system_1, source=feed_1_1_update_1, )) stop_2 = add_model( models.Stop( id="id_2", type=models.Stop.Type.STATION, latitude=lat, longitude=lon, system=system_1, source=feed_1_1_update_1, )) if lat == 0 and lon == 0: stop_2.parent_stop = stop_1_1 all_stops = stopqueries.list_all_in_geographical_bounds( -1, 1, -1, 1, system_1.id) assert [stop_1] == all_stops
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_geographical_search(monkeypatch): system = models.System(id="system_id") stop_1 = models.Stop(pk=1, id="1", name="1", latitude=0.1, longitude=0.1, system=system) stop_2 = models.Stop(pk=3, id="3", name="3", latitude=0.3, longitude=0.3, system=system) stop_3 = models.Stop(pk=5, id="5", name="5", latitude=0.5, longitude=0.5, system=system) monkeypatch.setattr( stopqueries, "list_all_in_geographical_bounds", lambda *args, **kwargs: [stop_3, stop_2, stop_1], ) actual_stops = stopservice.geographical_search("system_id", 0, 0, geography.distance( 0, 0, 0.4, 0.4), return_service_maps=False) assert [ views.Stop( id="1", name="1", _system_id="system_id", distance=int(geography.distance(0, 0, 0.1, 0.1)), ), views.Stop( id="3", name="3", _system_id="system_id", distance=int(geography.distance(0, 0, 0.3, 0.3)), ), ] == actual_stops
def stop_2_1(add_model, system_2, feed_2_1_update_1): return add_model( models.Stop( pk=111, id="112", system=system_2, type=models.Stop.Type.PLATFORM, source=feed_2_1_update_1, ))
def stop_1_1(add_model, system_1, feed_1_1_update_1): return add_model( models.Stop( pk=101, id="102", system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, ))
def stop_1_3(add_model, system_1, stop_1_2, feed_1_1_update_1): return add_model( models.Stop( pk=105, id="106", system=system_1, type=models.Stop.Type.PLATFORM, parent_stop_pk=stop_1_2.pk, source=feed_1_1_update_1, ))
def stop_1_4(add_model, system_1, stop_1_2, feed_1_1_update_1): return add_model( models.Stop( pk=107, id="108", system=system_1, type=models.Stop.Type.STATION, parent_stop_pk=stop_1_2.pk, source=feed_1_1_update_1, ))
def test_stop__tree_linking( db_session, system_1, add_model, previous_update, current_update, old_id_to_parent_id, expected_id_to_parent_id, ): stop_id_to_stop = { id_: add_model( models.Stop( id=id_, name=id_, system=system_1, source=previous_update, longitude=0, latitude=0, type=parse.Stop.Type.STATION, )) for id_ in old_id_to_parent_id.keys() } for id_, parent_id in old_id_to_parent_id.items(): if parent_id is None: continue stop_id_to_stop[id_].parent_stop = stop_id_to_stop[parent_id] db_session.flush() stop_id_to_stop = { id_: parse.Stop(id=id_, name=id_, longitude=0, latitude=0, type=parse.Stop.Type.STATION) for id_ in expected_id_to_parent_id.keys() } for id_, parent_id in expected_id_to_parent_id.items(): if parent_id is None: continue stop_id_to_stop[id_].parent_stop = stop_id_to_stop[parent_id] importdriver.run_import(current_update.pk, ParserForTesting(list(stop_id_to_stop.values()))) actual_stop_id_parent_id = {} for stop in db_session.query(models.Stop).all(): if stop.parent_stop is not None: actual_stop_id_parent_id[stop.id] = stop.parent_stop.id else: actual_stop_id_parent_id[stop.id] = None assert expected_id_to_parent_id == actual_stop_id_parent_id
def tree_factory(number_of_stops, adjacency_tuples, not_stations=None): system = models.System(id="system_id") stops = [ models.Stop(pk=i, id=str(i), type=models.Stop.Type.STATION, system=system) for i in range(number_of_stops) ] for stop_pk, parent_pk in adjacency_tuples: stops[stop_pk].parent_stop_pk = parent_pk if not_stations is not None: for not_station in not_stations: stops[not_station].type = models.Stop.Type.PLATFORM return stops
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
def test_flush(db_session, add_model, system_1, previous_update, current_update): current_update.update_type = models.FeedUpdate.Type.FLUSH add_model( models.Stop( system=system_1, source_pk=previous_update.pk, type=models.Stop.Type.STATION, )) add_model(models.Route( system=system_1, source_pk=previous_update.pk, )) importdriver.run_import(current_update.pk, ParserForTesting([])) assert [] == db_session.query(models.Route).all()
def test_list_all_in_system(monkeypatch): system = models.System(id=SYSTEM_ID) stop_one = models.Stop(pk=STOP_ONE_PK, id=STOP_ONE_ID, name=STOP_ONE_NAME, system=system) monkeypatch.setattr(systemqueries, "get_by_id", lambda *args, **kwargs: system) monkeypatch.setattr(stopqueries, "list_all_in_system", lambda *args: [stop_one]) expected = [ views.Stop(id=STOP_ONE_ID, name=STOP_ONE_NAME, _system_id=SYSTEM_ID) ] actual = stopservice.list_all_in_system(SYSTEM_ID) assert expected == actual
def list_all_in_system_factory(stops): return lambda system_id: [ stop for stop in stops if stop.system.id == system_id ] # TODO: test that the transfers are returned ion sorted order @pytest.mark.parametrize( "stops,distance,expected_tuples", [ [ # Base case [ models.Stop( id=STOP_1_ID, latitude=1, longitude=1, system=SYSTEM_1), models.Stop( id=STOP_2_ID, latitude=2, longitude=2, system=SYSTEM_1), models.Stop( id=STOP_3_ID, latitude=1.4, longitude=1, system=SYSTEM_2), ], 50000, {(STOP_1_ID, STOP_3_ID), (STOP_3_ID, STOP_1_ID)}, ], [ # No matches [ models.Stop( id=STOP_1_ID, latitude=1, longitude=1, system=SYSTEM_1), models.Stop( id=STOP_2_ID, latitude=2, longitude=2, system=SYSTEM_1), models.Stop(
models.Agency, [models.Agency(id="agency", name="old agency", timezone="")], [new_agency], (0, 1, 0), ], [ models.Route, [models.Route(id="route", description="old route")], [], (0, 0, 1), ], [models.Stop, [], [new_stop], (1, 0, 0)], [ models.Stop, [ models.Stop( id="route", name="old stop", type=models.Stop.Type.STATION) ], [new_stop], (0, 1, 0), ], [ models.Stop, [ models.Stop( id="route", name="old stop", type=models.Stop.Type.STATION) ], [], (0, 0, 1), ], [models.Vehicle, [], [new_vehicle], (1, 0, 0)], [
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
def test_list_all_stops_in_stop_tree(add_model, system_1, base_pk, feed_1_1_update_1): # 2 # / | \ # 1 3 4 # / | # 0 5 add_model( models.Stop( pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1001, parent_stop_pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1000, parent_stop_pk=1001, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1003, parent_stop_pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1005, parent_stop_pk=1003, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1004, parent_stop_pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) # Red herring add_model( models.Stop( pk=1012, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) expected_pks = {1000, 1001, 1002, 1003, 1004, 1005} actual_pks = { stop.pk for stop in stopqueries.list_all_stops_in_stop_tree(base_pk) } assert expected_pks == actual_pks
def test_build_stop_pk_to_descendant_pks_map(add_model, system_1, stations_only, feed_1_1_update_1): # 2 # / | \ # 1 3 4 # / | # 0 5 add_model( models.Stop( pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1001, parent_stop_pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1000, parent_stop_pk=1001, system=system_1, type=models.Stop.Type.PLATFORM, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1003, parent_stop_pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1005, parent_stop_pk=1003, system=system_1, type=models.Stop.Type.PLATFORM, source=feed_1_1_update_1, )) add_model( models.Stop( pk=1004, parent_stop_pk=1002, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) # Red herring add_model( models.Stop( pk=1012, system=system_1, type=models.Stop.Type.STATION, source=feed_1_1_update_1, )) if stations_only: expected_map = {1002: {1001, 1002, 1003, 1004}} else: expected_map = {1002: {1000, 1001, 1002, 1003, 1004, 1005}} actual_map = stopqueries.build_stop_pk_to_descendant_pks_map( [1002], stations_only=stations_only) assert expected_map == actual_map