Skip to content
Snippets Groups Projects
test_osm_charge.py 2.01 KiB
Newer Older
markn92's avatar
markn92 committed
import os

markn92's avatar
markn92 committed
import pytest
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
from evrouting.osm.imports import read_osm, OSMGraph
markn92's avatar
markn92 committed
from evrouting.osm.profiles import car
markn92's avatar
markn92 committed
from evrouting.osm.routing import shortest_path
markn92's avatar
markn92 committed
from evrouting.graph_tools import CHARGING_COEFFICIENT_KEY
markn92's avatar
markn92 committed


markn92's avatar
markn92 committed
@pytest.fixture
def graph():
markn92's avatar
markn92 committed
    G = OSMGraph()
markn92's avatar
markn92 committed

    node_coordinates = [
        (51.7705832, 7.0002595),
        (51.7696529, 6.9568520)
    ]

    for n_id, coordinates in enumerate(node_coordinates):
        lat, lon = coordinates
        # Add two nodes, that exist in osm test map
        G.add_node(n_id, lat=lat, lon=lon)
markn92's avatar
markn92 committed
        G.insert_into_rtree(n_id)
markn92's avatar
markn92 committed
@pytest.fixture
def map_graph():
markn92's avatar
markn92 committed
    G = read_osm(os.path.join(os.path.dirname(__file__), 'static/map.osm'),
                 car)
markn92's avatar
markn92 committed
    yield G
    del G

markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
def test_insert_charging_stations_close(graph):
markn92's avatar
markn92 committed
    # Close two node 1
    S = [{"lon": 7.0002593, "lat": 51.7705832, "power": 22.0}]

markn92's avatar
markn92 committed
    graph.insert_charging_stations(S)
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
    assert graph.nodes[0][CHARGING_COEFFICIENT_KEY] == 22.0
    assert CHARGING_COEFFICIENT_KEY not in graph.nodes[1]
markn92's avatar
markn92 committed


markn92's avatar
markn92 committed
def test_insert_charging_stations_eq(graph):
markn92's avatar
markn92 committed
    # Close exactly at node 1
    S = [{"lon": 7.0002595, "lat": 51.7705832, "power": 22.0}]

markn92's avatar
markn92 committed
    graph.insert_charging_stations(S)
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
    assert graph.nodes[0][CHARGING_COEFFICIENT_KEY] == 22.0
    assert CHARGING_COEFFICIENT_KEY not in graph.nodes[1]
markn92's avatar
markn92 committed


def test_shortest_route(map_graph):
    s = (51.7769461, 6.9832152)
    t = (51.7796487, 6.9795230)

    route = [
        "1827268706",
        "1826594887",
        "4955446046",
        "4955446048",
        "34053450",
        "4955446051",
        "418009799"
    ]

    assert route == shortest_path(map_graph, s, t, car)
markn92's avatar
markn92 committed


def test_other_shortest_route(map_graph):
    s = (51.75344308292687, 6.943187713623048)
    t = (51.754452602619935, 6.958980560302735)

    route = [
        "1827268706",
        "1826594887",
        "4955446046",
        "4955446048",
        "34053450",
        "4955446051",
        "418009799"
    ]

    assert route == shortest_path(map_graph, s, t, car)