Spaces:
Paused
Paused
Download tests/test_api.py from cm3r/infoclimat-models: direct link, hf CLI and curl.
- Browser
- Download file 4.14 kB
-
https://huggingface.co/spaces/cm3r/infoclimat-models/resolve/main/tests/test_api.py
- Command line
-
hf download hf://spaces/cm3r/infoclimat-models/tests/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/cm3r/infoclimat-models/resolve/main/tests/test_api.py
4.14 kB
| """API surface tests against a synthetic run written to a temporary local store.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from wmb.models.base import Grid, Level | |
| from wmb.store.backend import LocalBackend | |
| from wmb.store.layout import OVERVIEW_FACTOR, RunManifest, latest_path, manifest_path, zarr_path | |
| from wmb.store.writer import RunWriter | |
| def client(tmp_path, monkeypatch): | |
| grid = Grid(lat0=40.0, lon0=-6.0, dlat=0.1, dlon=0.1, nlat=120, nlon=170) | |
| lat, lon = np.meshgrid(grid.lats(), grid.lons(), indexing="ij") | |
| fields = { | |
| "t2m": (Level("height", 2), (285 + 5 * np.sin(np.radians(lon * 20))).astype(np.float32)), | |
| "mslp": (Level("msl"), (101300 + 500 * np.cos(np.radians(lat * 15))).astype(np.float32)), | |
| "u10": (Level("height", 10), np.full(lat.shape, 4, np.float32)), | |
| "v10": (Level("height", 10), np.full(lat.shape, -2, np.float32)), | |
| } | |
| backend = LocalBackend(tmp_path / "store") | |
| rk = "2026-09-10T12Z" | |
| w = RunWriter(Path(backend.root) / zarr_path("arome-hd", rk), grid, [0, 6, 12]) | |
| m = RunManifest(model_id="arome-hd", run=rk, grid=grid.to_dict(), overview_grid=grid.coarsen(OVERVIEW_FACTOR).to_dict(), | |
| overview_factor=OVERVIEW_FACTOR, status="complete", steps=[0, 6, 12]) | |
| for var, (level, arr) in fields.items(): | |
| e = w.ensure_field(var, level) | |
| for s in [0, 6, 12]: | |
| w.write_step(e, s, arr) | |
| m.fields[e.array] = e | |
| backend.write_bytes(manifest_path("arome-hd", rk), m.to_json().encode()) | |
| backend.write_bytes(latest_path("arome-hd"), json.dumps({"latest": rk, "runs": [rk]}).encode()) | |
| monkeypatch.setenv("WMB_STORE_URL", str(tmp_path / "store")) | |
| monkeypatch.setenv("WMB_CACHE_DIR", str(tmp_path / "cache")) | |
| monkeypatch.setenv("WMB_REMOTE_IMAGE_CACHE", "false") | |
| monkeypatch.setenv("WMB_AI_PROVIDER", "off") | |
| import wmb.config as cfg | |
| import wmb.store.reader as reader | |
| cfg.get_settings.cache_clear() | |
| reader._reader = None | |
| from wmb.api import app as app_module | |
| with TestClient(app_module.app) as c: | |
| yield c | |
| cfg.get_settings.cache_clear() | |
| reader._reader = None | |
| def test_catalog_and_run_info(client): | |
| cat = client.get("/api/catalog").json() | |
| assert cat["models"][0]["id"] == "arome-hd" and cat["models"][0]["latest"] == "2026-09-10T12Z" | |
| assert any(r["id"] == "france" for r in cat["regions"]) | |
| info = client.get("/api/models/arome-hd/runs/latest").json() | |
| ids = {v["id"] for v in info["variables"]} | |
| assert {"t2m", "mslp", "wind10"} <= ids | |
| assert info["steps"] == [0, 6, 12] | |
| def test_post_map_then_cached(client): | |
| body = {"model": "arome-hd", "run": "latest", "step": 6, "region": "france", "width": 600, | |
| "layers": [{"variable": "t2m"}, {"variable": "mslp", "style": "contour", "interval": 4}, | |
| {"variable": "wind10", "style": "barbs"}]} | |
| r1 = client.post("/api/maps", json=body).json() | |
| assert r1["cached"] is False and r1["spec"]["run"] == "2026-09-10T12Z" | |
| png = client.get(r1["url"]) | |
| assert png.status_code == 200 and png.content[:4] == b"\x89PNG" | |
| assert "immutable" in png.headers["cache-control"] | |
| r2 = client.post("/api/maps", json=body).json() | |
| assert r2["cached"] is True and r2["key"] == r1["key"] | |
| shared = client.get(r1["share"]) | |
| assert shared.status_code == 200 and shared.headers["x-map-key"] == r1["key"] | |
| def test_errors(client): | |
| r = client.post("/api/maps", json={"layers": [{"variable": "cape"}], "run": "latest", "step": 6}) | |
| assert r.status_code == 422 and "disponible" in r.json()["error"] | |
| r = client.post("/api/maps", json={"layers": [{"variable": "nope"}], "run": "latest"}) | |
| assert r.status_code == 422 | |
| r = client.get("/api/map.png?layers=t2m&step=3") | |
| assert r.status_code == 422 | |
| r = client.get("/api/models/arome-hd/runs/2020-01-01T00Z") | |
| assert r.status_code == 404 | |
| r = client.post("/api/ai/compose", json={"prompt": "orages demain"}) | |
| assert r.status_code == 503 | |