File size: 5,199 Bytes
847392c |
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 |
"""
Simple test script to verify the API is working
Run this after starting the server to test all endpoints
"""
import requests
import json
from datetime import datetime
# Update this to your API URL
# For local: http://localhost:8000
# For Hugging Face: https://your-username-your-space.hf.space
BASE_URL = "http://localhost:8000"
def test_health():
"""Test health endpoint"""
print("=" * 50)
print("Testing Health Endpoint")
print("=" * 50)
try:
response = requests.get(f"{BASE_URL}/health")
print(f"Status Code: {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_root():
"""Test root endpoint"""
print("\n" + "=" * 50)
print("Testing Root Endpoint")
print("=" * 50)
try:
response = requests.get(f"{BASE_URL}/")
print(f"Status Code: {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_expense():
"""Test creating an expense"""
print("\n" + "=" * 50)
print("Testing Create Expense Endpoint")
print("=" * 50)
try:
expense_data = {
"user_id": "test_user_123",
"amount": 3800,
"category": "Groceries",
"description": "Monthly groceries",
"date": datetime.now().isoformat(),
"type": "expense"
}
response = requests.post(
f"{BASE_URL}/expenses",
json=expense_data,
headers={"Content-Type": "application/json"}
)
print(f"Status Code: {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_get_expenses():
"""Test getting expenses"""
print("\n" + "=" * 50)
print("Testing Get Expenses Endpoint")
print("=" * 50)
try:
response = requests.get(
f"{BASE_URL}/expenses",
params={"user_id": "test_user_123", "limit": 10}
)
print(f"Status Code: {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_get_recommendations():
"""Test getting budget recommendations"""
print("\n" + "=" * 50)
print("Testing Get Recommendations Endpoint")
print("=" * 50)
try:
# Get recommendations for next month
next_month = datetime.now().month + 1
next_year = datetime.now().year
if next_month > 12:
next_month = 1
next_year += 1
response = requests.get(
f"{BASE_URL}/recommendations/test_user_123",
params={"month": next_month, "year": next_year}
)
print(f"Status Code: {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_get_category_expenses():
"""Test getting category expenses"""
print("\n" + "=" * 50)
print("Testing Get Category Expenses Endpoint")
print("=" * 50)
try:
response = requests.get(
f"{BASE_URL}/category-expenses/test_user_123",
params={"months": 3}
)
print(f"Status Code: {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
if __name__ == "__main__":
print("\n" + "=" * 50)
print("Smart Budget Recommendation API - Test Suite")
print("=" * 50)
print(f"\nTesting API at: {BASE_URL}")
print("\nMake sure the server is running!")
print("Start server with: uvicorn app.main:app --reload\n")
results = []
# Test all endpoints
results.append(("Health Check", test_health()))
results.append(("Root Endpoint", test_root()))
results.append(("Create Expense", test_create_expense()))
results.append(("Get Expenses", test_get_expenses()))
results.append(("Get Recommendations", test_get_recommendations()))
results.append(("Get Category Expenses", test_get_category_expenses()))
# Summary
print("\n" + "=" * 50)
print("Test Summary")
print("=" * 50)
for test_name, passed in results:
status = "✓ PASS" if passed else "✗ FAIL"
print(f"{status} - {test_name}")
passed_count = sum(1 for _, passed in results if passed)
print(f"\nTotal: {passed_count}/{len(results)} tests passed")
print("\n" + "=" * 50)
print("API Documentation available at:")
print(f" - Swagger UI: {BASE_URL}/docs")
print(f" - ReDoc: {BASE_URL}/redoc")
print("=" * 50)
|