Files
Northern-Thailand-Ping-Rive…/tests/test_station_management.py
grabowski af62cfef0b
Some checks failed
Security & Dependency Updates / Dependency Security Scan (push) Successful in 29s
Security & Dependency Updates / Docker Security Scan (push) Failing after 53s
Security & Dependency Updates / License Compliance (push) Successful in 13s
Security & Dependency Updates / Check for Dependency Updates (push) Successful in 19s
Security & Dependency Updates / Code Quality Metrics (push) Successful in 11s
Security & Dependency Updates / Security Summary (push) Successful in 7s
Initial commit: Northern Thailand Ping River Monitor v3.1.0
Features:
- Real-time water level monitoring for Ping River Basin (16 stations)
- Coverage from Chiang Dao to Nakhon Sawan in Northern Thailand
- FastAPI web interface with interactive dashboard and station management
- Multi-database support (SQLite, MySQL, PostgreSQL, InfluxDB, VictoriaMetrics)
- Comprehensive monitoring with health checks and metrics collection
- Docker deployment with Grafana integration
- Production-ready architecture with enterprise-grade observability

 CI/CD & Automation:
- Complete Gitea Actions workflows for CI/CD, security, and releases
- Multi-Python version testing (3.9-3.12)
- Multi-architecture Docker builds (amd64, arm64)
- Daily security scanning and dependency monitoring
- Automated documentation generation
- Performance testing and validation

 Production Ready:
- Type safety with Pydantic models and comprehensive type hints
- Data validation layer with range checking and error handling
- Rate limiting and request tracking for API protection
- Enhanced logging with rotation, colors, and performance metrics
- Station management API for dynamic CRUD operations
- Comprehensive documentation and deployment guides

 Technical Stack:
- Python 3.9+ with FastAPI and Pydantic
- Multi-database architecture with adapter pattern
- Docker containerization with multi-stage builds
- Grafana dashboards for visualization
- Gitea Actions for CI/CD automation
- Enterprise monitoring and alerting

 Ready for deployment to B4L infrastructure!
2025-08-12 15:40:24 +07:00

138 lines
5.3 KiB
Python

#!/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()