oedb-backend/test_api.py
2025-09-15 23:54:04 +02:00

87 lines
No EOL
2.4 KiB
Python
Executable file

#!/usr/bin/env python3
"""
Test script to verify API endpoints are accessible.
"""
import requests
import sys
import json
import time
BASE_URL = "http://127.0.0.1:8080"
def test_endpoint(endpoint, method="GET", data=None):
"""Test an API endpoint and print the result."""
url = f"{BASE_URL}{endpoint}"
print(f"Testing {method} {url}...")
try:
if method == "GET":
response = requests.get(url)
elif method == "POST":
headers = {"Content-Type": "application/json"}
response = requests.post(url, data=json.dumps(data), headers=headers)
print(f"Status code: {response.status_code}")
if response.status_code < 400:
print("Success!")
else:
print(f"Error: {response.text}")
print("-" * 50)
return response.status_code < 400
except Exception as e:
print(f"Exception: {e}")
print("-" * 50)
return False
def main():
"""Run tests for all endpoints."""
# Wait for server to start
print("Waiting for server to start...")
max_retries = 5
retries = 0
while retries < max_retries:
try:
requests.get(f"{BASE_URL}/")
print("Server is running!")
break
except requests.exceptions.ConnectionError:
print(f"Server not ready yet, retrying in 2 seconds... ({retries+1}/{max_retries})")
retries += 1
time.sleep(2)
if retries == max_retries:
print("Could not connect to server after multiple attempts.")
print("Please make sure the server is running on http://127.0.0.1:8080")
return 1
success = True
# Test root endpoint
success = test_endpoint("/") and success
# Test event endpoint
success = test_endpoint("/event") and success
# Test event/search endpoint with POST
search_data = {
"geometry": {
"type": "Point",
"coordinates": [2.3522, 48.8566] # Paris coordinates
}
}
success = test_endpoint("/event/search", method="POST", data=search_data) and success
# Test stats endpoint
success = test_endpoint("/stats") and success
if success:
print("All tests passed!")
return 0
else:
print("Some tests failed!")
return 1
if __name__ == "__main__":
sys.exit(main())