File size: 7,217 Bytes
9ff494f
 
 
4e13ce7
 
c7ee404
 
 
9ff494f
 
 
 
 
 
 
 
 
 
 
 
c7ee404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ff494f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e13ce7
 
 
 
 
9ff494f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ee404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e13ce7
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from fastapi.testclient import TestClient

from app import main as api
from app.schemas import CreditApplication, MODEL_FEATURES, TOP_SHAP_FEATURES
import numpy as np
import pandas as pd
import logging
from app.drift_monitor import DriftMonitor

AUTH_HEADER = {"Authorization": "Bearer test-token"}


class FakeModel:
    def __init__(self):
        self.last_frame = None

    def predict_proba(self, model_frame):
        self.last_frame = model_frame
        return [[0.2, 0.8]]

def build_test_monitor(batch_size=2):
    test_monitor = DriftMonitor.__new__(DriftMonitor)
    test_monitor.batch_size = batch_size
    test_monitor.drift_threshold = 0.05
    test_monitor.prediction_buffer = []
    test_monitor.logger = logging.getLogger("drift_monitor")

    test_monitor.reference_data = pd.DataFrame([
        {
            "EXT_SOURCE_MEAN": 0.5,
            "AMT_CREDIT": 200000,
            "AMT_INCOME_TOTAL": 150000,
            "DAYS_BIRTH": -15000,
            "DAYS_EMPLOYED": -3000,
        }
    ])

    return test_monitor

def test_health_reports_loaded_model(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    with TestClient(api.app) as client:
        response = client.get("/health")

    assert response.status_code == 200
    assert response.json() == {"status": "healthy", "model_loaded": True}


def test_features_endpoint_lists_exposed_top_shap_features(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setattr(api, "load_model", lambda: fake_model)

    with TestClient(api.app) as client:
        response = client.get("/features")

    assert response.status_code == 200
    assert response.json() == list(TOP_SHAP_FEATURES)


def test_predict_uses_top_features_and_imputes_hidden_features(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setenv("API_TOKEN", "test-token")
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    with TestClient(api.app) as client:
        response = client.post("/predict", json={}, headers=AUTH_HEADER)

    assert response.status_code == 200
    assert response.json() == {
        "prediction": 1,
        "probability_of_default": 0.8,
        "risk_category": "High",
    }
    expected_input = CreditApplication().to_model_input()
    expected_values = [expected_input[feature] for feature in MODEL_FEATURES]

    assert fake_model.last_frame.shape == (1, len(MODEL_FEATURES))
    np.testing.assert_allclose(fake_model.last_frame[0], expected_values)


def test_predict_rejects_hidden_model_features(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setenv("API_TOKEN", "test-token")
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    with TestClient(api.app) as client:
        response = client.post(
            "/predict",
            json={"CNT_CHILDREN": 2},
            headers=AUTH_HEADER,
        )

    assert response.status_code == 422


def test_predict_returns_503_when_model_is_not_loaded(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setenv("API_TOKEN", "test-token")
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(
        api,
        "get_model",
        lambda: (_ for _ in ()).throw(RuntimeError("Model not loaded")),
    )

    with TestClient(api.app) as client:
        response = client.post("/predict", json={}, headers=AUTH_HEADER)

    assert response.status_code == 503
    assert response.json()["detail"] == "Model not loaded"


def test_predict_requires_bearer_token(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setenv("API_TOKEN", "test-token")
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    with TestClient(api.app) as client:
        response = client.post("/predict", json={})

    assert response.status_code == 401
    assert response.json()["detail"] == "Missing bearer token"


def test_predict_rejects_invalid_bearer_token(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setenv("API_TOKEN", "test-token")
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    with TestClient(api.app) as client:
        response = client.post(
            "/predict",
            json={},
            headers={"Authorization": "Bearer wrong-token"},
        )

    assert response.status_code == 401
    assert response.json()["detail"] == "Invalid bearer token"


def test_predict_returns_503_when_api_token_is_not_configured(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.delenv("API_TOKEN", raising=False)
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    with TestClient(api.app) as client:
        response = client.post("/predict", json={}, headers=AUTH_HEADER)

    assert response.status_code == 503
    assert response.json()["detail"] == "API_TOKEN is not configured"


def test_drift_monitor_triggers(monkeypatch):
    fake_model = FakeModel()
    monkeypatch.setenv("API_TOKEN", "test-token")
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    # 🔥 Replace drift monitor with small batch size
    # from app.drift_monitor import DriftMonitor
    test_monitor = build_test_monitor(batch_size=3)

    monkeypatch.setattr(api, "drift_monitor", test_monitor)

    with TestClient(api.app) as client:
        for _ in range(3):  # trigger batch
            response = client.post("/predict", json={}, headers=AUTH_HEADER)
            assert response.status_code == 200


def test_drift_monitor_logs(monkeypatch, caplog):
    fake_model = FakeModel()
    monkeypatch.setenv("API_TOKEN", "test-token")
    monkeypatch.setattr(api, "load_model", lambda: fake_model)
    monkeypatch.setattr(api, "get_model", lambda: fake_model)

    # from app.drift_monitor import DriftMonitor
    test_monitor = build_test_monitor(batch_size=2)

    # Fake reference data
    test_monitor.reference_data = pd.DataFrame([
        {
            "EXT_SOURCE_MEAN": 0.5,
            "AMT_CREDIT": 200000,
            "AMT_INCOME_TOTAL": 150000,
            "DAYS_BIRTH": -15000,
            "DAYS_EMPLOYED": -3000,
        }
    ])

    monkeypatch.setattr(api, "drift_monitor", test_monitor)

    caplog.set_level(logging.INFO)

    with TestClient(api.app) as client:
        for _ in range(2):
            client.post("/predict", json={}, headers=AUTH_HEADER)


    # ✅ Check drift analysis triggered
    drift_logs = [
        record for record in caplog.records
        if "drift_analysis_completed" in record.getMessage()
    ]
    assert len(drift_logs) > 0

    # ✅ Check feature-level drift (OPTIONAL)
    feature_logs = [
        record for record in caplog.records
        if "feature_drift_detected" in record.getMessage()
    ]

    # You can assert or just inspect
    assert isinstance(feature_logs, list)