Esempio n. 1
0
 def testTripIdConflict(self):
     problems = util.GetTestFailureProblemReporter(self)
     schedule = transitfeed.Schedule(problem_reporter=problems)
     service_period = schedule.GetDefaultServicePeriod()
     service_period.SetDateHasService("20070101")
     route = schedule.AddRoute("0", "Long Name", "Bus")
     route.AddTrip()
     route.AddTrip(
         schedule=schedule,
         headsign="hs1",
         service_period=service_period,
         trip_id="1",
     )
     route.AddTrip(schedule, "hs2", service_period, "2")
     route.AddTrip(trip_id="4")
     route.AddTrip()  # This will be given a random trip_id
     trip_list = sorted(schedule.GetTripList(),
                        key=lambda t: int(t.trip_id))
     self.assertMatchesRegex("0 1 2 4 \d{7,9}",
                             " ".join(t.trip_id for t in trip_list))
     self.assertEqual(",hs1,hs2,,",
                      ",".join(t["trip_headsign"] for t in trip_list))
     for t in trip_list:
         self.assertEqual(service_period.service_id, t.service_id)
         self.assertEqual(route.route_id, t.route_id)
    def testBuildFeedWithUtf8Names(self):
        problems = util.GetTestFailureProblemReporter(self)
        schedule = transitfeed.Schedule(problem_reporter=problems)
        schedule.AddAgency("\xc8\x8b Fly Agency", "http://iflyagency.com",
                           "America/Los_Angeles")
        service_period = schedule.GetDefaultServicePeriod()
        service_period.SetDateHasService('20070101')
        # "u020b i with inverted accent breve" encoded in utf-8
        stop1 = schedule.AddStop(lng=140, lat=48.2, name="\xc8\x8b hub")
        # "u020b i with inverted accent breve" as unicode string
        stop2 = schedule.AddStop(lng=140.001,
                                 lat=48.201,
                                 name=u"remote \u020b station")
        route = schedule.AddRoute(u"\u03b2", "Beta", "Bus")
        trip = route.AddTrip(schedule, u"to remote \u020b station")
        repr(stop1)
        repr(stop2)
        repr(route)
        repr(trip)
        trip.AddStopTime(stop1, schedule=schedule, stop_time='10:00:00')
        trip.AddStopTime(stop2, stop_time='10:10:00')

        schedule.Validate(problems)
        schedule.WriteGoogleTransitFeed(self.tempfilepath)
        read_schedule = \
            transitfeed.Loader(self.tempfilepath, problems=problems,
                               extra_validation=True).Load()
        self.assertEquals(u'\u020b Fly Agency',
                          read_schedule.GetDefaultAgency().agency_name)
        self.assertEquals(
            u'\u03b2',
            read_schedule.GetRoute(route.route_id).route_short_name)
        self.assertEquals(u'to remote \u020b station',
                          read_schedule.GetTrip(trip.trip_id).trip_headsign)
Esempio n. 3
0
 def runTest(self):
     loader = transitfeed.Loader(
         util.DataPath("utf8bom"),
         problems=util.GetTestFailureProblemReporter(self),
         extra_validation=True,
     )
     loader.Load()
Esempio n. 4
0
    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.IsWarning())
        self.assertEquals('transfers.txt', e.file_name)
        self.assertEquals(3, e.row_num)
        self.accumulator.AssertNoMoreExceptions()

        saved_schedule_file = StringIO()
        schedule.WriteGoogleTransitFeed(saved_schedule_file)
        self.accumulator.AssertNoMoreExceptions()
        load_problems = util.GetTestFailureProblemReporter(
            self, ("ExpirationDate", "DuplicateID"))
        loaded_schedule = transitfeed.Loader(saved_schedule_file,
                                             problems=load_problems,
                                             extra_validation=True).Load()
        self.assertEquals(
            [0, 3],
            [int(t.transfer_type) for t in loaded_schedule.GetTransferIter()])
