-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_alert_routes.py
More file actions
79 lines (73 loc) · 3.04 KB
/
Copy pathtest_alert_routes.py
File metadata and controls
79 lines (73 loc) · 3.04 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
import httpx
import json
print('=== Testing Alert Routes ===\n')
# Test 1: GET /api/alerts - List all alerts
print('1. GET /api/alerts (list all alerts)')
r = httpx.get('http://localhost:8000/api/alerts')
print(f'Status: {r.status_code}')
if r.status_code == 200:
data = r.json()
print(f'Total alerts: {len(data)}')
for i, alert in enumerate(data[:3]): # Show first 3
print(f' {i+1}. [{alert["severity"].upper()}] {alert["patientName"]}: {alert["message"][:50]}...')
print(f' Acknowledged: {alert["acknowledged"]}')
print()
else:
print(f'Error: {r.json()}\n')
# Test 2: GET /api/alerts - Verify newest first
print('2. GET /api/alerts - Verify newest first')
r = httpx.get('http://localhost:8000/api/alerts')
if r.status_code == 200:
data = r.json()
timestamps = [a["timestamp"] for a in data]
is_sorted = all(timestamps[i] >= timestamps[i+1] for i in range(len(timestamps)-1))
print(f'Alerts are sorted newest first: {is_sorted}')
if len(data) > 1:
print(f' First timestamp: {data[0]["timestamp"]}, Last timestamp: {data[-1]["timestamp"]}')
print()
else:
print(f'Error\n')
# Test 3: PATCH /api/alerts/{id}/acknowledge - Acknowledge an alert
print('3. PATCH /api/alerts/{id}/acknowledge')
r = httpx.get('http://localhost:8000/api/alerts')
if r.status_code == 200:
alerts = r.json()
unacknowledged = [a for a in alerts if not a["acknowledged"]]
if unacknowledged:
alert_id = unacknowledged[0]["id"]
print(f'Acknowledging alert: {alert_id}')
r = httpx.patch(f'http://localhost:8000/api/alerts/{alert_id}/acknowledge')
print(f'Status: {r.status_code}')
if r.status_code == 200:
response = r.json()
print(f'Success: {response["success"]}, Message: {response.get("message", "")}')
# Verify it's now acknowledged
r2 = httpx.get(f'http://localhost:8000/api/alerts')
updated_alerts = r2.json()
updated_alert = [a for a in updated_alerts if a["id"] == alert_id][0]
print(f'Alert acknowledged status now: {updated_alert["acknowledged"]}')
print()
else:
print('No unacknowledged alerts to test\n')
else:
print(f'Error\n')
# Test 4: PATCH /api/alerts/{id}/acknowledge - 404 for non-existent alert
print('4. PATCH /api/alerts/nonexistent-id/acknowledge (404 test)')
r = httpx.patch('http://localhost:8000/api/alerts/nonexistent-id/acknowledge')
print(f'Status: {r.status_code}')
if r.status_code == 404:
error = r.json()
print(f'Error message: {error["detail"]}')
print()
# Test 5: Full alert response format
print('5. Full alert response format:')
r = httpx.get('http://localhost:8000/api/alerts')
if r.status_code == 200:
data = r.json()
if data:
print(json.dumps(data[0], indent=2))
print('\nRequired fields present:')
alert = data[0]
required_fields = ["id", "patientId", "patientName", "message", "severity", "timestamp", "acknowledged"]
for field in required_fields:
print(f' - {field}: {field in alert}')