Spaces:
Sleeping
Sleeping
| """Smoke tests for the NephroScreen API. Run: pytest""" | |
| import warnings | |
| from fastapi.testclient import TestClient | |
| from api.main import app | |
| client = TestClient(app) | |
| CKD_PATIENT = { | |
| "age": 62, "bp": 80, "sg": 1.01, "al": 3, "su": 0, "bgr": 148, "bu": 86, "sc": 3.2, | |
| "sod": 135, "pot": 4.6, "hemo": 9.5, "pcv": 28, "wbcc": 9800, "rbcc": 3.4, | |
| "rbc": "abnormal", "pc": "abnormal", "pcc": "present", "ba": "notpresent", | |
| "htn": "yes", "dm": "yes", "cad": "no", "appet": "poor", "pe": "yes", "ane": "yes", | |
| } | |
| HEALTHY_PATIENT = { | |
| "age": 30, "bp": 70, "sg": 1.025, "al": 0, "su": 0, "bgr": 90, "bu": 15, "sc": 0.9, | |
| "sod": 140, "pot": 4.2, "hemo": 15.5, "pcv": 47, "wbcc": 7500, "rbcc": 5.2, | |
| "rbc": "normal", "pc": "normal", "pcc": "notpresent", "ba": "notpresent", | |
| "htn": "no", "dm": "no", "cad": "no", "appet": "good", "pe": "no", "ane": "no", | |
| } | |
| def test_health(): | |
| with TestClient(app) as c: | |
| body = c.get("/health").json() | |
| assert body["status"] == "ok" | |
| assert body["model_loaded"] is True | |
| def test_metadata_shape(): | |
| with TestClient(app) as c: | |
| meta = c.get("/api/metadata").json() | |
| assert len(meta["numeric_fields"]) == 14 | |
| assert len(meta["categorical_fields"]) == 10 | |
| assert 0 < meta["threshold"] <= 1 | |
| def test_ckd_prediction(): | |
| with TestClient(app) as c: | |
| r = c.post("/api/predict", json=CKD_PATIENT).json() | |
| assert r["prediction"] == "CKD" | |
| assert r["probability"] >= 0.5 | |
| assert r["risk_band"] in {"Low", "Moderate", "High"} | |
| assert len(r["key_indicators"]) > 0 | |
| def test_healthy_prediction(): | |
| with TestClient(app) as c: | |
| r = c.post("/api/predict", json=HEALTHY_PATIENT).json() | |
| assert r["prediction"] == "Not CKD" | |
| assert r["probability"] < 0.5 | |
| def test_sparse_input_is_imputed(): | |
| with TestClient(app) as c: | |
| r = c.post("/api/predict", json={"hemo": 9.0, "sc": 4.0, "al": 4}) | |
| assert r.status_code == 200 | |
| assert "probability" in r.json() | |
| def test_serving_is_warning_free(): | |
| with TestClient(app) as c, warnings.catch_warnings(): | |
| warnings.simplefilter("error", FutureWarning) | |
| c.post("/api/predict", json={"hemo": 9.0, "sc": 4.0}) | |