| 1 |
#!/usr/bin/env python |
| 2 |
""" |
| 3 |
Quick script to test the LocalToast API endpoints |
| 4 |
Run from backend directory: python test_api.py |
| 5 |
""" |
| 6 |
import requests |
| 7 |
import json |
| 8 |
|
| 9 |
BASE_URL = "http://localhost:8000/api" |
| 10 |
|
| 11 |
# Test location (NYC) |
| 12 |
TEST_LAT = 40.7128 |
| 13 |
TEST_LNG = -74.0060 |
| 14 |
|
| 15 |
def test_health(): |
| 16 |
"""Test health endpoint""" |
| 17 |
print("🏥 Testing health check...") |
| 18 |
response = requests.get(f"{BASE_URL}/health/") |
| 19 |
print(f"Status: {response.status_code}") |
| 20 |
print(f"Response: {json.dumps(response.json(), indent=2)}\n") |
| 21 |
|
| 22 |
def test_seed(): |
| 23 |
"""Seed some data""" |
| 24 |
print("🌱 Seeding data...") |
| 25 |
response = requests.post(f"{BASE_URL}/seed/", json={ |
| 26 |
"lat": TEST_LAT, |
| 27 |
"lng": TEST_LNG |
| 28 |
}) |
| 29 |
print(f"Status: {response.status_code}") |
| 30 |
print(f"Response: {json.dumps(response.json(), indent=2)}\n") |
| 31 |
|
| 32 |
def test_nearby(): |
| 33 |
"""Test nearby restaurants""" |
| 34 |
print("📍 Testing nearby restaurants...") |
| 35 |
response = requests.get(f"{BASE_URL}/restaurants/nearby/", params={ |
| 36 |
"lat": TEST_LAT, |
| 37 |
"lng": TEST_LNG, |
| 38 |
"radius": 5 |
| 39 |
}) |
| 40 |
print(f"Status: {response.status_code}") |
| 41 |
data = response.json() |
| 42 |
print(f"Found {len(data)} restaurants") |
| 43 |
if data: |
| 44 |
print(f"First restaurant: {data[0]['name']} ({data[0]['distance']}km away)\n") |
| 45 |
|
| 46 |
def test_add_rating(): |
| 47 |
"""Test adding a rating""" |
| 48 |
print("⭐ Testing rating system...") |
| 49 |
# First get a restaurant |
| 50 |
response = requests.get(f"{BASE_URL}/restaurants/") |
| 51 |
restaurants = response.json() |
| 52 |
|
| 53 |
if restaurants: |
| 54 |
restaurant_id = restaurants[0]['id'] |
| 55 |
rating_data = { |
| 56 |
"rating": 5, |
| 57 |
"review": "The french toast here is absolutely amazing! Crispy on the outside, fluffy inside." |
| 58 |
} |
| 59 |
response = requests.post( |
| 60 |
f"{BASE_URL}/restaurants/{restaurant_id}/ratings/", |
| 61 |
json=rating_data |
| 62 |
) |
| 63 |
print(f"Status: {response.status_code}") |
| 64 |
print(f"Response: {json.dumps(response.json(), indent=2)}\n") |
| 65 |
|
| 66 |
def test_search(): |
| 67 |
"""Test place search""" |
| 68 |
print("🔍 Testing place search...") |
| 69 |
response = requests.get(f"{BASE_URL}/search/places/", params={ |
| 70 |
"lat": TEST_LAT, |
| 71 |
"lng": TEST_LNG, |
| 72 |
"radius": 1000 |
| 73 |
}) |
| 74 |
print(f"Status: {response.status_code}") |
| 75 |
data = response.json() |
| 76 |
print(f"Found {len(data)} places") |
| 77 |
if data: |
| 78 |
print(f"First place: {data[0]['name']} (confidence: {data[0].get('confidence', 'unknown')})\n") |
| 79 |
|
| 80 |
if __name__ == "__main__": |
| 81 |
print("🍞 LocalToast API Test Suite\n") |
| 82 |
|
| 83 |
test_health() |
| 84 |
test_seed() |
| 85 |
test_nearby() |
| 86 |
test_add_rating() |
| 87 |
test_search() |
| 88 |
|
| 89 |
print("✅ All tests complete!") |