diagram_ph/scripts/run_api_tests.py

59 lines
1.8 KiB
Python

import requests, json, base64, os
base = 'http://127.0.0.1:8001'
print('Health ->', requests.get(base + '/api/v1/health').json())
# Diagram JSON
body = {
'refrigerant': 'R290',
'pressure_range': {'min': 0.1, 'max': 10.0},
'format': 'json',
'include_isotherms': True,
'width': 800,
'height': 600,
'dpi': 100
}
print('\nRequesting diagram JSON...')
r = requests.post(base + '/api/v1/diagrams/ph', json=body, timeout=60)
print('Status', r.status_code)
try:
j = r.json()
print('Keys in response:', list(j.keys()))
if 'data' in j:
print('Saturation curve length:', len(j['data'].get('saturation_curve', [])))
except Exception as e:
print('Failed to parse JSON:', e, r.text[:200])
# Diagram PNG
body['format'] = 'png'
print('\nRequesting diagram PNG...')
r2 = requests.post(base + '/api/v1/diagrams/ph', json=body, timeout=60)
print('Status', r2.status_code)
try:
j2 = r2.json()
print('Keys in response:', list(j2.keys()))
if 'image' in j2:
img_b64 = j2['image']
os.makedirs('test_outputs', exist_ok=True)
path = os.path.join('test_outputs','sample_diagram.png')
with open(path, 'wb') as f:
f.write(base64.b64decode(img_b64))
print('Saved PNG to', path)
except ValueError:
print('Response is not JSON, printing text length', len(r2.text))
# Simple cycle (pressure mode)
print('\nRequesting simple cycle...')
cycle_body = {
'refrigerant': 'R290',
'evap_pressure': 0.2, # bar
'cond_pressure': 6.0, # bar
'superheat': 5.0,
'subcool': 2.0,
'mass_flow': 0.1
}
r3 = requests.post(base + '/api/v1/cycles/simple', json=cycle_body, timeout=60)
print('Status', r3.status_code)
try:
print('Simple cycle keys:', list(r3.json().keys()))
except Exception as e:
print('Failed to parse cycle response:', e, r3.text[:200])