|
|
"""
|
|
|
Test script for DCRM API
|
|
|
========================
|
|
|
Simple script to test the API endpoint with the sample CSV file.
|
|
|
"""
|
|
|
|
|
|
import requests
|
|
|
import json
|
|
|
import sys
|
|
|
|
|
|
|
|
|
API_URL = "http://localhost:5000"
|
|
|
BREAKER_ID = "6926e63d4614721a79b7b24e"
|
|
|
CSV_FILE = "df3_final (1).csv"
|
|
|
|
|
|
def test_health_check():
|
|
|
"""Test the health check endpoint"""
|
|
|
print("π Testing health check endpoint...")
|
|
|
try:
|
|
|
response = requests.get(f"{API_URL}/api/health")
|
|
|
if response.status_code == 200:
|
|
|
print("β
Health check passed!")
|
|
|
print(json.dumps(response.json(), indent=2))
|
|
|
return True
|
|
|
else:
|
|
|
print(f"β Health check failed with status {response.status_code}")
|
|
|
return False
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
print("β Cannot connect to API. Make sure the server is running!")
|
|
|
print(" Run: python dcrm_api.py")
|
|
|
return False
|
|
|
except Exception as e:
|
|
|
print(f"β Error: {e}")
|
|
|
return False
|
|
|
|
|
|
def test_analysis():
|
|
|
"""Test the main analysis endpoint"""
|
|
|
print(f"\nπ Testing analysis endpoint with {CSV_FILE}...")
|
|
|
|
|
|
try:
|
|
|
|
|
|
with open(CSV_FILE, 'rb') as f:
|
|
|
files = {'file': (CSV_FILE, f, 'text/csv')}
|
|
|
url = f"{API_URL}/api/circuit-breakers/{BREAKER_ID}/tests/upload"
|
|
|
|
|
|
print(f"π€ Uploading to: {url}")
|
|
|
print("β³ Processing (this may take 30-60 seconds)...")
|
|
|
|
|
|
response = requests.post(url, files=files, timeout=120)
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
print("β
Analysis completed successfully!")
|
|
|
|
|
|
|
|
|
result = response.json()
|
|
|
|
|
|
|
|
|
print("\nπ Key Results:")
|
|
|
print(f" Breaker ID: {result.get('breakerId')}")
|
|
|
print(f" Health Score: {result.get('healthScore')}/100")
|
|
|
print(f" CBHI Score: {result.get('cbhi', {}).get('score')}/100")
|
|
|
print(f" Findings: {result.get('findings')}")
|
|
|
|
|
|
|
|
|
output_file = "test_response.json"
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
|
json.dump(result, f, indent=2, ensure_ascii=False)
|
|
|
print(f"\nπΎ Full response saved to: {output_file}")
|
|
|
|
|
|
return True
|
|
|
else:
|
|
|
print(f"β Analysis failed with status {response.status_code}")
|
|
|
print(f" Error: {response.text}")
|
|
|
return False
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
print(f"β CSV file not found: {CSV_FILE}")
|
|
|
print(" Make sure the file exists in the current directory")
|
|
|
return False
|
|
|
except requests.exceptions.Timeout:
|
|
|
print("β Request timeout. Analysis took too long.")
|
|
|
return False
|
|
|
except Exception as e:
|
|
|
print(f"β Error: {e}")
|
|
|
return False
|
|
|
|
|
|
def test_invalid_file():
|
|
|
"""Test error handling with invalid file"""
|
|
|
print("\nπ Testing error handling with invalid file...")
|
|
|
|
|
|
try:
|
|
|
|
|
|
invalid_content = b"This is not a valid CSV"
|
|
|
files = {'file': ('invalid.txt', invalid_content, 'text/plain')}
|
|
|
url = f"{API_URL}/api/circuit-breakers/{BREAKER_ID}/tests/upload"
|
|
|
|
|
|
response = requests.post(url, files=files)
|
|
|
|
|
|
if response.status_code == 400:
|
|
|
print("β
Error handling works correctly!")
|
|
|
print(f" Error response: {response.json()}")
|
|
|
return True
|
|
|
else:
|
|
|
print(f"β οΈ Unexpected status code: {response.status_code}")
|
|
|
return False
|
|
|
|
|
|
except Exception as e:
|
|
|
print(f"β Error: {e}")
|
|
|
return False
|
|
|
|
|
|
def main():
|
|
|
"""Run all tests"""
|
|
|
print("=" * 60)
|
|
|
print("DCRM API Test Suite")
|
|
|
print("=" * 60)
|
|
|
|
|
|
|
|
|
if not test_health_check():
|
|
|
print("\nβ Health check failed. Stopping tests.")
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
if not test_analysis():
|
|
|
print("\nβ Analysis test failed.")
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
test_invalid_file()
|
|
|
|
|
|
print("\n" + "=" * 60)
|
|
|
print("β
All tests completed!")
|
|
|
print("=" * 60)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main()
|
|
|
|