-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_web.py
More file actions
197 lines (143 loc) · 7.27 KB
/
Copy pathtest_web.py
File metadata and controls
197 lines (143 loc) · 7.27 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""Tests for modelforge.web (US-018 — FastAPI MVP)."""
from __future__ import annotations
from pathlib import Path
import pytest
import yaml
from fastapi.testclient import TestClient
from modelforge.templates import build_model
from modelforge.web import create_app
ROOT = Path(__file__).resolve().parent.parent
@pytest.fixture(scope="module")
def client(tmp_path_factory):
session = tmp_path_factory.mktemp("web_session")
app = create_app(session_dir=session)
return TestClient(app)
@pytest.fixture(scope="module")
def uploaded_workbook(client, tmp_path_factory):
"""Build a unitranche workbook and POST it to /upload once."""
from modelforge.spec.unitranche import UnitrancheSpec
p = ROOT / "examples" / "unitranche_cdmo.yaml"
spec = UnitrancheSpec.model_validate(yaml.safe_load(p.read_bytes()))
out = tmp_path_factory.mktemp("web_uploaded") / "u.xlsx"
build_model(spec, out, spec_source_bytes=p.read_bytes(), spec_source_path=p)
with out.open("rb") as fh:
r = client.post(
"/upload",
files={"file": ("u.xlsx", fh,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")},
)
assert r.status_code == 200
return r.json()
# ── Basic health ────────────────────────────────────────────────────────────
def test_health_returns_ok(client):
r = client.get("/health")
assert r.status_code == 200
body = r.json()
assert body["status"] == "ok"
def test_index_renders_html(client):
r = client.get("/")
assert r.status_code == 200
assert r.headers["content-type"].startswith("text/html")
assert "ModelForge Web" in r.text
# ── Upload ──────────────────────────────────────────────────────────────────
def test_upload_rejects_non_xlsx(client):
r = client.post("/upload",
files={"file": ("test.pdf", b"%PDF", "application/pdf")})
assert r.status_code == 400
def test_upload_rejects_empty(client):
r = client.post("/upload",
files={"file": ("empty.xlsx", b"",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")})
assert r.status_code == 400
def test_upload_returns_metadata(uploaded_workbook):
assert "workbook_id" in uploaded_workbook
assert len(uploaded_workbook["workbook_id"]) == 16 # first 16 of SHA256
assert uploaded_workbook["primary_output_ref"] is not None
assert "Cover" in uploaded_workbook["sheet_names"]
assert "Assumptions" in uploaded_workbook["sheet_names"]
def test_upload_idempotent_on_identical_file_bytes(client, uploaded_workbook):
"""Uploading the EXACT SAME file bytes twice returns the same wid.
(We can't re-build the spec twice and expect matching wids — the
Reproducibility sheet's build_timestamp differs per build — so we
re-upload the stored file via the store's on-disk path.)
"""
wid = uploaded_workbook["workbook_id"]
# Find the stored file via the store
stored = client.app.state.store.get(wid)
raw = stored.path.read_bytes()
r = client.post("/upload",
files={"file": (stored.original_name, raw,
"application/octet-stream")})
assert r.status_code == 200
assert r.json()["workbook_id"] == wid
# ── View / metadata ─────────────────────────────────────────────────────────
def test_workbook_metadata_endpoint(client, uploaded_workbook):
wid = uploaded_workbook["workbook_id"]
r = client.get(f"/workbook/{wid}")
assert r.status_code == 200
body = r.json()
assert body["workbook_id"] == wid
assert body["primary_output_ref"] == uploaded_workbook["primary_output_ref"]
def test_workbook_view_html(client, uploaded_workbook):
wid = uploaded_workbook["workbook_id"]
r = client.get(f"/workbook/{wid}/view")
assert r.status_code == 200
assert "SensitivityAnalysis" in r.text
assert "Reproducibility" in r.text
def test_unknown_workbook_returns_404(client):
r = client.get("/workbook/nonexistenthex0")
assert r.status_code == 404
# ── Dossier ─────────────────────────────────────────────────────────────────
def test_dossier_endpoint_returns_pdf(client, uploaded_workbook):
wid = uploaded_workbook["workbook_id"]
r = client.get(f"/workbook/{wid}/dossier")
assert r.status_code == 200
assert r.headers["content-type"] == "application/pdf"
assert r.content.startswith(b"%PDF-")
assert len(r.content) > 5000
# ── Drift ───────────────────────────────────────────────────────────────────
def test_drift_endpoint(client, uploaded_workbook):
wid = uploaded_workbook["workbook_id"]
r = client.get(f"/workbook/{wid}/drift")
assert r.status_code == 200
body = r.json()
assert body["workbook_id"] == wid
# CDMO unitranche has EURIBOR 6M at stale 2.8% → at least 1 flag
assert body["flagged"] >= 1
# Items should carry all required fields
for item in body["items"]:
assert "driver" in item
assert "assumed" in item
assert "current" in item
assert "flagged" in item
# ── Diff ────────────────────────────────────────────────────────────────────
def test_diff_endpoint(client, uploaded_workbook):
# Diff workbook against itself — clean result
wid = uploaded_workbook["workbook_id"]
r = client.get(f"/diff?a={wid}&b={wid}")
assert r.status_code == 200
assert "ModelForge diff" in r.text
def test_diff_missing_workbook_404(client):
r = client.get("/diff?a=nonexistenthexxxx&b=nonexistenthexxxx")
assert r.status_code == 404
# ── Risk ────────────────────────────────────────────────────────────────────
def test_risk_endpoint_post(client):
r = client.post("/risk", json={
"equity_value_eur_m": 500, "equity_volatility": 0.32,
"debt_face_value_eur_m": 400, "lgd": 0.45, "maturity_years": 7,
"counterparty": "TestCo",
})
assert r.status_code == 200
body = r.json()
assert body["merton"]["converged"] is True
assert 0 <= body["merton"]["pd"] <= 1
assert body["ecl"]["stage"] in ("stage_1", "stage_2", "stage_3")
def test_risk_endpoint_rejects_invalid():
"""Negative equity rejected by Pydantic."""
app = create_app()
client_local = TestClient(app)
r = client_local.post("/risk", json={
"equity_value_eur_m": -1, "equity_volatility": 0.3,
"debt_face_value_eur_m": 50,
})
assert r.status_code == 422