LogicGoInfotechSpaces's picture
Initial smart budget API
847392c
raw
history blame
5.2 kB
"""
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)