File size: 2,223 Bytes
f00d669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a502dd
 
 
 
 
 
 
f00d669
1a502dd
f00d669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Brick 0 tests: plain routing, geocoding, and bounds handling."""
from __future__ import annotations

import pytest

from discoverroute import config
from discoverroute.routing import graph as g
from discoverroute.routing.graph import RouteError

# Known Paris points.
REPUBLIQUE = (48.8674, 2.3636)
LUXEMBOURG = (48.8462, 2.3372)
LONDON = (51.5074, -0.1278)  # out of bounds

graph_available = pytest.mark.skipif(
    not config.GRAPH_WALK_PATH.exists(),
    reason="Paris graph not built (run: python -m discoverroute.data.build_graph)",
)


def test_latlon_parsing():
    assert g._try_parse_latlon("48.8674, 2.3636") == (48.8674, 2.3636)
    assert g._try_parse_latlon("48.8674; 2.3636") == (48.8674, 2.3636)
    assert g._try_parse_latlon("not a point") is None


def test_in_paris_bounds():
    assert config.in_paris(*REPUBLIQUE)
    assert config.in_paris(*LUXEMBOURG)
    assert not config.in_paris(*LONDON)


def test_geocode_accepts_world_latlon():
    # World support: an explicit lat/lon anywhere on Earth resolves (no network).
    lat, lon = g.geocode_point(f"{LONDON[0]}, {LONDON[1]}")
    assert abs(lat - LONDON[0]) < 1e-6 and abs(lon - LONDON[1]) < 1e-6


def test_geocode_rejects_invalid_latlon():
    with pytest.raises(RouteError):
        g.geocode_point("123.0, 999.0")  # out of valid coordinate range


def test_geocode_empty():
    with pytest.raises(RouteError):
        g.geocode_point("")


def test_speed_model():
    # walk is slower than bike => walking takes longer for the same distance
    assert config.speed_ms("walk") < config.speed_ms("bike")


@graph_available
def test_plain_route_connected():
    graph = g.load_graph()
    route = g.plain_route(graph, *REPUBLIQUE, *LUXEMBOURG, mode="walk")
    assert route.distance_m > 0
    assert route.time_s > 0
    assert len(route.coords) >= 2
    # The straight-line distance is ~2.4 km; a real walk path is longer, not shorter.
    assert route.distance_m >= 2000


@graph_available
def test_bike_faster_than_walk_same_route():
    graph = g.load_graph()
    walk = g.plain_route(graph, *REPUBLIQUE, *LUXEMBOURG, mode="walk")
    bike = g.plain_route(graph, *REPUBLIQUE, *LUXEMBOURG, mode="bike")
    assert bike.time_s < walk.time_s