def test_vj_delay(self): """ send a mock kirin vj delay and test that the vj is not taken """ response = self.query_region(journey_basic_query + "&data_freshness=realtime") pt_response = self.query_region( 'vehicle_journeys/vjA?_current_datetime=20120614T1337') # with no cancellation, we have 2 journeys, one direct and one with the vj:A:0 eq_(get_arrivals(response), ['20120614T080222', '20120614T080435']) eq_(get_used_vj(response), [['vjA'], []]) pt_response = self.query_region('vehicle_journeys') eq_(len(pt_response['vehicle_journeys']), 4) # no disruption yet pt_response = self.query_region( 'vehicle_journeys/vjA?_current_datetime=20120614T1337') eq_(len(pt_response['disruptions']), 0) self.send_mock("vjA", "20120614", 'delayed', [("stop_point:stopB", tstamp("20120614T080224"), tstamp("20120614T080225"), 'cow on tracks'), ("stop_point:stopA", tstamp("20120614T080400"), tstamp("20120614T080400"))], disruption_id='vjA_delayed') # A new vj is created pt_response = self.query_region('vehicle_journeys') eq_(len(pt_response['vehicle_journeys']), 5) vj_ids = [vj['id'] for vj in pt_response['vehicle_journeys']] assert 'vjA:modified:0:vjA_delayed' in vj_ids def _check_train_delay_disruption(dis): is_valid_disruption(dis, chaos_disrup=False) eq_(dis['disruption_id'], 'vjA_delayed') eq_(dis['severity']['effect'], 'SIGNIFICANT_DELAYS') eq_(len(dis['impacted_objects']), 1) ptobj = dis['impacted_objects'][0]['pt_object'] eq_(ptobj['embedded_type'], 'trip') eq_(ptobj['id'], 'vjA') eq_(ptobj['name'], 'vjA') # for delay we should have detail on the impacted stops impacted_objs = get_not_null(dis['impacted_objects'][0], 'impacted_stops') assert len(impacted_objs) == 2 imp_obj1 = impacted_objs[0] eq_(get_valid_time(get_not_null(imp_obj1, 'amended_arrival_time')), _dt(h=8, m=2, s=24)) eq_( get_valid_time(get_not_null(imp_obj1, 'amended_departure_time')), _dt(h=8, m=2, s=25)) eq_(get_not_null(imp_obj1, 'cause'), 'cow on tracks') # for the moment we output 00000, but we should output the right base departure/arrival eq_(get_valid_time(get_not_null(imp_obj1, 'base_arrival_time')), _dt(8, 1, 0)) eq_(get_valid_time(get_not_null(imp_obj1, 'base_departure_time')), _dt(8, 1, 0)) imp_obj2 = impacted_objs[1] eq_(get_valid_time(get_not_null(imp_obj2, 'amended_arrival_time')), _dt(h=8, m=4, s=0)) eq_( get_valid_time(get_not_null(imp_obj2, 'amended_departure_time')), _dt(h=8, m=4, s=0)) eq_(imp_obj2['cause'], '') eq_(get_valid_time(get_not_null(imp_obj2, 'base_departure_time')), _dt(8, 1, 2)) eq_(get_valid_time(get_not_null(imp_obj2, 'base_arrival_time')), _dt(8, 1, 2)) # we should see the disruption pt_response = self.query_region( 'vehicle_journeys/vjA?_current_datetime=20120614T1337') eq_(len(pt_response['disruptions']), 1) _check_train_delay_disruption(pt_response['disruptions'][0]) new_response = self.query_region(journey_basic_query + "&data_freshness=realtime") eq_(get_arrivals(new_response), ['20120614T080435', '20120614T080520']) eq_(get_used_vj(new_response), [[], ['vjA:modified:0:vjA_delayed']]) pt_journey = new_response['journeys'][1] check_journey( pt_journey, Journey(sections=[ Section(departure_date_time='20120614T080208', arrival_date_time='20120614T080225', base_departure_date_time=None, base_arrival_date_time=None, stop_date_times=[]), Section(departure_date_time='20120614T080225', arrival_date_time='20120614T080400', base_departure_date_time='20120614T080100', base_arrival_date_time='20120614T080102', stop_date_times=[ SectionStopDT( departure_date_time='20120614T080225', arrival_date_time='20120614T080224', base_departure_date_time='20120614T080100', base_arrival_date_time='20120614T080100'), SectionStopDT( departure_date_time='20120614T080400', arrival_date_time='20120614T080400', base_departure_date_time='20120614T080102', base_arrival_date_time='20120614T080102') ]), Section(departure_date_time='20120614T080400', arrival_date_time='20120614T080520', base_departure_date_time=None, base_arrival_date_time=None, stop_date_times=[]) ])) # it should not have changed anything for the theoric new_base = self.query_region(journey_basic_query + "&data_freshness=base_schedule") eq_(get_arrivals(new_base), ['20120614T080222', '20120614T080435']) eq_(get_used_vj(new_base), [['vjA'], []]) # We send again the same disruption self.send_mock("vjA", "20120614", 'delayed', [("stop_point:stopB", tstamp("20120614T080224"), tstamp("20120614T080225"), 'cow on tracks'), ("stop_point:stopA", tstamp("20120614T080400"), tstamp("20120614T080400"))], disruption_id='vjA_delayed') # A new vj is created pt_response = self.query_region('vehicle_journeys') # No vj cleaning for the moment, vj nb SHOULD be 5, the first vj created for the first # disruption is useless eq_(len(pt_response['vehicle_journeys']), 6) pt_response = self.query_region( 'vehicle_journeys/vjA?_current_datetime=20120614T1337') eq_(len(pt_response['disruptions']), 1) _check_train_delay_disruption(pt_response['disruptions'][0]) # so the first real-time vj created for the first disruption should be deactivated new_response = self.query_region(journey_basic_query + "&data_freshness=realtime") eq_(get_arrivals(new_response), ['20120614T080435', '20120614T080520']) eq_(get_used_vj(new_response), [[], ['vjA:modified:1:vjA_delayed']]) # it should not have changed anything for the theoric new_base = self.query_region(journey_basic_query + "&data_freshness=base_schedule") eq_(get_arrivals(new_base), ['20120614T080222', '20120614T080435']) eq_(get_used_vj(new_base), [['vjA'], []]) # we then try to send a delay on another train. # we should not have lost the first delay self.send_mock("vjB", "20120614", 'delayed', [("stop_point:stopB", tstamp("20120614T180224"), tstamp("20120614T180225")), ("stop_point:stopA", tstamp("20120614T180400"), tstamp("20120614T180400"), "bob's in the place")]) pt_response = self.query_region( 'vehicle_journeys/vjA?_current_datetime=20120614T1337') eq_(len(pt_response['disruptions']), 1) _check_train_delay_disruption(pt_response['disruptions'][0]) # we should also have the disruption on vjB eq_( len( self.query_region( 'vehicle_journeys/vjB?_current_datetime=20120614T1337') ['disruptions']), 1)
def test_vj_delay(self): """ send a mock kirin vj delay and test that the vj is not taken """ response = self.query_region(journey_basic_query + "&data_freshness=realtime") # with no cancellation, we have 2 journeys, one direct and one with the vj:A:0 assert get_arrivals(response) == ['20120614T080222', '20120614T080436'] assert get_used_vj(response) == [['vjA'], []] # we have 2 departures and 1 disruption (linked to the first passage) departures = self.query_region("stop_points/stop_point:stopB/departures?_current_datetime=20120614T0800") assert len(departures['disruptions']) == 1 assert len(departures['departures']) == 2 assert departures['departures'][0]['stop_date_time']['departure_date_time'] == '20120614T080100' pt_response = self.query_region('vehicle_journeys') assert len(pt_response['vehicle_journeys']) == 5 # no disruption yet pt_response = self.query_region('vehicle_journeys/vjA?_current_datetime=20120614T1337') assert len(pt_response['disruptions']) == 0 self.send_mock("vjA", "20120614", 'modified', [UpdatedStopTime("stop_point:stopB", arrival=tstamp("20120614T080224"), departure=tstamp("20120614T080225"), arrival_delay=60 + 24, departure_delay=60+25, message='cow on tracks'), UpdatedStopTime("stop_point:stopA", arrival=tstamp("20120614T080400"), departure=tstamp("20120614T080400"), arrival_delay=3 * 60 + 58, departure_delay=3 * 60 + 58)], disruption_id='vjA_delayed') # A new vj is created pt_response = self.query_region('vehicle_journeys') assert len(pt_response['vehicle_journeys']) == 6 vj_ids = [vj['id'] for vj in pt_response['vehicle_journeys']] assert 'vjA:modified:0:vjA_delayed' in vj_ids def _check_train_delay_disruption(dis): is_valid_disruption(dis, chaos_disrup=False) assert dis['disruption_id'] == 'vjA_delayed' assert dis['severity']['effect'] == 'SIGNIFICANT_DELAYS' assert len(dis['impacted_objects']) == 1 ptobj = dis['impacted_objects'][0]['pt_object'] assert ptobj['embedded_type'] == 'trip' assert ptobj['id'] == 'vjA' assert ptobj['name'] == 'vjA' # for delay we should have detail on the impacted stops impacted_objs = get_not_null(dis['impacted_objects'][0], 'impacted_stops') assert len(impacted_objs) == 2 imp_obj1 = impacted_objs[0] assert get_valid_time(get_not_null(imp_obj1, 'amended_arrival_time')) == _dt(h=8, m=2, s=24) assert get_valid_time(get_not_null(imp_obj1, 'amended_departure_time')) == _dt(h=8, m=2, s=25) assert get_not_null(imp_obj1, 'cause') == 'cow on tracks' assert get_not_null(imp_obj1, 'departure_status') == 'delayed' assert get_not_null(imp_obj1, 'arrival_status') == 'delayed' assert get_not_null(imp_obj1, 'stop_time_effect') == 'delayed' assert get_valid_time(get_not_null(imp_obj1, 'base_arrival_time')) == _dt(8, 1, 0) assert get_valid_time(get_not_null(imp_obj1, 'base_departure_time')) == _dt(8, 1, 0) imp_obj2 = impacted_objs[1] assert get_valid_time(get_not_null(imp_obj2, 'amended_arrival_time')) == _dt(h=8, m=4, s=0) assert get_valid_time(get_not_null(imp_obj2, 'amended_departure_time')) == _dt(h=8, m=4, s=0) assert imp_obj2['cause'] == '' assert get_not_null(imp_obj1, 'stop_time_effect') == 'delayed' assert get_not_null(imp_obj1, 'departure_status') == 'delayed' assert get_not_null(imp_obj1, 'arrival_status') == 'delayed' assert get_valid_time(get_not_null(imp_obj2, 'base_departure_time')) == _dt(8, 1, 2) assert get_valid_time(get_not_null(imp_obj2, 'base_arrival_time')) == _dt(8, 1, 2) # we should see the disruption pt_response = self.query_region('vehicle_journeys/vjA?_current_datetime=20120614T1337') assert len(pt_response['disruptions']) == 1 _check_train_delay_disruption(pt_response['disruptions'][0]) new_response = self.query_region(journey_basic_query + "&data_freshness=realtime") assert get_arrivals(new_response) == ['20120614T080436', '20120614T080520'] assert get_used_vj(new_response) == [[], ['vjA:modified:0:vjA_delayed']] pt_journey = new_response['journeys'][1] check_journey(pt_journey, Journey(sections=[Section(departure_date_time='20120614T080208', arrival_date_time='20120614T080225', base_departure_date_time=None, base_arrival_date_time=None, stop_date_times=[]), Section(departure_date_time='20120614T080225', arrival_date_time='20120614T080400', base_departure_date_time='20120614T080100', base_arrival_date_time='20120614T080102', stop_date_times=[SectionStopDT(departure_date_time='20120614T080225', arrival_date_time='20120614T080224', base_departure_date_time='20120614T080100', base_arrival_date_time='20120614T080100'), SectionStopDT( departure_date_time='20120614T080400', arrival_date_time='20120614T080400', base_departure_date_time='20120614T080102', base_arrival_date_time='20120614T080102')]), Section(departure_date_time='20120614T080400', arrival_date_time='20120614T080520', base_departure_date_time=None, base_arrival_date_time=None, stop_date_times=[])])) # it should not have changed anything for the theoric new_base = self.query_region(journey_basic_query + "&data_freshness=base_schedule") assert get_arrivals(new_base) == ['20120614T080222', '20120614T080436'] assert get_used_vj(new_base), [['vjA'] == []] # we have one delayed departure departures = self.query_region("stop_points/stop_point:stopB/departures?_current_datetime=20120614T0800") assert len(departures['disruptions']) == 2 assert len(departures['departures']) == 2 assert departures['departures'][0]['stop_date_time']['departure_date_time'] == '20120614T080225' # Same as realtime except the departure date time departures = self.query_region("stop_points/stop_point:stopB/departures?_current_datetime=20120614T0800&data_freshness=base_schedule") assert len(departures['disruptions']) == 2 assert len(departures['departures']) == 2 assert departures['departures'][0]['stop_date_time']['departure_date_time'] == '20120614T080100' # We send again the same disruption self.send_mock("vjA", "20120614", 'modified', [UpdatedStopTime("stop_point:stopB", arrival=tstamp("20120614T080224"), departure=tstamp("20120614T080225"), arrival_delay=60 + 24, departure_delay=60+25, message='cow on tracks'), UpdatedStopTime("stop_point:stopA", arrival=tstamp("20120614T080400"), departure=tstamp("20120614T080400"), arrival_delay=3 * 60 + 58, departure_delay=3 * 60 + 58)], disruption_id='vjA_delayed') # A new vj is created, but a useless vj has been cleaned, so the number of vj does not change pt_response = self.query_region('vehicle_journeys') assert len(pt_response['vehicle_journeys']) == 6 pt_response = self.query_region('vehicle_journeys/vjA?_current_datetime=20120614T1337') assert len(pt_response['disruptions']) == 1 _check_train_delay_disruption(pt_response['disruptions'][0]) # so the first real-time vj created for the first disruption should be deactivated new_response = self.query_region(journey_basic_query + "&data_freshness=realtime") assert get_arrivals(new_response) == ['20120614T080436', '20120614T080520'] assert get_used_vj(new_response), [[] == ['vjA:modified:1:vjA_delayed']] # it should not have changed anything for the theoric new_base = self.query_region(journey_basic_query + "&data_freshness=base_schedule") assert get_arrivals(new_base) == ['20120614T080222', '20120614T080436'] assert get_used_vj(new_base), [['vjA'] == []] # we then try to send a delay on another train. # we should not have lost the first delay self.send_mock("vjB", "20120614", 'modified', [UpdatedStopTime("stop_point:stopB", tstamp("20120614T180224"), tstamp("20120614T180225"), arrival_delay=60 + 24, departure_delay=60 + 25,), UpdatedStopTime("stop_point:stopA", tstamp("20120614T180400"), tstamp("20120614T180400"), message="bob's in the place")]) pt_response = self.query_region('vehicle_journeys/vjA?_current_datetime=20120614T1337') assert len(pt_response['disruptions']) == 1 _check_train_delay_disruption(pt_response['disruptions'][0]) # we should also have the disruption on vjB assert len(self.query_region('vehicle_journeys/vjB?_current_datetime=20120614T1337')['disruptions']) == 1 ################################### # We now send a partial delete on B ################################### self.send_mock("vjA", "20120614", 'modified', [UpdatedStopTime("stop_point:stopB", arrival=tstamp("20120614T080100"), departure=tstamp("20120614T080100")), UpdatedStopTime("stop_point:stopA", arrival=tstamp("20120614T080102"), departure=tstamp("20120614T080102"), message='cow on tracks', arrival_skipped=True)], disruption_id='vjA_skip_A') # A new vj is created vjs = self.query_region('vehicle_journeys?_current_datetime=20120614T1337') assert len(vjs['vehicle_journeys']) == 7 vjA = self.query_region('vehicle_journeys/vjA?_current_datetime=20120614T1337') # we now have 2 disruption on vjA assert len(vjA['disruptions']) == 2 all_dis = {d['id']: d for d in vjA['disruptions']} assert 'vjA_skip_A' in all_dis dis = all_dis['vjA_skip_A'] is_valid_disruption(dis, chaos_disrup=False) assert dis['disruption_id'] == 'vjA_skip_A' assert dis['severity']['effect'] == 'DETOUR' assert len(dis['impacted_objects']) == 1 ptobj = dis['impacted_objects'][0]['pt_object'] assert ptobj['embedded_type'] == 'trip' assert ptobj['id'] == 'vjA' assert ptobj['name'] == 'vjA' # for delay we should have detail on the impacted stops impacted_objs = get_not_null(dis['impacted_objects'][0], 'impacted_stops') assert len(impacted_objs) == 2 imp_obj1 = impacted_objs[0] assert get_valid_time(get_not_null(imp_obj1, 'amended_arrival_time')) == _dt(8, 1, 0) assert get_valid_time(get_not_null(imp_obj1, 'amended_departure_time')) == _dt(8, 1, 0) assert get_not_null(imp_obj1, 'stop_time_effect') == 'unchanged' assert get_not_null(imp_obj1, 'arrival_status') == 'unchanged' assert get_not_null(imp_obj1, 'departure_status') == 'unchanged' assert get_valid_time(get_not_null(imp_obj1, 'base_arrival_time')) == _dt(8, 1, 0) assert get_valid_time(get_not_null(imp_obj1, 'base_departure_time')) == _dt(8, 1, 0) imp_obj2 = impacted_objs[1] assert 'amended_arrival_time' not in imp_obj2 assert get_not_null(imp_obj2, 'cause') == 'cow on tracks' assert get_not_null(imp_obj2, 'stop_time_effect') == 'deleted' # the stoptime is marked as deleted assert get_not_null(imp_obj2, 'arrival_status') == 'deleted' assert get_not_null(imp_obj2, 'departure_status') == 'unchanged' # the departure is not changed assert get_valid_time(get_not_null(imp_obj2, 'base_departure_time')) == _dt(8, 1, 2) assert get_valid_time(get_not_null(imp_obj2, 'base_arrival_time')) == _dt(8, 1, 2)