ShopTRAINING/test/test_api_models_endpoint.py
2025-07-02 11:05:23 +08:00

73 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify /api/models endpoint functionality
"""
import requests
import json
def test_models_endpoint():
"""Test the /api/models endpoint"""
base_url = "http://localhost:5000"
endpoint = "/api/models"
print("=== Testing /api/models endpoint ===")
try:
# Test basic endpoint
print("1. Testing basic endpoint...")
response = requests.get(f"{base_url}{endpoint}")
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Response status: {data.get('status')}")
print(f"Total models: {data.get('total', 0)}")
models = data.get('data', [])
print(f"Models found: {len(models)}")
for i, model in enumerate(models[:3]): # Show first 3
print(f"\nModel {i+1}:")
print(f" model_id: {model.get('model_id', 'NOT FOUND')}")
print(f" product_id: {model.get('product_id', 'NOT FOUND')}")
print(f" model_type: {model.get('model_type', 'NOT FOUND')}")
print(f" training_mode: {model.get('training_mode', 'NOT FOUND')}")
print(f" version: {model.get('version', 'NOT FOUND')}")
print(f" created_at: {model.get('created_at', 'NOT FOUND')}")
print(f" file_size: {model.get('file_size', 'NOT FOUND')}")
else:
print(f"Error: {response.text}")
# Test with product_id filter
print("\n2. Testing with product_id filter...")
response = requests.get(f"{base_url}{endpoint}?product_id=P001")
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Filtered models: {len(data.get('data', []))}")
# Test with model_type filter
print("\n3. Testing with model_type filter...")
response = requests.get(f"{base_url}{endpoint}?model_type=mlstm")
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"MLSTM models: {len(data.get('data', []))}")
if data.get('data'):
model = data['data'][0]
print(f"First MLSTM model_id: {model.get('model_id', 'NOT FOUND')}")
except requests.exceptions.ConnectionError:
print("Error: Cannot connect to API server. Make sure the server is running on http://localhost:5000")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
test_models_endpoint()