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_non_overlapping_feeds(self): dao = Dao(DAO_URL, sql_logging=SQL_LOG) # Load twice the same data under two distinct namespaces dao.load_gtfs(DUMMY_GTFS, feed_id='A') dao.load_gtfs(DUMMY_GTFS, feed_id='B') # Check that each feed only return it's own data feed_a = dao.feed('A') self.assertTrue(feed_a.feed_id == 'A') feed_b = dao.feed('B') self.assertTrue(feed_b.feed_id == 'B') self.assertTrue(len(dao.agencies()) == 4) self.assertTrue(len(feed_a.agencies) == 2) self.assertTrue(len(feed_b.agencies) == 2) self.assertTrue(len(feed_a.routes) * 2 == len(dao.routes())) self.assertTrue(len(feed_b.routes) * 2 == len(dao.routes())) self.assertTrue(len(feed_a.stops) * 2 == len(list(dao.stops()))) self.assertTrue(len(feed_b.stops) * 2 == len(list(dao.stops()))) self.assertTrue(len(feed_a.calendars) * 2 == len(dao.calendars())) self.assertTrue(len(feed_b.calendars) * 2 == len(dao.calendars())) self.assertTrue(len(feed_a.trips) * 2 == len(list(dao.trips()))) self.assertTrue(len(feed_b.trips) * 2 == len(list(dao.trips())))
class TestProviders(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) clear_mappers() self.dao_object = Dao() @mock.patch("exporter.api.radcom.Request") def test_radcom_provider(self, mocked_request): mocked_request.side_effect = _request_mock radcomProvider = RadcomApiDataProvider("", feed_id="1") assert radcomProvider is not None # load data from provider load_result = radcomProvider.load_data_source(self.dao_object) assert load_result is True # check calendars assert len(self.dao_object.calendars()) == 2 # check routes routes_len = 0 for route in self.dao_object.routes(): routes_len = routes_len + 1 assert route.route_id == '43' assert route.route_short_name == '1' assert route.route_long_name == 'Bulevardul Tomis - Sere (C.L.)' assert routes_len == 1 # check stops stops_len = 0 for stop in self.dao_object.stops(): stops_len = stops_len + 1 assert len(stop.stop_times) > 0 assert stop.stop_lat != 0.0 assert stop.stop_lon != 0.0 assert stops_len == 35 # check trips trips_len = 0 for trip in self.dao_object.trips(): trips_len = trips_len + 1 assert trip.shape != None expected_shape_id = "shp1_{0}_{1}".format(trip.route_id, trip.direction_id) assert trip.shape_id == expected_shape_id assert trips_len == 14 # cleanup self.dao_object.delete_feed('1') self.dao_object.flush() def test_brasov_export(self): provider = BrasovApiDataProvider("2") provider.dao = self.dao_object provider._load_agencies() assert provider.agency_id is not None assert len(self.dao_object.agencies()) == 1 provider._load_service() assert len(self.dao_object.calendars()) == 2 assert len(provider.routes) == 0 provider._load_routes() assert len(provider.routes) > 0 assert len(self.dao_object.routes()) > 0 assert len(provider.stop_map) == 0 provider._load_schedule() assert len(provider.stop_map) > 0 assert len(list(self.dao_object.stops())) > 0 assert len(list(self.dao_object.trips())) > 0 # stops_visited_by_route_trips = len(list(dao_fixture.stops())) # provider._load_stops() # assert len(list(dao_fixture.stops())) == stops_visited_by_route_trips # cleanup self.dao_object.delete_feed('2') self.dao_object.flush()
def test_gtfs_data(self): dao = Dao(DAO_URL, sql_logging=False) dao.load_gtfs(DUMMY_GTFS) # Check feed feed = dao.feed() self.assertTrue(feed.feed_id == "") self.assertTrue(feed.feed_publisher_name == "Mecatran") self.assertTrue(feed.feed_publisher_url == "http://www.mecatran.com/") self.assertTrue(feed.feed_contact_email == "*****@*****.**") self.assertTrue(feed.feed_lang == "fr") self.assertTrue(len(dao.agencies()) == 2) self.assertTrue(len(dao.routes()) == 3) self.assertTrue(len(feed.agencies) == 2) self.assertTrue(len(feed.routes) == 3) # Check agencies at = dao.agency("AT") self.assertTrue(at.agency_name == "Agency Train") self.assertTrue(len(at.routes) == 1) ab = dao.agency("AB") self.assertTrue(ab.agency_name == "Agency Bus") self.assertTrue(len(ab.routes) == 2) # Check calendars week = dao.calendar("WEEK") self.assertTrue(len(week.dates) == 253) summer = dao.calendar("SUMMER") self.assertTrue(len(summer.dates) == 42) mon = dao.calendar("MONDAY") self.assertTrue(len(mon.dates) == 49) sat = dao.calendar("SAT") self.assertTrue(len(sat.dates) == 53) for date in mon.dates: self.assertTrue(date.dow() == 0) for date in sat.dates: self.assertTrue(date.dow() == 5) for date in week.dates: self.assertTrue(date.dow() >= 0 and date.dow() <= 4) for date in summer.dates: self.assertTrue(date >= CalendarDate.ymd(2016, 7, 1) and date <= CalendarDate.ymd(2016, 8, 31)) empty = dao.calendars( func.date(CalendarDate.date) == datetime.date(2016, 5, 1)) # OR USE: empty = dao.calendars(CalendarDate.date == "2016-05-01") self.assertTrue(len(empty) == 0) july4 = CalendarDate.ymd(2016, 7, 4) summer_mon = dao.calendars(func.date(CalendarDate.date) == july4.date) n = 0 for cal in summer_mon: self.assertTrue(july4 in cal.dates) n += 1 self.assertTrue(n == 3) # Check stops sbq = dao.stop("BQ") self.assertAlmostEqual(sbq.stop_lat, 44.844, places=2) self.assertAlmostEqual(sbq.stop_lon, -0.573, places=2) self.assertTrue(sbq.stop_name == "Bordeaux Quinconces") n = 0 for stop in dao.stops(Stop.stop_name.like("Gare%")): self.assertTrue(stop.stop_name.startswith("Gare")) n += 1 self.assertTrue(n == 7) n = 0 for stop in dao.stops( fltr=dao.in_area(RectangularArea(44.7, -0.6, 44.9, -0.4))): self.assertTrue(stop.stop_lat >= 44.7 and stop.stop_lat <= 44.9 and stop.stop_lon >= -0.6 and stop.stop_lon <= -0.4) n += 1 self.assertTrue(n == 16) for station in dao.stops(Stop.location_type == Stop.TYPE_STATION): self.assertTrue(station.location_type == Stop.TYPE_STATION) self.assertTrue(len(station.sub_stops) >= 2) for stop in station.sub_stops: self.assertTrue(stop.parent_station == station) # Check zones z_inexistant = dao.zone("ZX") self.assertTrue(z_inexistant is None) z1 = dao.zone("Z1") self.assertEqual(16, len(z1.stops)) z2 = dao.zone("Z2") self.assertEqual(4, len(z2.stops)) # Check transfers transfers = dao.transfers() self.assertTrue(len(transfers) == 3) transfers = dao.transfers( fltr=(dao.transfer_from_stop().stop_id == 'GBSJB')) self.assertTrue(len(transfers) == 1) self.assertTrue(transfers[0].from_stop.stop_id == 'GBSJB') # Check routes tgv = dao.route("TGVBP") self.assertTrue(tgv.agency == at) self.assertTrue(tgv.route_type == 2) r1 = dao.route("BR") self.assertTrue(r1.route_short_name == "R1") self.assertTrue(r1.route_long_name == "Bus Red") n = 0 for route in dao.routes(Route.route_type == 3): self.assertTrue(route.route_type == 3) n += 1 self.assertTrue(n == 2) # Check trip for route n = 0 trips = dao.trips(fltr=Route.route_type == Route.TYPE_BUS) for trip in trips: self.assertTrue(trip.route.route_type == Route.TYPE_BUS) n += 1 self.assertTrue(n > 20) # Check trips on date trips = dao.trips(fltr=func.date(CalendarDate.date) == july4.date, prefetch_calendars=True) n = 0 for trip in trips: self.assertTrue(july4 in trip.calendar.dates) n += 1 self.assertTrue(n > 30)