87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script for the Todo API.
|
|
This script tests the basic functionality of the Todo API.
|
|
"""
|
|
|
|
import sys
|
|
from pprint import pprint
|
|
|
|
import requests
|
|
|
|
|
|
def main():
|
|
"""Main function to test the Todo API."""
|
|
base_url = "http://localhost:8000"
|
|
api_base = f"{base_url}/api/v1"
|
|
todos_url = f"{api_base}/todos"
|
|
|
|
# Test health endpoint
|
|
print("\n=== Testing health endpoint ===")
|
|
health_response = requests.get(f"{base_url}/health")
|
|
pprint(health_response.json())
|
|
|
|
# Test create todo
|
|
print("\n=== Testing create todo ===")
|
|
new_todo = {
|
|
"title": "Test Todo",
|
|
"description": "This is a test todo item",
|
|
"completed": False
|
|
}
|
|
create_response = requests.post(todos_url, json=new_todo)
|
|
if create_response.status_code != 201:
|
|
print(f"Error creating todo: {create_response.status_code}")
|
|
sys.exit(1)
|
|
|
|
created_todo = create_response.json()
|
|
todo_id = created_todo["id"]
|
|
pprint(created_todo)
|
|
|
|
# Test get all todos
|
|
print("\n=== Testing get all todos ===")
|
|
get_all_response = requests.get(todos_url)
|
|
pprint(get_all_response.json())
|
|
|
|
# Test get single todo
|
|
print(f"\n=== Testing get todo with id {todo_id} ===")
|
|
get_single_response = requests.get(f"{todos_url}/{todo_id}")
|
|
pprint(get_single_response.json())
|
|
|
|
# Test update todo
|
|
print(f"\n=== Testing update todo with id {todo_id} ===")
|
|
update_data = {
|
|
"title": "Updated Test Todo",
|
|
"completed": True
|
|
}
|
|
update_response = requests.patch(f"{todos_url}/{todo_id}", json=update_data)
|
|
pprint(update_response.json())
|
|
|
|
# Test filter by completed
|
|
print("\n=== Testing filter by completed status ===")
|
|
filter_response = requests.get(f"{todos_url}?completed=true")
|
|
pprint(filter_response.json())
|
|
|
|
# Test delete todo
|
|
print(f"\n=== Testing delete todo with id {todo_id} ===")
|
|
delete_response = requests.delete(f"{todos_url}/{todo_id}")
|
|
if delete_response.status_code != 204:
|
|
print(f"Error deleting todo: {delete_response.status_code}")
|
|
sys.exit(1)
|
|
print(f"Todo with id {todo_id} deleted successfully")
|
|
|
|
# Verify todo is deleted
|
|
print("\n=== Verifying todo was deleted ===")
|
|
verify_response = requests.get(f"{todos_url}/{todo_id}")
|
|
if verify_response.status_code == 404:
|
|
print(f"Todo with id {todo_id} no longer exists (as expected)")
|
|
else:
|
|
print(
|
|
f"Error: Todo with id {todo_id} still exists "
|
|
f"with status code {verify_response.status_code}"
|
|
)
|
|
|
|
print("\nAll tests completed successfully!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |