def proxy_only_database_test():
    def provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.siri.Siri',
            "args": {
                "requestor_ref": "AA_Siri_TAO",
                "service_url": "http://siri.com",
                "timeout": 1
            },
        }
        return [ExternalService(id='Siri_TAO', json=json)]

    manager = RealtimeProxyManager([],
                                   MockInstance(),
                                   providers_getter=provider)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 0
    assert len(manager.get_all_realtime_proxies()) == 0

    manager.update_config()

    assert len(manager._realtime_proxies_legacy) == 0

    assert len(manager._realtime_proxies) == 1
    assert 'Siri_TAO' in manager._realtime_proxies
    assert len(manager.get_all_realtime_proxies()) == 1
def one_proxy_conf_file_and_database_different_id_test():
    def provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.siri.Siri',
            "args": {
                "requestor_ref": "AA_Siri_TAO",
                "service_url": "http://siri.com",
                "timeout": 1
            },
        }
        return [ExternalService(id='Siri_TAO', json=json)]

    config = [{
        "object_id_tag": "source",
        "class": "jormungandr.realtime_schedule.sytral.Sytral",
        "args": {
            "service_url": "http://sytralrt.com",
            "timeout": 10
        },
        "id": "SytralRT",
    }]
    manager = RealtimeProxyManager(config,
                                   MockInstance(),
                                   providers_getter=provider)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 1
    assert 'SytralRT' in manager._realtime_proxies_legacy
    assert len(manager.get_all_realtime_proxies()) == 1

    manager.update_config()

    assert len(manager._realtime_proxies_legacy) == 1
    assert 'SytralRT' in manager._realtime_proxies_legacy

    assert len(manager._realtime_proxies) == 1
    assert 'Siri_TAO' in manager._realtime_proxies

    assert len(manager.get_all_realtime_proxies()) == 2
def multi_proxy_conf_file_and_database_test():
    manager = RealtimeProxyManager(
        All_realtime_proxies,
        MockInstance(),
        providers_getter=mock_get_realtime_proxies_from_db)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 5
    for rt_proxy in [
            "clever_age_TBC", "SytralRT", "Timeo_Amelys", "Siri_TAO",
            "siri_stif"
    ]:
        assert rt_proxy in manager._realtime_proxies_legacy

    manager.update_config()

    assert len(manager._realtime_proxies) == 5
    for rt_proxy in [
            "clever_age_TBC", "SytralRT", "Timeo_Amelys", "Siri_TAO",
            "siri_stif"
    ]:
        assert rt_proxy in manager._realtime_proxies

    assert len(manager._realtime_proxies_legacy) == 0
def update_proxy_database_test():
    def provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.siri.Siri',
            "args": {
                "requestor_ref": "AA_Siri_TAO",
                "service_url": "http://siri.com",
                "timeout": 1
            },
        }
        external_service = ExternalService(id='Siri_TAO', json=json)
        external_service.created_at = datetime.datetime.utcnow(
        ) - datetime.timedelta(hours=2)
        return [external_service]

    manager = RealtimeProxyManager([],
                                   MockInstance(),
                                   providers_getter=provider,
                                   update_interval=-1)
    assert len(manager._realtime_proxies) == 0
    assert len(manager._realtime_proxies_legacy) == 0
    assert len(manager.get_all_realtime_proxies()) == 0

    manager.update_config()

    assert len(manager._realtime_proxies_legacy) == 0

    assert len(manager._realtime_proxies) == 1
    assert 'Siri_TAO' in manager._realtime_proxies
    assert manager._realtime_proxies[
        "Siri_TAO"].service_url == 'http://siri.com'
    assert manager._realtime_proxies["Siri_TAO"].timeout == 1
    assert len(manager.get_all_realtime_proxies()) == 1

    # Update Url and timeout values
    def update_provider():
        json = {
            "navitia_service": 'realtime_proxies',
            "klass": 'jormungandr.realtime_schedule.siri.Siri',
            "args": {
                "requestor_ref": "AA_Siri_TAO",
                "service_url": "http://new_url_siri.com",
                "timeout": 10
            },
        }
        external_service = ExternalService(id='Siri_TAO', json=json)
        external_service.updated_at = datetime.datetime.utcnow()
        return [external_service]

    manager._realtime_proxies_getter = update_provider
    manager.update_config()

    assert len(manager._realtime_proxies_legacy) == 0

    assert len(manager._realtime_proxies) == 1
    assert 'Siri_TAO' in manager._realtime_proxies
    assert manager._realtime_proxies[
        "Siri_TAO"].service_url == 'http://new_url_siri.com'
    assert manager._realtime_proxies["Siri_TAO"].timeout == 10
    assert len(manager.get_all_realtime_proxies()) == 1