def testDuplicateTransfer(self): self.AppendToArchiveContents( "stops.txt", "BEATTY_AIRPORT_HANGER,Airport Hanger,36.868178,-116.784915\n" "BEATTY_AIRPORT_34,Runway 34,36.85352,-116.786316\n") self.AppendToArchiveContents("trips.txt", "AB,FULLW,AIR1\n") self.AppendToArchiveContents( "stop_times.txt", "AIR1,7:00:00,7:00:00,BEATTY_AIRPORT_HANGER,1\n" "AIR1,7:05:00,7:05:00,BEATTY_AIRPORT_34,2\n" "AIR1,7:10:00,7:10:00,BEATTY_AIRPORT_HANGER,3\n") self.SetArchiveContents( "transfers.txt", "from_stop_id,to_stop_id,transfer_type\n" "BEATTY_AIRPORT,BEATTY_AIRPORT_HANGER,0\n" "BEATTY_AIRPORT,BEATTY_AIRPORT_HANGER,3") schedule = self.MakeLoaderAndLoad() e = self.accumulator.PopException('DuplicateID') self.assertEquals('(from_stop_id, to_stop_id)', e.column_name) self.assertEquals('(BEATTY_AIRPORT, BEATTY_AIRPORT_HANGER)', e.value) self.assertTrue(e.is_warning()) self.assertEquals('transfers.txt', e.file_name) self.assertEquals(3, e.row_num) self.accumulator.assert_no_more_exceptions() saved_schedule_file = StringIO() schedule.write_google_transit_feed(saved_schedule_file) self.accumulator.assert_no_more_exceptions() load_problems = util.get_test_failure_problem_reporter( self, ("ExpirationDate", "DuplicateID")) loaded_schedule = transitfeed.Loader(saved_schedule_file, loader_problems=load_problems, extra_validation=True).load() self.assertEquals([0, 3], [ int(t.transfer_type) for t in loaded_schedule.get_transfer_iter() ])
def setUp(self): problems = util.get_test_failure_problem_reporter(self) schedule = transitfeed.Schedule(problem_reporter=problems) self.schedule = schedule schedule.add_agency("Agency", "http://iflyagency.com", "America/Los_Angeles") service_period = schedule.get_default_service_period() service_period.set_start_date("20080101") service_period.set_end_date("20090101") service_period.set_weekday_service(True) self.stop1 = schedule.add_stop(lng=140.01, lat=0, name="140.01,0") self.stop2 = schedule.add_stop(lng=140.02, lat=0, name="140.02,0") self.stop3 = schedule.add_stop(lng=140.03, lat=0, name="140.03,0") self.stop4 = schedule.add_stop(lng=140.04, lat=0, name="140.04,0") self.stop5 = schedule.add_stop(lng=140.05, lat=0, name="140.05,0") self.route1 = schedule.add_route("1", "One", "Bus") self.trip1 = self.route1.add_trip(schedule, "trip 1", trip_id="trip1") # add different types of stop times self.trip1.AddStopTime( self.stop1, arrival_time="17:00:00", departure_time="17:01:00") # both arrival and departure time self.trip1.AddStopTime(self.stop2, schedule=schedule) # non timed self.trip1.AddStopTime(self.stop3, stop_time="17:45:00") # only stop_time # add headways starting before the trip self.trip1.add_frequency("16:00:00", "18:00:00", 1800) # each 30 min self.trip1.add_frequency("18:00:00", "20:00:00", 2700) # each 45 min
def testCustomAttribute(self): """Add unknown attributes to a Transfer and make sure they are saved.""" transfer = transitfeed.Transfer() transfer.attr1 = "foo1" schedule = self.SimpleSchedule() transfer.to_stop_id = "stop1" transfer.from_stop_id = "stop1" schedule.add_transfer_object(transfer) transfer.attr2 = "foo2" saved_schedule_file = StringIO() schedule.write_google_transit_feed(saved_schedule_file) self.accumulator.assert_no_more_exceptions() # Ignore NoServiceExceptions error to keep the test simple load_problems = util.get_test_failure_problem_reporter( self, ("ExpirationDate", "UnrecognizedColumn", "NoServiceExceptions")) loaded_schedule = transitfeed.Loader(saved_schedule_file, loader_problems=load_problems, extra_validation=True).load() transfers = loaded_schedule.get_transfer_list() self.assertEquals(1, len(transfers)) self.assertEquals("foo1", transfers[0].attr1) self.assertEquals("foo1", transfers[0]["attr1"]) self.assertEquals("foo2", transfers[0].attr2) self.assertEquals("foo2", transfers[0]["attr2"])
def test_TemporaryFile(self): loader = transitfeed.Loader( util.data_path('good_feed.zip'), loader_problems=util.get_test_failure_problem_reporter(self), extra_validation=True, memory_db=False) schedule = loader.load() self.assertLoadedCorrectly(schedule) self.assertLoadedStopTimesCorrectly(schedule)
def test_NoLoadStopTimes(self): problems = util.get_test_failure_problem_reporter( self, ignore_types=("ExpirationDate", "UnusedStop", "OtherProblem")) loader = transitfeed.Loader(util.data_path('good_feed.zip'), loader_problems=problems, extra_validation=True, load_stop_times=False) schedule = loader.load() self.assertLoadedCorrectly(schedule) self.assertEqual(0, len(schedule.get_trip('CITY1').get_stop_times()))
def runTest(self): loader = transitfeed.Loader( util.data_path('good_feed.zip'), loader_problems=util.get_test_failure_problem_reporter(self), extra_validation=True) loader.load() # now try using Schedule.Load schedule = transitfeed.Schedule( problem_reporter=util.ExceptionProblemReporterNoExpiration()) schedule.load(util.data_path('good_feed.zip'), extra_validation=True)
def assertLoadAndCheckExtraValues(self, schedule_file): """Load file-like schedule_file and check for extra trip columns.""" load_problems = util.get_test_failure_problem_reporter( self, ("ExpirationDate", "UnrecognizedColumn")) loaded_schedule = transitfeed.Loader(schedule_file, loader_problems=load_problems, extra_validation=True).load() self.assertEqual("foo", loaded_schedule.get_trip("AB1")["t_foo"]) self.assertEqual("", loaded_schedule.get_trip("AB2")["t_foo"]) self.assertEqual("", loaded_schedule.get_trip("AB1")["n_foo"]) self.assertEqual("bar", loaded_schedule.get_trip("AB2")["n_foo"])
def testGetTimeInterpolatedStopsUntimedStart(self): # Temporarily replace the problem reporter so that adding the first # StopTime without a time doesn't throw an exception. old_problems = self.schedule.problem_reporter self.schedule.problem_reporter = util.get_test_failure_problem_reporter( self, ("OtherProblem", )) self.trip3.AddStopTime(self.stop3, schedule=self.schedule) self.schedule.problem_reporter = old_problems self.trip3.AddStopTime(self.stop2, schedule=self.schedule, departure_secs=500, arrival_secs=500) self.assertRaises(ValueError, self.trip3.GetTimeInterpolatedStops)
def setUp(self): problems = util.get_test_failure_problem_reporter(self) schedule = transitfeed.Schedule(problem_reporter=problems) self.schedule = schedule schedule.add_agency("Agency", "http://iflyagency.com", "America/Los_Angeles") service_period = schedule.get_default_service_period() service_period.set_date_has_service('20070101') self.stop1 = schedule.add_stop(lng=140.01, lat=0, name="140.01,0") self.stop2 = schedule.add_stop(lng=140.02, lat=0, name="140.02,0") self.stop3 = schedule.add_stop(lng=140.03, lat=0, name="140.03,0") self.stop4 = schedule.add_stop(lng=140.04, lat=0, name="140.04,0") self.stop5 = schedule.add_stop(lng=140.05, lat=0, name="140.05,0") self.route1 = schedule.add_route("1", "One", "Bus") self.trip1 = self.route1.add_trip(schedule, "trip 1", trip_id='trip1') self.trip1.add_stop_time(self.stop1, schedule=schedule, departure_secs=100, arrival_secs=100) self.trip1.add_stop_time(self.stop2, schedule=schedule) self.trip1.add_stop_time(self.stop3, schedule=schedule) # loop back to stop2 to test that interpolated stops work ok even when # a stop between timepoints is further from the timepoint than the # preceding self.trip1.add_stop_time(self.stop2, schedule=schedule) self.trip1.add_stop_time(self.stop4, schedule=schedule, departure_secs=400, arrival_secs=400) self.trip2 = self.route1.add_trip(schedule, "trip 2", trip_id='trip2') self.trip2.add_stop_time(self.stop2, schedule=schedule, departure_secs=500, arrival_secs=500) self.trip2.AddStopTime(self.stop3, schedule=schedule, departure_secs=600, arrival_secs=600) self.trip2.AddStopTime(self.stop4, schedule=schedule, departure_secs=700, arrival_secs=700) self.trip2.AddStopTime(self.stop3, schedule=schedule, departure_secs=800, arrival_secs=800) self.trip3 = self.route1.add_trip(schedule, "trip 3", trip_id='trip3')
def testExtraFileColumn(self): """Extra columns loaded from a file are preserved when writing.""" # Uncomment the code in assertLoadAndCheckExtraValues to generate this # string. self.SetArchiveContents( "trips.txt", "route_id,service_id,trip_id,t_foo,n_foo\n" "AB,FULLW,AB1,foo,\n" "AB,FULLW,AB2,,bar\n") self.AppendToArchiveContents( "stop_times.txt", "AB2,09:00:00,09:00:00,BULLFROG,1\n" "AB2,09:30:00,09:30:00,STAGECOACH,2\n") load1_problems = util.get_test_failure_problem_reporter( self, ("ExpirationDate", "UnrecognizedColumn")) schedule = self.MakeLoaderAndLoad(loader_problems=load1_problems) saved_schedule_file = StringIO() schedule.write_google_transit_feed(saved_schedule_file) self.assertLoadAndCheckExtraValues(saved_schedule_file)
def testExtraFileColumn(self): """Extra columns loaded from a file are preserved when writing.""" # Uncomment the code in assertLoadAndCheckExtraValues to generate this # string. self.SetArchiveContents( "routes.txt", "route_id,agency_id,route_short_name,route_long_name,route_type," "t_foo,n_foo\n" "AB,DTA,,Airport Bullfrog,3,,\n" "t,DTA,T,,3,foo,\n" "n,DTA,N,,3,,bar\n") load1_problems = util.get_test_failure_problem_reporter( self, ("ExpirationDate", "UnrecognizedColumn")) schedule = self.MakeLoaderAndLoad(loader_problems=load1_problems) saved_schedule_file = StringIO() schedule.write_google_transit_feed(saved_schedule_file) self.assertLoadAndCheckExtraValues(saved_schedule_file)
def runTest(self): problem_reporter = util.get_test_failure_problem_reporter(self) schedule = transitfeed.Schedule(problem_reporter=problem_reporter) route = schedule.add_route(short_name="10", long_name="", route_type="Bus") stop = schedule.add_stop(40, -128, "My stop") # Stop must be added to schedule so that the call # AddStopTime -> AddStopTimeObject -> GetStopTimes -> GetStop can work trip = transitfeed.Trip() trip.route_id = route.route_id trip.service_id = schedule.get_default_service_period().service_id trip.trip_id = "SAMPLE_TRIP" schedule.add_trip_object(trip) # First stop must have time trip.add_stop_time(stop, arrival_secs=300, departure_secs=360) trip.add_stop_time(stop) trip.add_stop_time(stop, arrival_time="00:07:00", departure_time="00:07:30") trip.validate(problem_reporter)
def runTest(self): loader = transitfeed.Loader( util.data_path('utf8bom'), loader_problems=util.get_test_failure_problem_reporter(self), extra_validation=True) loader.load()