def test_init_trip(self): # prepare args = dict(trip_id='24104916_010117', trip_date=datetime.date(2017, 2, 2)) # exec trip = RealTimeArrivals.Trip(**args) # test self.assertEqual(trip.trip_date, args['trip_date']) self.assertEqual(trip.trip_id, args['trip_id'])
def test_stop_between_records(self): trip = RealTimeArrivals.Trip() trip.records = records[1:] trip._add_route_offset_in_meters(stops[-1].shape_dist_traveled) bef, aft = trip._get_before_and_after_records(stop=stops[2]) self.assertEqual(bef, records[1]) self.assertEqual(aft, records[2])
def test_trip_fill_set_route_offset_in_meters(self): # prepare args = dict(trip_id='24104916_010117', trip_date=datetime.date(2017, 2, 2)) trip = RealTimeArrivals.Trip(crud =crud_stub.CrudStub(), **args) # exec trip._fill_() for i in trip.records: self.assertTrue(i.route_offset_in_meters is not None)
def test_stop_after_records(self): trip = RealTimeArrivals.Trip() trip.records = records[1:] trip._add_route_offset_in_meters(1) try: trip._get_before_and_after_records(stop=stops[3]) except ValueError: return self.fail()
def test_trip_real_time_without_exception(self): # noinspection PyBroadException try: # prepare args = dict(trip_id='24104916_010117', trip_date=datetime.date(2017, 2, 2)) trip = RealTimeArrivals.Trip(crud =crud_stub.CrudStub(), **args) # exec trip._fill_() except Exception: self.fail("exception")
def test_trip_fill_set_records(self): # prepare args = dict(trip_id='24104916_010117', trip_date=datetime.date(2017, 2, 2)) trip = RealTimeArrivals.Trip(crud =crud_stub.CrudStub(), **args) # exec trip._fill_() expect = len({x['recorded_at_time'] for x in crud_stub.CrudStub.raw_records}) self.assertEqual(expect, len(trip.records))
def test_trip_fill_sort_records(self): # prepare args = dict(trip_id='24104916_010117', trip_date=datetime.date(2017, 2, 2)) trip = RealTimeArrivals.Trip(crud =crud_stub.CrudStub(), **args) # exec trip._fill_() for num, record in enumerate(trip.records[1:]): curr = record.route_offset prev = trip.records[num].route_offset self.assertTrue(curr > prev)
def test_trip_fill_sort_stops(self): # prepare args = dict(trip_id='24104916_010117', trip_date=datetime.date(2017, 2, 2)) trip = RealTimeArrivals.Trip(crud =crud_stub.CrudStub(), **args) # exec trip._fill_() for num, stop in enumerate(trip.stops[1:]): curr = stop.stop_sequence prev = trip.stops[num].stop_sequence self.assertTrue(curr > prev)
def test_all_records_AFTER_stop_get_no_real_time(self): # prepare trip = RealTimeArrivals.Trip(crud=StubRecordsAfter()) # exec trip._fill_() # trip.real_time() # test self.assertFalse(trip.real_times)
def get_relevant_trips_from_gtfs(self, date: datetime.date): sql = """SELECT trip_id_from_gtfs, date_trunc('day', max(recorded_at_time)) FROM siri_arrivals WHERE recorded_at_time between '{}' and '{}' GROUP BY trip_id_from_gtfs;""" with self.conn.cursor() as curs: curs.execute(sql.format(date, (date + datetime.timedelta(days=1)))) return [ RealTimeArrivals.Trip(row[0], row[1], self) for row in curs if row[0] ]
def test_init_record(self): # prepare args = {'vehicle_ref': '8761901', 'trip_id': '24104916_010117', 'recorded_at_time': datetime.datetime(2017, 2, 2, 16, 35, 48, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 'route_offset': 0.362768825871595} # exec record = RealTimeArrivals.Record(**args) self.assertEqual(record.vehicle_ref, args['vehicle_ref']) self.assertEqual(record.trip_id, args['trip_id']) self.assertEqual(record.recorded_at_time, args['recorded_at_time']) self.assertEqual(record.route_offset, args['route_offset'])
def test_stop_BETWEEN_records_get_real_time(self): # prepare trip = RealTimeArrivals.Trip(crud=StubRecordsRegular()) # exec trip._fill_() # trip.real_time() # test self.assertTrue(trip.real_times) self.assertTrue(trip.real_times[0].real_time == datetime.datetime(2017, 1, 1, 12, 45)) self.assertTrue(trip.real_times[0].speed == 2.0)
def test_init_stop(self): # prepare args = {'route_id': 10802, 'route_long_name': 'כניסה ראשית/הדסה עין כרם-ירושלים<->האוניברסיטה העברית הר הצופים-ירושלים-1א', 'trip_id': '24104916_010117', 'stop_sequence': 1, 'stop_id': 9863, 'route_short_name': '19', 'shape_dist_traveled': 0, 'arrival_time': '18:15:00'} # exec stop = RealTimeArrivals.Stop(**args) # test self.assertEqual(stop.route_id, args['route_id']) self.assertEqual(stop.trip_id, args['trip_id']) self.assertEqual(stop.stop_sequence, args['stop_sequence']) self.assertEqual(stop.stop_id, args['stop_id']) self.assertEqual(stop.route_id, args['route_id']) self.assertEqual(stop.shape_dist_traveled, args['shape_dist_traveled'])
def test_threadpool(self): pool = ThreadPoolExecutor() trip_ids = ["27782048_310717", "27782049_310717"] with Cruds.Connection(**conn_config) as c: trips = [RealTimeArrivals.Trip(trip_date=datetime.date(2017, 7, 31), trip_id= i, crud=Cruds.CrudPostgresql(c)) for i in trip_ids] futuers = [pool.submit(trip) for trip in trips] for i in futuers: print (i) i.result(120)
def read_stops(self, trip_id=None): return [RealTimeArrivals.Stop(stop_sequence=4, shape_dist_traveled=1000)]
RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 13), route_offset=1.8)] def read_stops(self, trip_id=None): return [RealTimeArrivals.Stop(stop_sequence=4, shape_dist_traveled=2500)] class StubRecordsRegular(Cruds.Crud): def read_records_from_siri(self, trip_id=None, trip_date=None): return [RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 12, 30), route_offset=0.5), RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 13), route_offset=1.5)] def read_stops(self, trip_id=None): return [RealTimeArrivals.Stop(stop_sequence=4, shape_dist_traveled=1000)] records = [RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 12), route_offset=0), RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 12, 30), route_offset=0.4), RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 13), route_offset=0.8)] stops = [RealTimeArrivals.Stop(stop_sequence=1, shape_dist_traveled=0), RealTimeArrivals.Stop(stop_sequence=2, shape_dist_traveled=500), RealTimeArrivals.Stop(stop_sequence=3, shape_dist_traveled=1500), RealTimeArrivals.Stop(stop_sequence=4, shape_dist_traveled=2500)] class TestGetBeforeAndAfter(unittest.TestCase): def test_stop_between_records(self): trip = RealTimeArrivals.Trip() trip.records = records[1:] trip._add_route_offset_in_meters(stops[-1].shape_dist_traveled)
def read_records_from_siri(self, trip_id=None, trip_date=None): return [RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 12, 30), route_offset=0.4), RealTimeArrivals.Record(recorded_at_time=datetime.datetime(2017, 1, 1, 13), route_offset=0.8)]