Exemple #1
0
def street_network_backends(app):
    from jormungandr.street_network.streetnetwork_backend_manager import StreetNetworkBackendManager

    if app.config['DISABLE_DATABASE']:
        return StreetNetworkBackendManager()
    else:
        from navitiacommon import models

        return StreetNetworkBackendManager(models.StreetNetworkBackend.all)
Exemple #2
0
def get_all_street_networks_db_test():
    manager = StreetNetworkBackendManager(sn_backends_getter_ok, -1)
    instance = FakeInstance()

    all_sn = manager.get_all_street_networks_db(instance)
    assert len(all_sn) == 2

    # So that Asgard and kraken are always in the same order
    all_sn_sorted = sorted(all_sn, key=lambda sn: sn.url)

    assert all_sn_sorted[0].url == "asgard.url"
    assert sorted(all_sn_sorted[0].modes) == sorted(["walking", "car"])

    assert all_sn_sorted[1].url == "kraken.url"
    assert all_sn_sorted[1].modes == ["bss"]
Exemple #3
0
 def __init__(
     self,
     disable_database,
     ridesharing_configurations=None,
     equipment_details_config=None,
     instance_equipment_providers=None,
 ):
     super(FakeInstance, self).__init__(
         context=None,
         name="instance",
         zmq_socket=None,
         street_network_configurations=[],
         ridesharing_configurations=ridesharing_configurations,
         instance_equipment_providers=[],
         realtime_proxies_configuration=[],
         zmq_socket_type=None,
         autocomplete_type='kraken',
         streetnetwork_backend_manager=StreetNetworkBackendManager(),
         external_service_provider_configurations=[],
     )
     self.disable_database = disable_database
     self.equipment_provider_manager = EquipmentProviderManager(
         equipment_details_config)
     self.equipment_provider_manager.init_providers(
         instance_equipment_providers)
def get_all_street_networks_db_test():
    manager = StreetNetworkBackendManager(sn_backends_getter_ok, -1)
    instance = FakeInstance()

    manager._can_connect_to_database = MagicMock(return_value=True)
    all_sn = manager.get_all_street_networks_db(instance)
    assert len(all_sn) == 2

    # So that Asgard and kraken are always in the same order
    all_sn_sorted = sorted(all_sn, key=lambda sn: sn.url)

    assert all_sn_sorted[0].url == "asgard.url"
    assert sorted(all_sn_sorted[0].modes) == sorted(
        ["walking", "car", "car_no_park"])

    assert all_sn_sorted[1].url == "kraken.url"
    assert all_sn_sorted[1].modes == ["bss"]
def get_street_network_db_test():
    manager = StreetNetworkBackendManager(sn_backends_getter_ok, -1)

    sn = manager.get_street_network_db("instance", "kraken")
    assert sn is not None
    assert sn.timeout == 2
    assert sn.url == 'kraken.url'

    sn = manager.get_street_network_db("instance", "asgard")
    assert sn is not None
    assert sn.timeout == 2
    assert sn.url == 'asgard.url'

    with pytest.raises(TechnicalError) as excinfo:
        sn = manager.get_street_network_db("instance", "plopi")
    assert (
        str(excinfo.value.data['message']) ==
        'impossible to find a streetnetwork module for instance instance with configuration plopi'
    )
    assert 'TechnicalError' == str(excinfo.typename)
 def __init__(self):
     super(FakeInstance, self).__init__(
         context=None,
         name="instance",
         zmq_socket=None,
         street_network_configurations=[],
         ridesharing_configurations=[],
         realtime_proxies_configuration=[],
         zmq_socket_type=None,
         autocomplete_type='kraken',
         instance_equipment_providers=[],
         streetnetwork_backend_manager=StreetNetworkBackendManager(),
     )
    def test_call_with_two_krakens(self):
        """
        Checks that in distributed mode with streetnetwork_backends in database
        There is no error when multiples krakens are up and we call one of them after an other
        """
        manager = StreetNetworkBackendManager(self.sn_backends_getter)

        i_manager.instances["main_routing_test"]._streetnetwork_backend_manager = manager
        i_manager.instances["min_nb_journeys_test"]._streetnetwork_backend_manager = manager

        self._call_and_check_journeys_on_coverage("main_routing_test", "stopA", "stopB", "20120614T080000")
        self._call_and_check_journeys_on_coverage(
            "min_nb_journeys_test", "stop_point:sa1:s1", "stop_point:sa3:s1", "20180309T080000"
        )
        self._call_and_check_journeys_on_coverage("main_routing_test", "stopB", "stopC", "20120614T080000")
def wrong_streetnetwork_backend_test():
    """
    Test that streetnetwork backends with wrong parameters aren't created
    """

    # Sn_backend has a class wrongly formatted
    manager = StreetNetworkBackendManager(sn_backends_getter_wrong_class, -1)
    manager._can_connect_to_database = MagicMock(return_value=True)
    manager._update_config("instance")
    assert not manager._streetnetwork_backends_by_instance_legacy
    assert not manager._streetnetwork_backends

    # No sn_backends available in db
    manager._sn_backends_getter = []
    manager._update_config("instance")
    assert not manager._streetnetwork_backends_by_instance_legacy
    assert not manager._streetnetwork_backends
def _init_and_create_backend_without_default(conf):
    sn_manager = StreetNetworkBackendManager()
    sn_manager._create_street_network_backends("instance", conf)
    return sn_manager.get_all_street_networks_legacy("instance")
