forked from dbhavnakhatri/BreastCancerDetect_finalcodee
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_database.py
More file actions
164 lines (147 loc) · 4.8 KB
/
test_database.py
File metadata and controls
164 lines (147 loc) · 4.8 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
"""
Test script to verify database connection and API endpoints
Run this after starting docker-compose
"""
import requests
import json
BASE_URL = "http://localhost:8001"
def test_health():
"""Test health endpoint"""
print("\n1. Testing Health Endpoint...")
try:
response = requests.get(f"{BASE_URL}/health")
print(f" Status: {response.status_code}")
print(f" Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f" Error: {e}")
return False
def test_signup():
"""Test user signup"""
print("\n2. Testing User Signup...")
try:
response = requests.post(
f"{BASE_URL}/auth/signup",
json={
"email": "test@example.com",
"name": "Test User",
"password": "password123"
}
)
print(f" Status: {response.status_code}")
print(f" Response: {json.dumps(response.json(), indent=2)}")
return response.status_code in [200, 400] # 400 if user already exists
except Exception as e:
print(f" Error: {e}")
return False
def test_login():
"""Test user login"""
print("\n3. Testing User Login...")
try:
response = requests.post(
f"{BASE_URL}/auth/login/json",
json={
"email": "test@example.com",
"password": "password123"
}
)
print(f" Status: {response.status_code}")
data = response.json()
print(f" Response: {json.dumps(data, indent=2)}")
if response.status_code == 200:
return data.get("access_token")
return None
except Exception as e:
print(f" Error: {e}")
return None
def test_get_me(token):
"""Test get current user"""
print("\n4. Testing Get Current User...")
try:
response = requests.get(
f"{BASE_URL}/auth/me",
headers={"Authorization": f"Bearer {token}"}
)
print(f" Status: {response.status_code}")
print(f" Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f" Error: {e}")
return False
def test_create_patient(token):
"""Test create patient"""
print("\n5. Testing Create Patient...")
try:
response = requests.post(
f"{BASE_URL}/patients/",
headers={"Authorization": f"Bearer {token}"},
json={
"patient_hn": "HN001",
"name": "Jane Doe",
"age": "45 Years",
"sex": "Female",
"phone": "1234567890",
"email": "jane@example.com"
}
)
print(f" Status: {response.status_code}")
print(f" Response: {json.dumps(response.json(), indent=2)}")
return response.status_code in [200, 400] # 400 if patient already exists
except Exception as e:
print(f" Error: {e}")
return False
def test_get_patients(token):
"""Test get patients"""
print("\n6. Testing Get Patients...")
try:
response = requests.get(
f"{BASE_URL}/patients/",
headers={"Authorization": f"Bearer {token}"}
)
print(f" Status: {response.status_code}")
print(f" Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f" Error: {e}")
return False
def test_dashboard(token):
"""Test dashboard stats"""
print("\n7. Testing Dashboard Stats...")
try:
response = requests.get(
f"{BASE_URL}/dashboard/stats",
headers={"Authorization": f"Bearer {token}"}
)
print(f" Status: {response.status_code}")
print(f" Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
except Exception as e:
print(f" Error: {e}")
return False
def main():
print("=" * 50)
print("DATABASE & API TEST SCRIPT")
print("=" * 50)
# Test health
if not test_health():
print("\n❌ Health check failed. Is the backend running?")
return
# Test signup
test_signup()
# Test login
token = test_login()
if not token:
print("\n❌ Login failed. Cannot continue tests.")
return
print(f"\n✅ Got access token: {token[:50]}...")
# Test authenticated endpoints
test_get_me(token)
test_create_patient(token)
test_get_patients(token)
test_dashboard(token)
print("\n" + "=" * 50)
print("✅ ALL TESTS COMPLETED!")
print("=" * 50)
print("\nAPI Documentation: http://localhost:8001/docs")
if __name__ == "__main__":
main()