-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest-api.py
More file actions
123 lines (108 loc) · 3.67 KB
/
Copy pathtest-api.py
File metadata and controls
123 lines (108 loc) · 3.67 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
#!/usr/bin/env python3
"""
VishwaGuru Backend API Validation Script
Tests all API endpoints to ensure they are working correctly.
"""
import os
import sys
import requests
import json
from pathlib import Path
# Add backend to path
sys.path.insert(0, str(Path(__file__).parent))
def test_health_endpoint(base_url="http://localhost:8000"):
"""Test the health endpoint"""
try:
response = requests.get(f"{base_url}/health")
if response.status_code == 200:
data = response.json()
print("✅ Health endpoint working")
return True
else:
print(f"❌ Health endpoint failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Health endpoint error: {e}")
return False
def test_root_endpoint(base_url="http://localhost:8000"):
"""Test the root endpoint"""
try:
response = requests.get(f"{base_url}/")
if response.status_code == 200:
data = response.json()
print("✅ Root endpoint working")
return True
else:
print(f"❌ Root endpoint failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Root endpoint error: {e}")
return False
def test_stats_endpoint(base_url="http://localhost:8000"):
"""Test the stats endpoint"""
try:
response = requests.get(f"{base_url}/api/stats")
if response.status_code == 200:
data = response.json()
print("✅ Stats endpoint working")
return True
else:
print(f"❌ Stats endpoint failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Stats endpoint error: {e}")
return False
def test_recent_issues_endpoint(base_url="http://localhost:8000"):
"""Test the recent issues endpoint"""
try:
response = requests.get(f"{base_url}/api/issues/recent")
if response.status_code == 200:
data = response.json()
print("✅ Recent issues endpoint working")
return True
else:
print(f"❌ Recent issues endpoint failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Recent issues endpoint error: {e}")
return False
def test_maharashtra_rep_endpoint(base_url="http://localhost:8000"):
"""Test the Maharashtra representative endpoint"""
try:
response = requests.get(f"{base_url}/api/mh/rep-contacts?pincode=400001")
if response.status_code == 200:
data = response.json()
print("✅ Maharashtra rep endpoint working")
return True
else:
print(f"❌ Maharashtra rep endpoint failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Maharashtra rep endpoint error: {e}")
return False
def main():
"""Run all API validation tests"""
print("🧪 VishwaGuru Backend API Validation")
print("=" * 40)
base_url = os.getenv("API_BASE_URL", "http://localhost:8000")
tests = [
test_health_endpoint,
test_root_endpoint,
test_stats_endpoint,
test_recent_issues_endpoint,
test_maharashtra_rep_endpoint,
]
passed = 0
total = len(tests)
for test in tests:
if test(base_url):
passed += 1
print(f"\n📊 Results: {passed}/{total} tests passed")
if passed == total:
print("🎉 All API endpoints are working correctly!")
return 0
else:
print("⚠️ Some API endpoints need attention.")
return 1
if __name__ == "__main__":
sys.exit(main())