Spaces:
Sleeping
Sleeping
File size: 3,623 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 |
"""
Test script to verify the new Task model fields work correctly
"""
import requests
import json
from datetime import datetime
BASE_URL = "http://localhost:8001"
def test_new_task_fields():
print("Testing new Task model fields (priority, due_date)...")
# Register a test user
timestamp = str(int(datetime.now().timestamp()))
user_data = {
"email": f"testuser_{timestamp}@example.com",
"password": "securepassword123",
"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:
print(f"Failed to register user: {response.status_code}")
return False
result = response.json()
token = result['token']
user_id = result['user']['id']
print(f"User registered: {user_id}")
# Create a task with new fields
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
task_data = {
"title": "Test Task with Priority",
"description": "This is a test task with priority and due date",
"completed": False,
"priority": "high",
"due_date": "2025-12-31T23:59:59"
}
response = requests.post(f"{BASE_URL}/api/{user_id}/tasks",
json=task_data,
headers=headers)
if response.status_code != 201:
print(f"Failed to create task with new fields: {response.status_code} - {response.text}")
return False
task_result = response.json()
print(f"Task created with new fields: {task_result}")
# Verify the response contains the new fields
if 'priority' in task_result and 'due_date' in task_result:
print("✅ New fields (priority, due_date) are present in response")
else:
print("❌ New fields are missing from response")
return False
# Get the task back to verify it was stored correctly
response = requests.get(f"{BASE_URL}/api/{user_id}/tasks/{task_result['id']}", headers=headers)
if response.status_code != 200:
print(f"Failed to retrieve task: {response.status_code}")
return False
retrieved_task = response.json()
print(f"Retrieved task: {retrieved_task}")
# Verify the new fields are preserved
if (retrieved_task.get('priority') == 'high' and
'due_date' in retrieved_task and
retrieved_task.get('title') == 'Test Task with Priority'):
print("✅ New fields are correctly stored and retrieved")
else:
print("❌ New fields are not correctly stored or retrieved")
return False
# Test updating the task with new fields
update_data = {
"priority": "low",
"due_date": "2026-01-15T10:30:00"
}
response = requests.put(f"{BASE_URL}/api/{user_id}/tasks/{task_result['id']}",
json=update_data,
headers=headers)
if response.status_code != 200:
print(f"Failed to update task with new fields: {response.status_code}")
return False
updated_task = response.json()
print(f"Updated task: {updated_task}")
if updated_task.get('priority') == 'low':
print("✅ Task update with new fields works correctly")
else:
print("❌ Task update with new fields failed")
return False
print("\n🎉 All tests passed! New Task fields are working correctly!")
return True
if __name__ == "__main__":
test_new_task_fields() |