-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_determinism.py
More file actions
65 lines (48 loc) · 1.61 KB
/
test_determinism.py
File metadata and controls
65 lines (48 loc) · 1.61 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
"""Determinism smoke test: submits identical payloads and verifies identical outputs."""
import json
import sys
import requests
API_URL = "http://localhost:8000/apply"
RUNS = 10
TIMEOUT = 10
PAYLOAD = {
"income_monthly": 5000,
"dti": 0.35,
"employment_months": 24,
"age": 28,
"has_defaults": False,
}
def main() -> None:
decisions: list[str] = []
scores: list[int] = []
for i in range(1, RUNS + 1):
resp = requests.post(API_URL, json=PAYLOAD, timeout=TIMEOUT)
if resp.status_code != 200:
print(f"Run {i}: FAILED (status {resp.status_code})")
print(resp.text)
sys.exit(1)
try:
data = resp.json()
except Exception:
print(f"Run {i}: FAILED (invalid JSON response)")
print(resp.text)
sys.exit(1)
decision = data.get("decision")
score = data.get("score")
if decision is None or score is None:
print(f"Run {i}: FAILED (missing decision/score)")
print(json.dumps(data, indent=2, sort_keys=True))
sys.exit(1)
decisions.append(decision)
scores.append(score)
print(f"Run {i}: decision={decision}, score={score}")
unique_decisions = sorted(set(decisions))
unique_scores = sorted(set(scores))
deterministic = (len(unique_decisions) == 1) and (len(unique_scores) == 1)
print()
print(f"Deterministic: {deterministic}")
print(f"Unique decisions: {unique_decisions}")
print(f"Unique scores: {unique_scores}")
sys.exit(0 if deterministic else 2)
if __name__ == "__main__":
main()