Spaces:
Sleeping
Sleeping
File size: 5,522 Bytes
697c967 | 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 | """
Quick test to verify the backend API with new fields is working
"""
import requests
import json
from datetime import datetime
BASE_URL = "http://localhost:8000" # Server running on port 8000
def test_backend():
print("Testing Backend API with new fields...")
print("=" * 50)
# Test 1: Health check
print("1. Testing health endpoint...")
try:
response = requests.get(f"{BASE_URL}/")
if response.status_code == 200:
print(" ✅ Health check: OK")
else:
print(f" ❌ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f" ❌ Health check failed: {e}")
return False
# Test 2: Register a user
print("2. Testing user registration...")
try:
timestamp = str(int(datetime.now().timestamp()))
user_data = {
"email": f"test_{timestamp}@example.com",
"password": "password123",
"name": f"Test User {timestamp}"
}
response = requests.post(f"{BASE_URL}/api/auth/register",
json=user_data,
headers={"Content-Type": "application/json"})
if response.status_code == 201:
result = response.json()
token = result['token']
user_id = result['user']['id']
print(f" ✅ User registration: OK (User ID: {user_id})")
else:
print(f" ❌ User registration failed: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f" ❌ User registration failed: {e}")
return False
# Test 3: Create a task with new fields
print("3. Testing task creation with new fields...")
try:
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
task_data = {
"title": "Test Task with New Fields",
"description": "Task with priority and due date",
"completed": False,
"priority": "high", # NEW FIELD
"due_date": "2025-12-31T23:59:59" # NEW FIELD
}
response = requests.post(f"{BASE_URL}/api/{user_id}/tasks",
json=task_data,
headers=headers)
if response.status_code == 201:
result = response.json()
task_id = result['id']
print(f" ✅ Task creation with new fields: OK (Task ID: {task_id})")
# Verify new fields are present in response
if 'priority' in result and 'due_date' in result:
print(f" ✅ New fields present in response: priority={result['priority']}, due_date={result['due_date']}")
else:
print(f" ⚠️ New fields missing from response: {result.keys()}")
else:
print(f" ❌ Task creation failed: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f" ❌ Task creation failed: {e}")
return False
# Test 4: Get the task back
print("4. Testing task retrieval...")
try:
response = requests.get(f"{BASE_URL}/api/{user_id}/tasks/{task_id}", headers=headers)
if response.status_code == 200:
result = response.json()
print(f" ✅ Task retrieval: OK")
# Verify new fields are preserved
if result.get('priority') == 'high':
print(f" ✅ Priority field preserved: {result['priority']}")
else:
print(f" ⚠️ Priority field not preserved: {result.get('priority')}")
if 'due_date' in result and result['due_date']:
print(f" ✅ Due date field preserved: {result['due_date']}")
else:
print(f" ⚠️ Due date field not preserved: {result.get('due_date')}")
else:
print(f" ❌ Task retrieval failed: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f" ❌ Task retrieval failed: {e}")
return False
# Test 5: Update task with new fields
print("5. Testing task update with new fields...")
try:
update_data = {
"priority": "low",
"due_date": "2026-01-15T10:30:00"
}
response = requests.put(f"{BASE_URL}/api/{user_id}/tasks/{task_id}",
json=update_data,
headers=headers)
if response.status_code == 200:
result = response.json()
print(f" ✅ Task update with new fields: OK")
if result.get('priority') == 'low':
print(f" ✅ Priority updated successfully: {result['priority']}")
else:
print(f" ⚠️ Priority not updated: {result.get('priority')}")
else:
print(f" ❌ Task update failed: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f" ❌ Task update failed: {e}")
return False
print("=" * 50)
print("🎉 ALL TESTS PASSED! Backend API with new fields is working correctly!")
print("✅ Priority and due_date fields are fully functional")
print("✅ All CRUD operations work with new fields")
print("=" * 50)
return True
if __name__ == "__main__":
test_backend() |