-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
59 lines (48 loc) · 1.66 KB
/
Copy pathtest_api.py
File metadata and controls
59 lines (48 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests
import time
import sys
BASE_URL = "http://localhost:8000"
def test_health():
print("Testing Health Check...")
try:
response = requests.get(f"{BASE_URL}/health")
response.raise_for_status()
data = response.json()
print(f"Health check: {data['status']}")
return True
except Exception as e:
print(f"Health check failed: {e}")
return False
def test_single_classification():
print("\nTesting Single Image Classification...")
# Create a dummy image for testing
from PIL import Image
import io
img = Image.new('RGB', (224, 224), color='red')
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr.seek(0)
files = {'file': ('test.png', img_byte_arr, 'image/png')}
try:
start_time = time.time()
response = requests.post(f"{BASE_URL}/api/v1/jobs/classify", files=files)
response.raise_for_status()
data = response.json()
duration = (time.time() - start_time) * 1000
print(f"Prediction: {data['predicted_class']} (Conf: {data['confidence']:.2f})")
print(f"Response time: {duration:.2f}ms")
return True
except Exception as e:
print(f"Classification failed: {e}")
print(response.text if 'response' in locals() else "")
return False
if __name__ == "__main__":
print(f"Running tests against {BASE_URL}")
health_ok = test_health()
class_ok = test_single_classification()
if health_ok and class_ok:
print("\nAll tests passed!")
sys.exit(0)
else:
print("\nSome tests failed.")
sys.exit(1)