def test_ire_delayed_then_OK(mock_rabbitmq): """ We delay a stop, then the vj is back on time """ ire_96231 = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed() assert mock_rabbitmq.call_count == 1 ire_96231 = get_ire_data('train_96231_normal.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_normal() assert mock_rabbitmq.call_count == 2
def test_ire_trip_delayed_then_removal(mock_rabbitmq): """ post delayed stops then trip removal on the same trip """ ire_96231_delayed = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231_delayed) assert res == 'OK' ire_96231_trip_removal = get_ire_data('train_96231_trip_removal.xml') res = api_post('/ire', data=ire_96231_trip_removal) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_96231_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_ire_delayed_and_trip_removal_post(mock_rabbitmq): """ post delayed stops on one trip than trip removal on another """ ire_96231 = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' ire_6113 = get_ire_data('train_6113_trip_removal.xml') res = api_post('/ire', data=ire_6113) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed() check_db_ire_6113_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_save_bad_raw_ire(): """ send a bad formatted ire, the bad raw ire should be saved in db """ bad_ire = get_ire_data('bad_ire.xml') res = api_post('/ire', data=bad_ire, check=False) assert res[1] == 400 assert res[0]['message'] == 'Invalid arguments' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert RealTimeUpdate.query.first().status == 'KO' assert RealTimeUpdate.query.first().error == \ 'invalid xml, impossible to find "Train" in xml elt InfoRetard' assert RealTimeUpdate.query.first().raw_data == bad_ire
def test_ire_trip_removal_simple_post(mock_rabbitmq): """ simple trip removal post """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') res = api_post('/ire', data=ire_6113) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() assert mock_rabbitmq.call_count == 1
def test_ire_delayed_simple_post(mock_rabbitmq): """ simple delayed stops post """ ire_96231 = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed() assert mock_rabbitmq.call_count == 1
def test_ire_trip_removal_parity(mock_rabbitmq): """ simple parity trip removal post """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') ire_6113_14 = ire_6113.replace('<NumeroTrain>006113</NumeroTrain>', '<NumeroTrain>006113/14</NumeroTrain>') res = api_post('/ire', data=ire_6113_14) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() assert mock_rabbitmq.call_count == 1
def test_ire_two_trip_removal_one_post(mock_rabbitmq): """ post one ire trip removal on two trips (navitia mock returns 2 vj for 'JOHN' headsign) """ ire_JOHN_trip_removal = get_ire_data('train_JOHN_trip_removal.xml') res = api_post('/ire', data=ire_JOHN_trip_removal) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_JOHN_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 1
def test_ire_trip_removal_post_twice(mock_rabbitmq): """ double trip removal post """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') res = api_post('/ire', data=ire_6113) assert res == 'OK' res = api_post('/ire', data=ire_6113) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_ire_delayed_post_twice(mock_rabbitmq): """ double delayed stops post """ ire_96231 = get_ire_data('train_96231_delayed.xml') res = api_post('/ire', data=ire_96231) assert res == 'OK' res = api_post('/ire', data=ire_96231) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 2 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed() # the rabbit mq has to have been called twice assert mock_rabbitmq.call_count == 2
def test_ire_partial_removal(mock_rabbitmq): """ the trip 840427 has been partialy deleted Normally there are 7 stops in this VJ, but 2 (Bar-sur-Aube and Vendeuvre) have been removed """ ire_080427 = get_ire_data('train_840427_partial_removal.xml') res = api_post('/ire', data=ire_080427) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 7 db_trip_partial_removed = TripUpdate.find_by_dated_vj( 'OCE:SN840427F03001', datetime.datetime(2017, 3, 18, 13, 05, tzinfo=utc))
def test_ire_trip_without_any_motifexterne(mock_rabbitmq): """ a trip with a parity has been impacted, but the ExternModif is missing, the IRE should still be acceptable """ ire_96231 = get_ire_data('train_96231_delayed.xml') # Removing MotifExterne ire_96231_without_MotifExterne = ire_96231.replace('<MotifExterne>Affluence exceptionnelle de voyageurs</MotifExterne>', '') res = api_post('/ire', data=ire_96231_without_MotifExterne) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 6 check_db_ire_96231_delayed(motif_externe_is_null=True) assert mock_rabbitmq.call_count == 1
def test_ire_trip_with_parity_one_unknown_vj(mock_rabbitmq): """ a trip with a parity has been impacted, but the train 6112 is not known by navitia there should be only the train 6113 impacted """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') ire_6112_13 = ire_6113.replace('<NumeroTrain>006113</NumeroTrain>', '<NumeroTrain>006112/3</NumeroTrain>') res = api_post('/ire', data=ire_6112_13) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() assert mock_rabbitmq.call_count == 1
def test_train_trip_removal(mock_navitia_fixture): """ test the import of train_6113_trip_removal.xml """ input_train_trip_removed = get_ire_data('train_6113_trip_removal.xml') with app.app_context(): rt_update = model.RealTimeUpdate(input_train_trip_removed, connector='ire') trip_updates = KirinModelBuilder(dumb_nav_wrapper()).build(rt_update) rt_update.trip_updates = trip_updates db.session.add(rt_update) db.session.commit() assert len(trip_updates) == 1 trip_up = trip_updates[0] assert trip_up.vj.navitia_id == 'OCETGV-87686006-87751008-2:25768' assert trip_up.vj_id == trip_up.vj.id assert trip_up.status == 'delete' # full trip removal : no stop_time to precise assert len(trip_up.stop_time_updates) == 0
def test_train_trip_removal(mock_navitia_fixture): """ test the import of train_6113_trip_removal.xml """ input_train_trip_removed = get_ire_data('train_6113_trip_removal.xml') with app.app_context(): rt_update = model.RealTimeUpdate(input_train_trip_removed, connector='ire') trip_updates = KirinModelBuilder(dumb_nav_wrapper()).build(rt_update) rt_update.trip_updates = trip_updates db.session.add(rt_update) db.session.commit() assert len(trip_updates) == 1 trip_up = trip_updates[0] assert trip_up.vj.navitia_trip_id == 'trip:OCETGV-87686006-87751008-2:25768' assert trip_up.vj_id == trip_up.vj.id assert trip_up.status == 'delete' # full trip removal : no stop_time to precise assert len(trip_up.stop_time_updates) == 0
def test_ire_trip_with_parity(mock_rabbitmq): """ a trip with a parity has been impacted, there should be 2 VJ impacted """ ire_6113 = get_ire_data('train_6113_trip_removal.xml') ire_6113_14 = ire_6113.replace('<NumeroTrain>006113</NumeroTrain>', '<NumeroTrain>006113/4</NumeroTrain>') res = api_post('/ire', data=ire_6113_14) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 # there should be 2 trip updated, # - trip:OCETGV-87686006-87751008-2:25768-2 for the headsign 6114 # - trip:OCETGV-87686006-87751008-2:25768 for the headsign 6113 assert len(TripUpdate.query.all()) == 2 assert len(StopTimeUpdate.query.all()) == 0 check_db_ire_6113_trip_removal() check_db_ire_6114_trip_removal() assert mock_rabbitmq.call_count == 1
def test_train_delayed(mock_navitia_fixture): """ test the import of train_96231_delayed.xml """ input_train_delayed = get_ire_data('train_96231_delayed.xml') with app.app_context(): rt_update = model.RealTimeUpdate(input_train_delayed, connector='ire') trip_updates = KirinModelBuilder(dumb_nav_wrapper()).build(rt_update) # we associate the trip_update manually for sqlalchemy to make the links rt_update.trip_updates = trip_updates db.session.add(rt_update) db.session.commit() assert len(trip_updates) == 1 trip_up = trip_updates[0] assert trip_up.vj.navitia_id == 'OCETrainTER-87212027-85000109-3:11859' assert trip_up.vj_id == trip_up.vj.id assert trip_up.status == 'update' # 5 stop times must have been created assert len(trip_up.stop_time_updates) == 5
def test_train_delayed(mock_navitia_fixture): """ test the import of train_96231_delayed.xml """ input_train_delayed = get_ire_data('train_96231_delayed.xml') with app.app_context(): rt_update = model.RealTimeUpdate(input_train_delayed, connector='ire') trip_updates = KirinModelBuilder(dumb_nav_wrapper()).build(rt_update) # we associate the trip_update manually for sqlalchemy to make the links rt_update.trip_updates = trip_updates db.session.add(rt_update) db.session.commit() assert len(trip_updates) == 1 trip_up = trip_updates[0] assert trip_up.vj.navitia_trip_id == 'trip:OCETrainTER-87212027-85000109-3:11859' assert trip_up.vj_id == trip_up.vj.id assert trip_up.status == 'update' # 5 stop times must have been created assert len(trip_up.stop_time_updates) == 5 # first stop time should be 'gare de Sélestat' st = trip_up.stop_time_updates[0] assert st.id assert st.stop_id == 'stop_point:OCE:SP:TrainTER-87214056' # the arrival has no EcartExterne in the IRE data, so the status is 'none' assert st.arrival is None # not computed yet assert st.arrival_delay is None assert st.arrival_status == 'none' assert st.departure is None assert st.departure_delay == timedelta(minutes=15) assert st.departure_status == 'update' assert st.message == 'Affluence exceptionnelle de voyageurs' # second should be 'gare de Colmar' st = trip_up.stop_time_updates[1] assert st.id assert st.stop_id == 'stop_point:OCE:SP:TrainTER-87182014' assert st.arrival is None assert st.arrival_delay == timedelta(minutes=15) assert st.arrival_status == 'update' assert st.departure is None assert st.departure_delay == timedelta(minutes=15) assert st.departure_status == 'update' assert st.message == 'Affluence exceptionnelle de voyageurs' # last should be 'gare de Basel-SBB' st = trip_up.stop_time_updates[-1] assert st.id assert st.stop_id == 'stop_point:OCE:SP:TrainTER-85000109' assert st.arrival is None assert st.arrival_delay == timedelta(minutes=15) assert st.arrival_status == 'update' # no departure in ire since it's the last (thus the departure will be before the arrival) assert st.departure is None assert st.departure_delay is None assert st.departure_status == 'none' assert st.message == 'Affluence exceptionnelle de voyageurs'
def test_ire_partial_removal(mock_rabbitmq): """ the trip 840427 has been partialy deleted Normally there are 7 stops in this VJ, but 2 (Bar-sur-Aube and Vendeuvre) have been removed """ ire_080427 = get_ire_data('train_840427_partial_removal.xml') res = api_post('/ire', data=ire_080427) assert res == 'OK' with app.app_context(): assert len(RealTimeUpdate.query.all()) == 1 assert len(TripUpdate.query.all()) == 1 assert len(StopTimeUpdate.query.all()) == 7 db_trip_partial_removed = TripUpdate.find_by_dated_vj('OCE:SN840427F03001', datetime.date(2017, 3, 18)) assert db_trip_partial_removed assert db_trip_partial_removed.vj.navitia_trip_id == 'OCE:SN840427F03001' assert db_trip_partial_removed.vj.circulation_date == datetime.date(2017, 3, 18) assert db_trip_partial_removed.vj_id == db_trip_partial_removed.vj.id assert db_trip_partial_removed.status == 'update' # 7 stop times must have been created assert len(db_trip_partial_removed.stop_time_updates) == 7 # the first stop have not been changed first_st = db_trip_partial_removed.stop_time_updates[0] assert first_st.stop_id == 'stop_point:OCE:SP:TrainTER-87713040' assert first_st.arrival_status == 'none' assert first_st.departure_status == 'none' assert first_st.message is None for s in db_trip_partial_removed.stop_time_updates[0:3]: assert s.arrival_status == 'none' assert s.departure_status == 'none' assert s.message is None # the stops Chaumont, Bar-sur-Aube, Vendeuvre and Troyes should have been marked as deleted # (even if Chaumont and Vendeuvre were in a 'PRDebut'/'PRFin' tag bar_st = db_trip_partial_removed.stop_time_updates[3] assert bar_st.stop_id == 'stop_point:OCE:SP:TrainTER-87142000' # Chaumont assert bar_st.arrival_status == 'none' # the train still arrives in this stop assert bar_st.departure_status == 'delete' assert bar_st.message == u"Défaut d'alimentation électrique" bar_st = db_trip_partial_removed.stop_time_updates[4] assert bar_st.stop_id == 'stop_point:OCE:SP:TrainTER-87118299' # Bar-sur-Aube assert bar_st.arrival_status == 'delete' assert bar_st.departure_status == 'delete' assert bar_st.message == u"Défaut d'alimentation électrique" bar_st = db_trip_partial_removed.stop_time_updates[5] assert bar_st.stop_id == 'stop_point:OCE:SP:TrainTER-87118257' # Vendeuvre assert bar_st.arrival_status == 'delete' assert bar_st.departure_status == 'delete' assert bar_st.message == u"Défaut d'alimentation électrique" bar_st = db_trip_partial_removed.stop_time_updates[6] assert bar_st.stop_id == 'stop_point:OCE:SP:TrainTER-87118000' # Troyes assert bar_st.arrival_status == 'delete' assert bar_st.departure_status == 'none' # the train still does not leave from this stop assert bar_st.message == u"Défaut d'alimentation électrique" assert db_trip_partial_removed.contributor == 'realtime.ire' assert mock_rabbitmq.call_count == 1