-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_kolmogorov_smirnov.py
More file actions
427 lines (379 loc) · 15.6 KB
/
Copy pathtest_kolmogorov_smirnov.py
File metadata and controls
427 lines (379 loc) · 15.6 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""Tests for Kolmogorov-Smirnov drift detection endpoint."""
from http import HTTPStatus
from fastapi import FastAPI
from fastapi.testclient import TestClient
from src.endpoints.metrics.drift.kolmogorov_smirnov import KSTestMetricRequest, router
from . import factory
# Create test app with KS Test router
app = FastAPI()
app.include_router(router)
client = TestClient(app)
class TestKSTestEndpoints:
"""Unified endpoint tests for KS Test metric."""
# Pandas DataFrame tests
test_compute_endpoint_pandas = factory.make_compute_endpoint_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1", "feature2"],
"batchSize": 100,
},
expected_response_keys=[
"status",
"value",
"drift_detected",
"p_value",
"alpha",
],
df_type="Pandas",
)
# Polars DataFrame tests
test_compute_endpoint_polars = factory.make_compute_endpoint_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1", "feature2"],
"batchSize": 100,
},
expected_response_keys=[
"status",
"value",
"drift_detected",
"p_value",
"alpha",
],
df_type="Polars",
)
test_definition_endpoint = factory.make_definition_endpoint_test(
metric_name="KSTest",
endpoint_path="/metrics/drift/kstest/definition",
client=client,
expected_name="Kolmogorov-Smirnov",
)
test_schedule_endpoint = factory.make_schedule_endpoint_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
)
test_delete_schedule_endpoint = factory.make_delete_schedule_endpoint_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
)
test_list_requests_endpoint = factory.make_list_requests_endpoint_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/requests",
client=client,
)
# ========================================================================
# Error Handling Tests
# ========================================================================
# Compute endpoint errors
test_compute_missing_reference_tag = factory.make_compute_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
# Missing referenceTag
"fitColumns": ["feature1"],
},
expected_status_code=HTTPStatus.BAD_REQUEST,
expected_error_substring="referenceTag is required",
)
test_compute_missing_fit_columns_derives_from_metadata = (
factory.make_compute_endpoint_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
},
expected_response_keys=["status", "value", "drift_detected"],
)
)
test_compute_invalid_feature = factory.make_compute_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["nonexistent_feature"],
},
expected_status_code=HTTPStatus.BAD_REQUEST,
expected_error_substring="not found in data",
)
# Delete endpoint errors
test_delete_invalid_uuid = factory.make_delete_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_id="not-a-valid-uuid",
expected_status_code=HTTPStatus.BAD_REQUEST, # Invalid UUID returns 400 (Bad Request)
expected_error_substring="Invalid request ID",
)
# List endpoint with multiple requests
test_list_requests_with_data = factory.make_list_requests_with_data_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/requests",
client=client,
num_requests=3,
)
# List endpoint with malformed requests (defensive logic test)
test_list_requests_filters_malformed = (
factory.make_list_requests_with_malformed_data_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/requests",
client=client,
num_valid_requests=2,
num_malformed_requests=3,
)
)
# ========================================================================
# Scheduler Exception Tests
# ========================================================================
# Schedule endpoint scheduler exceptions
test_schedule_connection_error = factory.make_schedule_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
expected_status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
expected_error_substring="connect", # Match "Failed to connect"
mock_scheduler_none=False,
register_side_effect=ConnectionError("Failed to connect to scheduler database"),
)
test_schedule_timeout_error = factory.make_schedule_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
expected_status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
expected_error_substring="timeout",
mock_scheduler_none=False,
register_side_effect=TimeoutError("Scheduler registration timeout after 30s"),
)
test_schedule_runtime_error = factory.make_schedule_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
expected_status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
expected_error_substring="error",
mock_scheduler_none=False,
register_side_effect=RuntimeError("Internal scheduler error occurred"),
)
# Delete endpoint scheduler exceptions
test_delete_connection_error = factory.make_delete_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_id="123e4567-e89b-12d3-a456-426614174000",
expected_status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
expected_error_substring="connect", # Match "Failed to connect"
mock_scheduler_none=False,
delete_side_effect=ConnectionError("Failed to connect to scheduler database"),
)
test_delete_runtime_error = factory.make_delete_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_id="123e4567-e89b-12d3-a456-426614174000",
expected_status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
expected_error_substring="failed",
mock_scheduler_none=False,
delete_side_effect=RuntimeError("Scheduler deletion failed"),
)
# Scheduler unavailable tests
# Note: Current endpoint implementation returns 500 instead of 503 for
# scheduler unavailable. This could be improved to return 503
# (Service Unavailable) in a future update
test_schedule_scheduler_unavailable = factory.make_schedule_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
expected_status_code=HTTPStatus.SERVICE_UNAVAILABLE,
# Matches "Prometheus scheduler not available"
expected_error_substring="not available",
mock_scheduler_none=True,
)
test_delete_scheduler_unavailable = factory.make_delete_endpoint_error_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/request",
client=client,
request_id="123e4567-e89b-12d3-a456-426614174000",
expected_status_code=HTTPStatus.SERVICE_UNAVAILABLE,
# Matches "Prometheus scheduler not available"
expected_error_substring="not available",
mock_scheduler_none=True,
)
# ========================================================================
# Empty Data Tests
# ========================================================================
test_compute_empty_reference_data = factory.make_compute_empty_reference_data_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
)
test_compute_empty_current_data = factory.make_compute_empty_current_data_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
)
# ========================================================================
# List Endpoint Exception Tests
# ========================================================================
test_list_scheduler_unavailable = (
factory.make_list_endpoint_scheduler_unavailable_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/requests",
client=client,
)
)
test_list_exception_handling = factory.make_list_endpoint_exception_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest/requests",
client=client,
)
# ========================================================================
# Compute Endpoint Generic Exception
# ========================================================================
test_compute_generic_exception = factory.make_compute_generic_exception_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
},
)
# ========================================================================
# Additional Coverage Tests
# ========================================================================
# Test with custom threshold_delta (covers line 84: custom alpha)
test_compute_custom_threshold = factory.make_compute_endpoint_test(
metric_name="KSTest",
module_path="src.endpoints.metrics.drift.kolmogorov_smirnov",
endpoint_path="/metrics/drift/kstest",
client=client,
request_payload={
"modelId": "test-model",
"referenceTag": "baseline",
"fitColumns": ["feature1"],
"thresholdDelta": 0.01, # Custom alpha value
"batchSize": 50, # Also test custom batch size (line 63)
},
expected_response_keys=[
"status",
"value",
"drift_detected",
"p_value",
"alpha",
],
df_type="Polars",
)
# ========================================================================
# KSTestMetricRequest.retrieve_tags() Tests
# ========================================================================
def test_retrieve_tags_with_all_fields(self) -> None:
"""Test retrieve_tags method with all fields populated."""
request = KSTestMetricRequest(
modelId="test-model",
referenceTag="baseline",
fitColumns=["feature1", "feature2"],
)
tags = request.retrieve_tags()
# Check that tags include the base tags plus KSTest-specific tags
assert "modelId" in tags
assert tags["modelId"] == "test-model"
assert "referenceTag" in tags
assert tags["referenceTag"] == "baseline"
assert "fitColumns" in tags
assert tags["fitColumns"] == "feature1,feature2"
def test_retrieve_tags_without_reference_tag(self) -> None:
"""Test retrieve_tags method without referenceTag."""
request = KSTestMetricRequest(
modelId="test-model",
fitColumns=["feature1"],
)
tags = request.retrieve_tags()
# Check that tags include base tags but not referenceTag
assert "modelId" in tags
assert tags["modelId"] == "test-model"
assert "referenceTag" not in tags
assert "fitColumns" in tags
def test_retrieve_tags_without_fit_columns(self) -> None:
"""Test retrieve_tags method without fitColumns."""
request = KSTestMetricRequest(
modelId="test-model",
referenceTag="baseline",
)
tags = request.retrieve_tags()
# Check that tags include referenceTag but not fitColumns
assert "modelId" in tags
assert "referenceTag" in tags
assert "fitColumns" not in tags