Spaces:
Running on Zero
Running on Zero
Download tests/test_api.py from deveshc23/drift-quant: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/spaces/deveshc23/drift-quant/resolve/main/tests/test_api.py
- Command line
-
hf download hf://spaces/deveshc23/drift-quant/tests/test_api.py
-
curl -L -o test_api.py https://huggingface.co/spaces/deveshc23/drift-quant/resolve/main/tests/test_api.py
1.72 kB
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| from fastapi.testclient import TestClient | |
| from drift_quant.api.app import app | |
| def _write_artifacts(root: Path) -> None: | |
| experiments = root / "experiments" | |
| experiments.mkdir(parents=True) | |
| identifier = "123e4567-e89b-12d3-a456-426614174000" | |
| index = [ | |
| { | |
| "experiment_id": identifier, | |
| "strategy_name": "example", | |
| "strategy_version": "1", | |
| "dataset_id": "fixture", | |
| "period_start": "2024-01-01", | |
| "period_end_exclusive": "2024-02-01", | |
| "metrics": {"cumulative_return": 0.1}, | |
| } | |
| ] | |
| payload = { | |
| **index[0], | |
| "config": {"initial_capital": 1000}, | |
| "equity_curve": [{"timestamp": "2024-01-01 09:15:00", "equity": 1000, "gross_exposure": 0}], | |
| "trades": [], | |
| } | |
| (experiments / "index.json").write_text(json.dumps(index), encoding="utf-8") | |
| (experiments / f"{identifier}.json").write_text(json.dumps(payload), encoding="utf-8") | |
| (root / "dataset_audit.json").write_text(json.dumps({"rows": 1}), encoding="utf-8") | |
| def test_api_reads_persisted_artifacts_without_path_input( | |
| tmp_path: Path, monkeypatch: object | |
| ) -> None: | |
| _write_artifacts(tmp_path) | |
| monkeypatch.setenv("DRIFT_ARTIFACTS_DIR", str(tmp_path)) # type: ignore[attr-defined] | |
| client = TestClient(app) | |
| listing = client.get("/api/v1/experiments") | |
| assert listing.status_code == 200 | |
| experiment_id = listing.json()["items"][0]["experiment_id"] | |
| assert client.get(f"/api/v1/experiments/{experiment_id}/equity").status_code == 200 | |
| assert client.get("/api/v1/experiments/../../etc/passwd").status_code == 404 | |