#!/usr/bin/env python3 """ Test script for station management API endpoints """ import requests import json def test_station_management(): """Test the station management endpoints""" base_url = "http://localhost:8000" print("🧪 Testing Station Management API") print("Make sure the API server is running: python run.py --web-api") print() # Test data for new station new_station = { "station_code": "P.TEST", "thai_name": "สถานีทดสอบ", "english_name": "Test Station", "latitude": 18.7875, "longitude": 99.0045, "geohash": "w5q6uuhvfcfp25", "status": "active" } try: # 1. List existing stations print("1. Listing existing stations...") response = requests.get(f"{base_url}/stations") if response.status_code == 200: stations = response.json() print(f"✅ Found {len(stations)} existing stations") initial_count = len(stations) else: print(f"❌ Failed to list stations: {response.status_code}") return # 2. Create new station print("\n2. Creating new test station...") response = requests.post( f"{base_url}/stations", json=new_station, headers={"Content-Type": "application/json"} ) if response.status_code == 200: created_station = response.json() station_id = created_station["station_id"] print(f"✅ Created station with ID: {station_id}") print(f" Code: {created_station['station_code']}") print(f" Name: {created_station['english_name']}") else: print(f"❌ Failed to create station: {response.status_code}") print(f" Response: {response.text}") return # 3. Get specific station print(f"\n3. Getting station details for ID {station_id}...") response = requests.get(f"{base_url}/stations/{station_id}") if response.status_code == 200: station_details = response.json() print(f"✅ Retrieved station details") print(f" Thai name: {station_details['thai_name']}") print(f" Coordinates: {station_details['latitude']}, {station_details['longitude']}") else: print(f"❌ Failed to get station: {response.status_code}") # 4. Update station print(f"\n4. Updating station {station_id}...") update_data = { "english_name": "Updated Test Station", "thai_name": "สถานีทดสอบที่อัปเดต" } response = requests.put( f"{base_url}/stations/{station_id}", json=update_data, headers={"Content-Type": "application/json"} ) if response.status_code == 200: updated_station = response.json() print(f"✅ Updated station successfully") print(f" New name: {updated_station['english_name']}") else: print(f"❌ Failed to update station: {response.status_code}") print(f" Response: {response.text}") # 5. Verify station count increased print("\n5. Verifying station count...") response = requests.get(f"{base_url}/stations") if response.status_code == 200: stations = response.json() new_count = len(stations) if new_count == initial_count + 1: print(f"✅ Station count increased from {initial_count} to {new_count}") else: print(f"⚠️ Unexpected station count: {new_count} (expected {initial_count + 1})") # 6. Delete test station print(f"\n6. Deleting test station {station_id}...") response = requests.delete(f"{base_url}/stations/{station_id}") if response.status_code == 200: result = response.json() print(f"✅ Deleted station: {result['message']}") else: print(f"❌ Failed to delete station: {response.status_code}") # 7. Verify station was deleted print("\n7. Verifying station deletion...") response = requests.get(f"{base_url}/stations/{station_id}") if response.status_code == 404: print("✅ Station successfully deleted (404 as expected)") else: print(f"⚠️ Station still exists: {response.status_code}") # 8. Final station count check response = requests.get(f"{base_url}/stations") if response.status_code == 200: stations = response.json() final_count = len(stations) if final_count == initial_count: print(f"✅ Station count restored to original: {final_count}") else: print(f"⚠️ Station count mismatch: {final_count} (expected {initial_count})") print("\n🎉 Station management tests completed!") except requests.exceptions.ConnectionError: print("❌ Connection failed - is the API server running?") print("Start it with: python run.py --web-api") except Exception as e: print(f"❌ Test failed with error: {e}") if __name__ == "__main__": test_station_management()