Spaces:
Sleeping
Sleeping
File size: 5,592 Bytes
bf78a48 |
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
#!/usr/bin/env python3
"""
Simple API test script to verify endpoints work correctly
"""
import requests
import json
import time
import sys
import os
# Add the current directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
BASE_URL = "http://localhost:8000/api/v1"
def test_health_check():
"""Test the health check endpoint"""
try:
response = requests.get("http://localhost:8000/health")
if response.status_code == 200:
print("β
Health check passed")
return True
else:
print(f"β Health check failed: {response.status_code}")
return False
except requests.exceptions.ConnectionError:
print("β Could not connect to server. Make sure it's running on http://localhost:8000")
return False
def test_create_todo():
"""Test creating a todo"""
try:
todo_data = {
"title": "Test Todo",
"description": "This is a test todo for API verification"
}
response = requests.post(
f"{BASE_URL}/todos/",
json=todo_data,
headers={"Content-Type": "application/json"}
)
if response.status_code == 201:
todo = response.json()
print(f"β
Created todo: {todo['title']} (ID: {todo['id']})")
return todo['id']
else:
print(f"β Failed to create todo: {response.status_code} - {response.text}")
return None
except Exception as e:
print(f"β Error creating todo: {e}")
return None
def test_get_todos():
"""Test getting all todos"""
try:
response = requests.get(f"{BASE_URL}/todos/")
if response.status_code == 200:
todos = response.json()
print(f"β
Retrieved {len(todos)} todos")
return len(todos) > 0
else:
print(f"β Failed to get todos: {response.status_code}")
return False
except Exception as e:
print(f"β Error getting todos: {e}")
return False
def test_get_todo(todo_id):
"""Test getting a specific todo"""
try:
response = requests.get(f"{BASE_URL}/todos/{todo_id}")
if response.status_code == 200:
todo = response.json()
print(f"β
Retrieved todo: {todo['title']}")
return True
else:
print(f"β Failed to get todo: {response.status_code}")
return False
except Exception as e:
print(f"β Error getting todo: {e}")
return False
def test_toggle_todo(todo_id):
"""Test toggling todo completion"""
try:
response = requests.patch(f"{BASE_URL}/todos/{todo_id}/toggle")
if response.status_code == 200:
todo = response.json()
print(f"β
Toggled todo completion: {todo['completed']}")
return True
else:
print(f"β Failed to toggle todo: {response.status_code}")
return False
except Exception as e:
print(f"β Error toggling todo: {e}")
return False
def test_update_todo(todo_id):
"""Test updating a todo"""
try:
update_data = {
"title": "Updated Test Todo",
"description": "This todo has been updated"
}
response = requests.put(
f"{BASE_URL}/todos/{todo_id}",
json=update_data,
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
todo = response.json()
print(f"β
Updated todo: {todo['title']}")
return True
else:
print(f"β Failed to update todo: {response.status_code}")
return False
except Exception as e:
print(f"β Error updating todo: {e}")
return False
def test_delete_todo(todo_id):
"""Test deleting a todo"""
try:
response = requests.delete(f"{BASE_URL}/todos/{todo_id}")
if response.status_code == 204:
print("β
Deleted todo successfully")
return True
else:
print(f"β Failed to delete todo: {response.status_code}")
return False
except Exception as e:
print(f"β Error deleting todo: {e}")
return False
def main():
"""Run all API tests"""
print("π§ͺ Testing AI Todo App API Endpoints")
print("=" * 50)
# Test health check first
if not test_health_check():
print("\nβ Server is not running. Please start the server first:")
print(" python run.py")
return False
print("\nπ Running API tests...")
# Test CRUD operations
todo_id = test_create_todo()
if not todo_id:
return False
if not test_get_todos():
return False
if not test_get_todo(todo_id):
return False
if not test_toggle_todo(todo_id):
return False
if not test_update_todo(todo_id):
return False
if not test_delete_todo(todo_id):
return False
print("\n" + "=" * 50)
print("β
All API tests passed!")
print("\nπ The backend is working correctly!")
print("\nπ You can now:")
print(" - View API docs at: http://localhost:8000/docs")
print(" - Test AI features (requires GROQ_API_KEY)")
print(" - Integrate with your frontend application")
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1) |