def append_default_street_network_to_config_test():
    manager = StreetNetworkBackendManager(sn_backends_getter=None,
                                          update_interval=-1)

    # Kraken is used for all the modes.
    config_full_default = [
        {
            'args': {
                'street_network': {
                    'args': {
                        'timeout': 10
                    },
                    'class': 'jormungandr.street_network.Kraken',
                    'modes': [],
                }
            },
            'class': 'jormungandr.street_network.Taxi',
            'modes': ['taxi'],
        },
        {
            'args': {
                'street_network': {
                    'args': {
                        'timeout': 10
                    },
                    'class': 'jormungandr.street_network.Kraken',
                    'modes': [],
                }
            },
            'class': 'jormungandr.street_network.Ridesharing',
            'modes': ['ridesharing'],
        },
        {
            'args': {
                'timeout': 10
            },
            'class': 'jormungandr.street_network.Kraken',
            'modes': ['car', 'walking', 'bike', 'bss'],
        },
    ]

    response = manager._append_default_street_network_to_config(None)
    compare_list_of_dicts("class", response, config_full_default)

    # Asgard is used for car, Kraken for all the other modes.
    config_car_asgard = [{
        'class': 'jormungandr.street_network.Asgard',
        'modes': ['car']
    }]
    response = manager._append_default_street_network_to_config(
        config_car_asgard)

    config_asgard_plus_default = [
        {
            'class': 'jormungandr.street_network.Asgard',
            'modes': ['car']
        },
        {
            'args': {
                'street_network': {
                    'args': {
                        'timeout': 10
                    },
                    'class': 'jormungandr.street_network.Kraken',
                    'modes': [],
                }
            },
            'class': 'jormungandr.street_network.Taxi',
            'modes': ['taxi'],
        },
        {
            'args': {
                'street_network': {
                    'args': {
                        'timeout': 10
                    },
                    'class': 'jormungandr.street_network.Kraken',
                    'modes': [],
                }
            },
            'class': 'jormungandr.street_network.Ridesharing',
            'modes': ['ridesharing'],
        },
        {
            'args': {
                'timeout': 10
            },
            'class': 'jormungandr.street_network.Kraken',
            'modes': ['walking', 'bike', 'bss'],
        },
    ]
    compare_list_of_dicts("class", response, config_asgard_plus_default)

    # Surf is used for surf, Kraken for all the other modes.
    # Surf stay in the config but is not used.
    wrong_config = [{
        'class': 'jormungandr.street_network.Surf',
        'modes': ['surf']
    }]
    response = manager._append_default_street_network_to_config(wrong_config)

    wrong_plus_default_config = [
        {
            'class': 'jormungandr.street_network.Surf',
            'modes': ['surf']
        },
        {
            'args': {
                'street_network': {
                    'args': {
                        'timeout': 10
                    },
                    'class': 'jormungandr.street_network.Kraken',
                    'modes': [],
                }
            },
            'class': 'jormungandr.street_network.Taxi',
            'modes': ['taxi'],
        },
        {
            'args': {
                'street_network': {
                    'args': {
                        'timeout': 10
                    },
                    'class': 'jormungandr.street_network.Kraken',
                    'modes': [],
                }
            },
            'class': 'jormungandr.street_network.Ridesharing',
            'modes': ['ridesharing'],
        },
        {
            'args': {
                'timeout': 10
            },
            'class': 'jormungandr.street_network.Kraken',
            'modes': ['car', 'walking', 'bike', 'bss'],
        },
    ]
    compare_list_of_dicts("class", response, wrong_plus_default_config)
def streetnetwork_backend_manager_db_test():
    """
    Test that streetnetwork backends are created from db when conditions are met
    """
    manager = StreetNetworkBackendManager(sn_backends_getter_ok, -1)

    # 2 sn_backends defined in db are associated to the coverage
    # -> 2 sn_backends created
    manager._update_config("instance")

    assert not manager._streetnetwork_backends_by_instance_legacy
    assert len(manager._streetnetwork_backends) == 2
    assert 'kraken' in manager._streetnetwork_backends
    assert manager._streetnetwork_backends['kraken'].url == 'kraken.url'
    assert 'asgard' in manager._streetnetwork_backends
    assert manager._streetnetwork_backends['asgard'].url == 'asgard.url'

    manager_update = manager._last_update
    assert 'kraken' in manager._streetnetwork_backends_last_update
    kraken_update = manager._streetnetwork_backends_last_update['kraken']

    # Sn_backend already existing is updated
    manager._sn_backends_getter = sn_backends_getter_update
    manager._update_config("instance")
    assert manager._last_update > manager_update
    assert not manager._streetnetwork_backends_by_instance_legacy
    assert len(manager._streetnetwork_backends) == 2
    assert 'kraken' in manager._streetnetwork_backends
    assert manager._streetnetwork_backends['kraken'].url == 'kraken.url.UPDATE'
    assert 'kraken' in manager._streetnetwork_backends_last_update
    assert manager._streetnetwork_backends_last_update['kraken'] > kraken_update

    # Long update interval so sn_backend shouldn't be updated
    manager = StreetNetworkBackendManager(sn_backends_getter_ok, 600)
    manager._update_config("instance")
    assert not manager._streetnetwork_backends_by_instance_legacy
    assert len(manager._streetnetwork_backends) == 2
    assert 'kraken' in manager._streetnetwork_backends
    assert manager._streetnetwork_backends['kraken'].url == 'kraken.url'
    manager_update = manager._last_update

    manager.sn_backends_getter = sn_backends_getter_update
    manager._update_config("instance")
    assert manager._last_update == manager_update
    assert not manager._streetnetwork_backends_by_instance_legacy
    assert len(manager._streetnetwork_backends) == 2
    assert 'kraken' in manager._streetnetwork_backends
    assert manager._streetnetwork_backends['kraken'].url == 'kraken.url'