def test_duplicate_update(self):
        """Test whether duplicates are actually updated or re-inserted."""

        initial = self.from_file("tests/data/schedule_initial.xml")
        duplicate = self.from_file("tests/data/schedule_duplicate.xml")

        # just to be sure
        assert(initial.uid == duplicate.uid)
        assert(initial.rid == duplicate.rid)

        # force messages directly to listener
        listener = MyListener(None, None)

        listener.on_schedule_message(initial)

        res = Schedule.select().where(
            (Schedule.uid==initial.uid) & (Schedule.rid==initial.rid)
        )

        assert(res.count() == 1)
        s = res[0]
        assert(s.uid == initial.uid)
        assert(s.rid == initial.rid)
        assert(s.toc_code == initial.toc_code)

        cps = CallingPoint.select().where(
            CallingPoint.schedule==s
        ).order_by(CallingPoint.id.asc())

        assert(cps.count() == 2)
        destination_cp = cps[1]
        assert(destination_cp.public_arrival == datetime.time(
            hour=10,
            minute=14))

        listener.on_schedule_message(duplicate)

        res = Schedule.select().where(
            (Schedule.uid==initial.uid) & (Schedule.rid==initial.rid)
        )

        assert(res.count() == 1)
        s_after = res[0]
        assert(s_after.uid == initial.uid) # same uid
        assert(s_after.rid == initial.rid) # same rid
        assert(s_after.toc_code == duplicate.toc_code) # different toc

        # get calling points
        cps = CallingPoint.select().where(
            CallingPoint.schedule == s_after
        ).order_by(CallingPoint.id.asc())

        assert(cps.count() == 2) # should be only two
        destination_cp = cps[1]
        assert(destination_cp.public_arrival == datetime.time(
            hour=10,
            minute=17)) # updated time
Beispiel #2
0
    def add_schedule_from_buffer(self, journey_buffer):
        r = pp.CreateFromDocument(journey_buffer)
        assert(None is r.sR)
        assert(None is not r.uR)
        assert(1 == len(r.uR.schedule))

        m = ScheduleXMLMessageFactory.build(r.uR.schedule[0], r, journey_buffer)

        ###### This code below checks that there are no schedules already stored
        # # We try to find a schedule, and replace it if we do
        # found = (Schedule
        #         .select()
        #         .where(
        #             Schedule.uid == m.uid,
        #             Schedule.rid == m.rid
        #         ))

        # count = found.count()
        # if count > 0:
        #     assert(count == 1)
        #     s = found[0]

        #     # Removing all relevant calling points
        #     CallingPoint.delete().where(
        #         CallingPoint.schedule == s
        #     ).execute()

        # else:
        #     s = Schedule()
        #     s.uid = m.uid
        #     s.rid = m.rid


        # Script assumes new schedules added, and overrides any existing
        s = Schedule()
        s.uid = m.uid
        s.rid = m.rid


        s.headcode = m.headcode
        s.start_date = m.start_date
        s.toc_code = m.toc_code
        s.category = m.category
        s.status = m.status
        s.active = m.active
        s.deleted = m.deleted
        s.cancel_tiploc = m.cancel_reason_tiploc
        s.cancel_code = m.cancel_reason_code
        s.cancel_near = m.cancel_reason_near
        s.save()

        for o in m.all_points:
            p = CallingPoint()
            p.tiploc = o.tiploc
            p.schedule = s
            p.activity_codes = o.planned_activity_codes
            p.cancelled = o.cancelled
            p.false_tiploc = o.false_tiploc
            p.route_delay = o.route_delay
            p.working_arrival = o.raw_working_arrival_time
            p.working_pass = o.raw_working_pass_time
            p.working_departure = o.raw_working_departure_time
            p.public_arrival = o.raw_public_arrival_time
            p.public_departure = o.raw_public_departure_time
            p.type = str(type(o))
            p.save()