CoolFace
Apppublic

saber2164/bms_demo

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
test_api.py62 linesDownload Raw Back to root
1import requests2import json3import time4 5def test_soc_api():6    base_url = "http://localhost:8080"7    8    print("1. Testing Initialization...")9    init_url = f"{base_url}/api/init_filter"10    init_payload = {11        "capacity_ah": 2.0,12        "initial_soc": 0.9,13        "initial_r0": 0.01,14        "dt": 1.015    }16    17    try:18        response = requests.post(init_url, json=init_payload)19        if response.status_code == 200:20            print("Initialization Success!")21            print(json.dumps(response.json(), indent=2))22        else:23            print(f"Initialization Failed: {response.status_code}")24            print(response.text)25            return26    except requests.exceptions.ConnectionError:27        print("Connection Error: Is the server running on port 8080?")28        return29 30    print("\n2. Testing Prediction (Sequence)...")31    predict_url = f"{base_url}/api/predict_soc"32    33    # Sample data: [Voltage, Current, Temperature]34    payload = {35        "features": [36            [4.1, 1.0, 25.0],37            [4.09, 1.0, 25.1],38            [4.08, 1.0, 25.2]39        ]40    }41    42    response = requests.post(predict_url, json=payload)43    44    if response.status_code == 200:45        data = response.json()46        print("Prediction Success!")47        print(json.dumps(data, indent=2))48        49        results = data.get('results', [])50        if len(results) == 3:51            print("\nTEST PASSED: Received 3 SoC estimates.")52        else:53            print(f"\nTEST FAILED: Expected 3 results, got {len(results)}.")54    else:55        print(f"\nTEST FAILED: Status Code {response.status_code}")56        print(response.text)57 58if __name__ == "__main__":59    # Wait a bit for server to start if running in parallel (manual run)60    time.sleep(2) 61    test_soc_api()62