Esempio n. 5
0
  def setUp(self):
    problems = util.GetTestFailureProblemReporter(self)
    schedule = transitfeed.Schedule(problem_reporter=problems)
    self.schedule = schedule
    schedule.AddAgency("Agency", "http://iflyagency.com",
                       "America/Los_Angeles")
    service_period = schedule.GetDefaultServicePeriod()
    service_period.SetStartDate("20080101")
    service_period.SetEndDate("20090101")
    service_period.SetWeekdayService(True)
    self.stop1 = schedule.AddStop(lng=140.01, lat=0, name="140.01,0")
    self.stop2 = schedule.AddStop(lng=140.02, lat=0, name="140.02,0")
    self.stop3 = schedule.AddStop(lng=140.03, lat=0, name="140.03,0")
    self.stop4 = schedule.AddStop(lng=140.04, lat=0, name="140.04,0")
    self.stop5 = schedule.AddStop(lng=140.05, lat=0, name="140.05,0")
    self.route1 = schedule.AddRoute("1", "One", "Bus")

    self.trip1 = self.route1.AddTrip(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.AddFrequency("16:00:00", "18:00:00", 1800) # each 30 min
    self.trip1.AddFrequency("18:00:00", "20:00:00", 2700) # each 45 min
Esempio n. 6
0
  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.AddTransferObject(transfer)
    transfer.attr2 = "foo2"

    saved_schedule_file = io.BytesIO()
    schedule.WriteGoogleTransitFeed(saved_schedule_file)
    self.accumulator.AssertNoMoreExceptions()

    # Ignore NoServiceExceptions error to keep the test simple
    load_problems = util.GetTestFailureProblemReporter(
        self, ("ExpirationDate", "UnrecognizedColumn", "NoServiceExceptions"))
    loaded_schedule = transitfeed.Loader(saved_schedule_file,
                                         problems=load_problems,
                                         extra_validation=True).Load()
    transfers = loaded_schedule.GetTransferList()
    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"])
Esempio n. 7
0
 def test_TemporaryFile(self):
     loader = transitfeed.Loader(
         util.DataPath('good_feed.zip'),
         problems=util.GetTestFailureProblemReporter(self),
         extra_validation=True,
         memory_db=False)
     schedule = loader.Load()
     self.assertLoadedCorrectly(schedule)
     self.assertLoadedStopTimesCorrectly(schedule)
Esempio n. 8
0
 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.GetTestFailureProblemReporter(
       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)
Esempio n. 9
0
 def assertLoadAndCheckExtraValues(self, schedule_file):
     """Load file-like schedule_file and check for extra route columns."""
     load_problems = util.GetTestFailureProblemReporter(
         self, ("ExpirationDate", "UnrecognizedColumn"))
     loaded_schedule = transitfeed.Loader(schedule_file,
                                          problems=load_problems,
                                          extra_validation=True).Load()
     self.assertEqual("foo", loaded_schedule.GetRoute("t")["t_foo"])
     self.assertEqual("", loaded_schedule.GetRoute("AB")["t_foo"])
     self.assertEqual("bar", loaded_schedule.GetRoute("n")["n_foo"])
     self.assertEqual("", loaded_schedule.GetRoute("AB")["n_foo"])
Esempio n. 10
0
    def runTest(self):
        loader = transitfeed.Loader(
            util.DataPath('good_feed.zip'),
            problems=util.GetTestFailureProblemReporter(self),
            extra_validation=True)
        loader.Load()

        # now try using Schedule.Load
        schedule = transitfeed.Schedule(
            problem_reporter=util.ExceptionProblemReporterNoExpiration())
        schedule.Load(util.DataPath('good_feed.zip'), extra_validation=True)
Esempio n. 11
0
 def test_NoLoadStopTimes(self):
     problems = util.GetTestFailureProblemReporter(
         self,
         ignore_types=("ExpirationDate", "UnusedStop", "OtherProblem"))
     loader = transitfeed.Loader(util.DataPath('good_feed.zip'),
                                 problems=problems,
                                 extra_validation=True,
                                 load_stop_times=False)
     schedule = loader.Load()
     self.assertLoadedCorrectly(schedule)
     self.assertEqual(0, len(schedule.GetTrip('CITY1').GetStopTimes()))
Esempio n. 12
0
  def testBuildSimpleFeed(self):
    """Make a very simple feed using the Schedule class."""
    problems = util.GetTestFailureProblemReporter(self, ("ExpirationDate",
                                                         "NoServiceExceptions"))
    schedule = transitfeed.Schedule(problem_reporter=problems)

    schedule.AddAgency("Test Agency", "http://example.com",
                       "America/Los_Angeles")

    service_period = schedule.GetDefaultServicePeriod()
    self.assertTrue(service_period.service_id)
    service_period.SetWeekdayService(has_service=True)
    service_period.SetStartDate("20070320")
    service_period.SetEndDate("20071231")

    stop1 = schedule.AddStop(lng=-140.12, lat=48.921,
                             name="one forty at forty eight")
    stop2 = schedule.AddStop(lng=-140.22, lat=48.421, name="west and south")
    stop3 = schedule.AddStop(lng=-140.32, lat=48.121, name="more away")
    stop4 = schedule.AddStop(lng=-140.42, lat=48.021, name="more more away")

    route = schedule.AddRoute(short_name="R", long_name="My Route",
                              route_type="Bus")
    self.assertTrue(route.route_id)
    self.assertEqual(route.route_short_name, "R")
    self.assertEqual(route.route_type, 3)

    trip = route.AddTrip(schedule, headsign="To The End",
                         service_period=service_period)
    trip_id = trip.trip_id
    self.assertTrue(trip_id)
    trip = schedule.GetTrip(trip_id)
    self.assertEqual("To The End", trip.trip_headsign)
    self.assertEqual(service_period, trip.service_period)

    trip.AddStopTime(stop=stop1, arrival_secs=3600*8, departure_secs=3600*8)
    trip.AddStopTime(stop=stop2)
    trip.AddStopTime(stop=stop3, arrival_secs=3600*8 + 60*60,
                     departure_secs=3600*8 + 60*60)
    trip.AddStopTime(stop=stop4, arrival_time="9:13:00",
                     departure_secs=3600*8 + 60*103, stop_headsign="Last stop",
                     pickup_type=1, drop_off_type=3)

    schedule.Validate()
    schedule.WriteGoogleTransitFeed(self.tempfilepath)
    read_schedule = \
        transitfeed.Loader(self.tempfilepath, problems=problems,
                           extra_validation=True).Load()
    self.assertEqual(4, len(read_schedule.GetTrip(trip_id).GetTimeStops()))
    self.assertEqual(1, len(read_schedule.GetRouteList()))
    self.assertEqual(4, len(read_schedule.GetStopList()))
 def testStopIdConflict(self):
     problems = util.GetTestFailureProblemReporter(self)
     schedule = transitfeed.Schedule(problem_reporter=problems)
     schedule.AddStop(lat=3, lng=4.1, name="stop1", stop_id="1")
     schedule.AddStop(lat=3, lng=4.0, name="stop0", stop_id="0")
     schedule.AddStop(lat=3, lng=4.2, name="stop2")
     schedule.AddStop(lat=3, lng=4.2, name="stop4", stop_id="4")
     # AddStop will try to use stop_id=4 first but it is taken
     schedule.AddStop(lat=3, lng=4.2, name="stop5")
     stop_list = sorted(schedule.GetStopList(), key=lambda s: s.stop_name)
     self.assertEqual("stop0 stop1 stop2 stop4 stop5",
                      " ".join([s.stop_name for s in stop_list]))
     self.assertMatchesRegex(r"0 1 2 4 \d{7,9}",
                             " ".join(s.stop_id for s in stop_list))
Esempio n. 14
0
    def setUp(self):
        problems = util.GetTestFailureProblemReporter(self)
        schedule = transitfeed.Schedule(problem_reporter=problems)
        self.schedule = schedule
        schedule.AddAgency("Agency", "http://iflyagency.com",
                           "America/Los_Angeles")
        service_period = schedule.GetDefaultServicePeriod()
        service_period.SetDateHasService('20070101')
        self.stop1 = schedule.AddStop(lng=140.01, lat=0, name="140.01,0")
        self.stop2 = schedule.AddStop(lng=140.02, lat=0, name="140.02,0")
        self.stop3 = schedule.AddStop(lng=140.03, lat=0, name="140.03,0")
        self.stop4 = schedule.AddStop(lng=140.04, lat=0, name="140.04,0")
        self.stop5 = schedule.AddStop(lng=140.05, lat=0, name="140.05,0")
        self.route1 = schedule.AddRoute("1", "One", "Bus")

        self.trip1 = self.route1.AddTrip(schedule, "trip 1", trip_id='trip1')
        self.trip1.AddStopTime(self.stop1,
                               schedule=schedule,
                               departure_secs=100,
                               arrival_secs=100)
        self.trip1.AddStopTime(self.stop2, schedule=schedule)
        self.trip1.AddStopTime(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.AddStopTime(self.stop2, schedule=schedule)
        self.trip1.AddStopTime(self.stop4,
                               schedule=schedule,
                               departure_secs=400,
                               arrival_secs=400)

        self.trip2 = self.route1.AddTrip(schedule, "trip 2", trip_id='trip2')
        self.trip2.AddStopTime(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.AddTrip(schedule, "trip 3", trip_id='trip3')
 def testRouteIdConflict(self):
     problems = util.GetTestFailureProblemReporter(self)
     schedule = transitfeed.Schedule(problem_reporter=problems)
     route0 = schedule.AddRoute("0", "Long Name", "Bus")
     route1 = schedule.AddRoute("1", "", "Bus", route_id="1")
     route3 = schedule.AddRoute("3", "", "Bus", route_id="3")
     route_rand = schedule.AddRoute("R", "LNR", "Bus")
     route4 = schedule.AddRoute("4", "GooCar", "Bus")
     route_list = schedule.GetRouteList()
     route_list.sort(key=lambda r: r.route_short_name)
     self.assertEqual("0 1 3 4 R",
                      " ".join(r.route_short_name for r in route_list))
     self.assertMatchesRegex("0 1 3 4 \d{7,9}",
                             " ".join(r.route_id for r in route_list))
     self.assertEqual("Long Name,,,GooCar,LNR",
                      ",".join(r.route_long_name for r in route_list))
Esempio n. 16
0
  def runTest(self):
    problem_reporter = util.GetTestFailureProblemReporter(self)
    schedule = transitfeed.Schedule(problem_reporter=problem_reporter)
    route = schedule.AddRoute(short_name="10", long_name="", route_type="Bus")
    stop = schedule.AddStop(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.GetDefaultServicePeriod().service_id
    trip.trip_id = "SAMPLE_TRIP"
    schedule.AddTripObject(trip)

    # First stop must have time
    trip.AddStopTime(stop, arrival_secs=300, departure_secs=360)
    trip.AddStopTime(stop)
    trip.AddStopTime(stop, arrival_time="00:07:00", departure_time="00:07:30")
    trip.Validate(problem_reporter)
Esempio n. 17
0
    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.GetTestFailureProblemReporter(
            self, ("ExpirationDate", "UnrecognizedColumn"))
        schedule = self.MakeLoaderAndLoad(problems=load1_problems)
        saved_schedule_file = StringIO()
        schedule.WriteGoogleTransitFeed(saved_schedule_file)

        self.assertLoadAndCheckExtraValues(saved_schedule_file)
Esempio n. 18
0
    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.GetTestFailureProblemReporter(
            self, ("ExpirationDate", "UnrecognizedColumn"))
        schedule = self.MakeLoaderAndLoad(problems=load1_problems)
        saved_schedule_file = StringIO()
        schedule.WriteGoogleTransitFeed(saved_schedule_file)

        self.assertLoadAndCheckExtraValues(saved_schedule_file)