def test_broken(self): exception = False try: clear_mappers() dao = Dao("") dao.load_gtfs(BROKEN_GTFS, lenient=False) except KeyError: exception = True self.assertTrue(exception) clear_mappers() dao = Dao("") dao.load_gtfs(BROKEN_GTFS, lenient=True) # The following are based on BROKEN GTFS content, # that is the entities count minus broken ones. self.assertTrue(len(dao.routes()) == 4) self.assertTrue(len(list(dao.stops())) == 12) self.assertTrue(len(dao.calendars()) == 2) self.assertTrue(len(list(dao.trips())) == 104) self.assertTrue(len(dao.stoptimes()) == 500) self.assertTrue(len(dao.fare_attributes()) == 2) self.assertTrue(len(dao.fare_rules()) == 4) # This stop has missing coordinates in the broken file stop00 = dao.stop('FUR_CREEK_RES3') self.assertAlmostEqual(stop00.stop_lat, 0.0, 5) self.assertAlmostEqual(stop00.stop_lon, 0.0, 5)
def test_complex_queries(self): dao = Dao(DAO_URL, sql_logging=SQL_LOG) dao.load_gtfs(DUMMY_GTFS) # Get the list of departures: # 1) from "Porte de Bourgogne" # 2) on 4th July # 3) between 10:00 and 14:00 # 4) on route type BUS # 5) not the last of trip (only departing) porte_bourgogne = dao.stop("BBG") july4 = CalendarDate.ymd(2016, 7, 4) from_time = gtfstime(10, 00) to_time = gtfstime(14, 00) departures = dao.stoptimes( fltr=(StopTime.stop == porte_bourgogne) & (StopTime.departure_time >= from_time) & (StopTime.departure_time <= to_time) & (Route.route_type == Route.TYPE_BUS) & (func.date(CalendarDate.date) == july4.date), prefetch_trips=True) n = 0 for dep in departures: self.assertTrue(dep.stop == porte_bourgogne) self.assertTrue(july4 in dep.trip.calendar.dates) self.assertTrue(dep.trip.route.route_type == Route.TYPE_BUS) self.assertTrue(dep.departure_time >= from_time and dep.departure_time <= to_time) n += 1 self.assertTrue(n > 10) # Plage is a stop that is used only in summer (hence the name!) plage = dao.stop("BPG") # Get the list of stops used by some route: # 1) All-year round route_red = dao.route("BR") stoplist_all = list(dao.stops(fltr=Trip.route == route_red)) # 2) Only in january from_date = CalendarDate.ymd(2016, 1, 1) to_date = CalendarDate.ymd(2016, 1, 31) stoplist_jan = list( dao.stops(fltr=(Trip.route == route_red) & (func.date(CalendarDate.date) >= from_date.date) & (func.date(CalendarDate.date) <= to_date.date))) # Now, make some tests self.assertTrue(len(stoplist_all) > 5) self.assertTrue(plage in stoplist_all) self.assertFalse(plage in stoplist_jan) stoplist = list(stoplist_all) stoplist.remove(plage) self.assertTrue(set(stoplist) == set(stoplist_jan)) # Get all routes passing by the set of stops routes = dao.routes(fltr=or_(StopTime.stop == stop for stop in stoplist_jan)) stopset = set() for route in routes: for trip in route.trips: for stoptime in trip.stop_times: stopset.add(stoptime.stop) self.assertTrue(set(stoplist_jan).issubset(stopset))
def test_demo(self): dao = Dao(DAO_URL, sql_logging=False) dao.load_gtfs(DUMMY_GTFS) print("List of stops named '...Bordeaux...':") stops_bordeaux = list( dao.stops(fltr=(Stop.stop_name.ilike('%Bordeaux%')) & (Stop.location_type == Stop.TYPE_STOP))) for stop in stops_bordeaux: print(stop.stop_name) print("List of routes passing by those stops:") routes_bordeaux = dao.routes(fltr=or_(StopTime.stop == stop for stop in stops_bordeaux)) for route in routes_bordeaux: print("%s - %s" % (route.route_short_name, route.route_long_name)) july4 = CalendarDate.ymd(2016, 7, 4) print("All departures from those stops on %s:" % (july4.as_date())) departures = list( dao.stoptimes(fltr=(or_(StopTime.stop == stop for stop in stops_bordeaux)) & (StopTime.departure_time != None) & (func.date(CalendarDate.date) == july4.date))) print("There is %d departures" % (len(departures))) for departure in departures: print("%30.30s %10.10s %-20.20s > %s" % (departure.stop.stop_name, fmttime(departure.departure_time), departure.trip.route.route_long_name, departure.trip.trip_headsign)) print("Number of departures and time range per stop on %s:" % (july4.as_date())) departure_by_stop = defaultdict(list) for departure in departures: departure_by_stop[departure.stop].append(departure) for stop, deps in departure_by_stop.items(): min_dep = min(d.departure_time for d in deps) max_dep = max(d.departure_time for d in deps) print("%30.30s %3d departures (from %s to %s)" % (stop.stop_name, len(deps), fmttime(min_dep), fmttime(max_dep))) # Compute the average distance and time to next stop by route type ntd = [[0, 0, 0.0] for type in range(0, Route.TYPE_FUNICULAR + 1)] for departure in departures: # The following is guaranteed to succeed as we have departure_time == Null for last stop time in trip next_arrival = departure.trip.stop_times[departure.stop_sequence + 1] hop_dist = next_arrival.shape_dist_traveled - departure.shape_dist_traveled hop_time = next_arrival.arrival_time - departure.departure_time route_type = departure.trip.route.route_type ntd[route_type][0] += 1 ntd[route_type][1] += hop_time ntd[route_type][2] += hop_dist for route_type in range(0, len(ntd)): n, t, d = ntd[route_type] if n > 0: print( "The average distance to the next stop on those departures for route type %d is %.2f meters" % (route_type, d / n)) print( "The average time in sec to the next stop on those departures for route type %d is %s" % (route_type, fmttime(t / n)))