File size: 2,241 Bytes
dc3d345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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})