コード例 #1
0
ファイル: test_dao.py プロジェクト: vesavlad/gtfslib-python
    def test_trip(self):
        dao = Dao()
        f1 = FeedInfo("F1")
        a1 = Agency("F1",
                    "A1",
                    "Agency 1",
                    agency_url="http://www.agency.fr/",
                    agency_timezone="Europe/Paris")
        r1 = Route("F1",
                   "R1",
                   "A1",
                   3,
                   route_short_name="R1",
                   route_long_name="Route 1")
        c1 = Calendar("F1", "C1")
        c1.dates = [
            d for d in CalendarDate.range(
                CalendarDate.ymd(2016, 1, 1),
                CalendarDate.ymd(2016, 1, 31).next_day())
        ]
        s1 = Stop("F1", "S1", "Stop 1", 45.0, 0.0)
        s2 = Stop("F1", "S2", "Stop 2", 45.1, 0.1)
        s3 = Stop("F1", "S3", "Stop 3", 45.2, 0.2)
        t1 = Trip("F1", "T1", "R1", "C1")
        t1.direction_id = 0
        t11 = StopTime("F1", "T1", "S1", 0, 28800, 28800, 0.0)
        t12 = StopTime("F1", "T1", "S2", 1, 29400, 29400, 0.0)
        t13 = StopTime("F1", "T1", "S3", 2, 30000, 30000, 0.0)
        t2 = Trip("F1", "T2", "R1", "C1")
        t2.direction_id = 1
        # Order is not important for now
        t2.stop_times.append(StopTime(None, None, "S1", 1, 31000, 31000, 0.0))
        t2.stop_times.append(StopTime(None, None, "S2", 0, 30600, 30600, 0.0))

        dao.add_all([f1, a1, r1, c1, s1, s2, s3, t1, t11, t12, t13, t2])
        # Commit is needed to re-order stop times of T2
        dao.commit()

        cal = dao.calendar("C1", feed_id="F1")
        for trip in cal.trips:
            self.assertTrue(trip.calendar.service_id == "C1")
            for stoptime in trip.stop_times:
                self.assertTrue(stoptime.trip.calendar.service_id == "C1")

        stop = dao.stop("S2", feed_id="F1")
        for stoptime in stop.stop_times:
            self.assertTrue(stoptime.stop.stop_id == "S2")
            self.assertTrue(stoptime.trip.trip_id.startswith("T"))

        trip = dao.trip("T1", feed_id="F1")
        self.assertTrue(len(trip.stop_times) == 3)

        trip = dao.trip("T2", feed_id="F1")
        self.assertTrue(len(trip.stop_times) == 2)

        for trip in dao.trips(prefetch_stop_times=True):
            last_stop_seq = -1
            for stoptime in trip.stop_times:
                self.assertTrue(stoptime.stop_sequence > last_stop_seq)
                last_stop_seq = stoptime.stop_sequence

        for trip in dao.trips():
            for stoptime1, stoptime2 in trip.hops():
                self.assertTrue(stoptime1.trip == stoptime2.trip)
                self.assertTrue(stoptime1.stop_sequence +
                                1 == stoptime2.stop_sequence)

        trips = list(dao.trips(fltr=Trip.direction_id == 0))
        self.assertTrue(len(trips) == 1)
        trips = list(dao.trips(fltr=Trip.direction_id == 1))
        self.assertTrue(len(trips) == 1)