104 lines
3.7 KiB
Python
104 lines
3.7 KiB
Python
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture
|
|
def client(tmp_path, monkeypatch, fixtures):
|
|
from app import config, deps
|
|
|
|
monkeypatch.setattr(config.settings, "data_dir", str(tmp_path))
|
|
deps.get_store.cache_clear()
|
|
deps.get_llm.cache_clear()
|
|
deps.get_elevation.cache_clear()
|
|
deps.get_offline_maps.cache_clear()
|
|
|
|
from app.main import app
|
|
|
|
with TestClient(app) as c:
|
|
yield c
|
|
deps.get_store.cache_clear()
|
|
|
|
|
|
def test_health(client):
|
|
resp = client.get("/api/health")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["status"] == "ok"
|
|
|
|
|
|
def test_upload_list_get_export_delete(client, fixtures):
|
|
resp = client.post(
|
|
"/api/routes/upload",
|
|
files={"file": ("gaia.gpx", (fixtures / "gaia.gpx").read_bytes(), "application/gpx+xml")},
|
|
)
|
|
assert resp.status_code == 200, resp.text
|
|
route = resp.json()
|
|
assert route["source"] == "gaia"
|
|
rid = route["id"]
|
|
|
|
assert len(client.get("/api/routes").json()) == 1
|
|
assert client.get(f"/api/routes/{rid}").json()["name"] == "Windrock Loop"
|
|
|
|
gpx = client.get(f"/api/routes/{rid}/export?format=gpx")
|
|
assert gpx.status_code == 200
|
|
assert "grade_pct" in gpx.text
|
|
kml = client.get(f"/api/routes/{rid}/export?format=kml")
|
|
assert "LineString" in kml.text
|
|
|
|
assert client.delete(f"/api/routes/{rid}").status_code == 200
|
|
assert client.get(f"/api/routes/{rid}").status_code == 404
|
|
|
|
|
|
def test_upload_rejects_non_gpx(client):
|
|
resp = client.post("/api/routes/upload", files={"file": ("x.txt", b"hi", "text/plain")})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_guide_save_and_link(client, fixtures):
|
|
rid = client.post(
|
|
"/api/routes/upload",
|
|
files={"file": ("onx.gpx", (fixtures / "onx.gpx").read_bytes(), "application/gpx+xml")},
|
|
).json()["id"]
|
|
|
|
guide = client.post("/api/trails", json={"name": "Ridge guide", "difficulty": "hard"}).json()
|
|
linked = client.put(f"/api/routes/{rid}/trail-guide", json={"trail_guide_id": guide["id"]})
|
|
assert linked.json()["trail_guide_id"] == guide["id"]
|
|
|
|
missing = client.put(f"/api/routes/{rid}/trail-guide", json={"trail_guide_id": "nope"})
|
|
assert missing.status_code == 404
|
|
|
|
|
|
def test_generate_trail_card(client, fixtures):
|
|
rid = client.post(
|
|
"/api/routes/upload",
|
|
files={"file": ("gaia.gpx", (fixtures / "gaia.gpx").read_bytes(), "application/gpx+xml")},
|
|
).json()["id"]
|
|
resp = client.post(f"/api/routes/{rid}/generate/trail_card")
|
|
assert resp.status_code == 200
|
|
assert resp.json()["data"]["route_name"] == "Windrock Loop"
|
|
# weather overlay requires cached weather -> 409
|
|
resp = client.post(f"/api/routes/{rid}/generate/weather_overlay")
|
|
assert resp.status_code == 409
|
|
assert client.post(f"/api/routes/{rid}/generate/bogus").status_code == 404
|
|
|
|
|
|
def test_offline_pack_estimate_and_cap(client):
|
|
body = {"name": "t", "bbox": [-84.4, 36.1, -84.3, 36.2], "min_zoom": 10, "max_zoom": 12,
|
|
"layers": ["base"]}
|
|
est = client.post("/api/offline-maps/estimate", json=body).json()
|
|
assert est["tile_count"] > 0
|
|
huge = dict(body, bbox=[-90.0, 30.0, -80.0, 40.0], min_zoom=0, max_zoom=16)
|
|
resp = client.post("/api/offline-maps", json=huge)
|
|
assert resp.status_code == 400
|
|
assert "cap" in resp.json()["detail"]
|
|
|
|
|
|
def test_sync_bundle_endpoints(client, fixtures):
|
|
client.post(
|
|
"/api/routes/upload",
|
|
files={"file": ("onx.gpx", (fixtures / "onx.gpx").read_bytes(), "application/gpx+xml")},
|
|
)
|
|
bundle = client.get("/api/sync/bundle").json()
|
|
assert len(bundle["routes"]) == 1
|
|
resp = client.post("/api/sync/bundle", json=bundle)
|
|
assert resp.json()["applied"] == {"routes": 0, "trail_guides": 0}
|