Repository navigation
Expand file tree
/
Copy pathtest_api.py
More file actions
168 lines (148 loc) · 6.44 KB
/
Copy pathtest_api.py
File metadata and controls
168 lines (148 loc) · 6.44 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""
Test suite for the CV CNC Manufacturing Platform API
Run with: python -m pytest test_api.py -v
"""
import pytest
import httpx
import asyncio
from typing import Dict, Any
BASE_URL = "http://localhost:8000"
class TestAPI:
"""Test class for API endpoints"""
@pytest.mark.asyncio
async def test_root_endpoint(self):
"""Test the root endpoint"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/")
assert response.status_code == 200
data = response.json()
assert data["message"] == "Computer Vision CNC Manufacturing Platform"
assert data["service"] == "api"
assert data["version"] == "1.0.0"
assert data["status"] == "running"
assert "timestamp" in data
@pytest.mark.asyncio
async def test_health_endpoint(self):
"""Test the health check endpoint"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/health")
assert response.status_code == 200
data = response.json()
assert data["status"] == "healthy"
assert data["service"] == "api"
assert "checks" in data
assert data["checks"]["database"] == "healthy"
assert data["checks"]["redis"] == "healthy"
@pytest.mark.asyncio
async def test_api_status_endpoint(self):
"""Test the API status endpoint"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/api/status")
assert response.status_code == 200
data = response.json()
assert data["service"] == "api"
assert data["status"] == "ready"
assert "endpoints" in data
assert isinstance(data["endpoints"], list)
@pytest.mark.asyncio
async def test_manufacturing_status_endpoint(self):
"""Test the manufacturing status endpoint"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/api/manufacturing/status")
assert response.status_code == 200
data = response.json()
# Check CNC machines
assert "cnc_machines" in data
cnc = data["cnc_machines"]
assert cnc["connected"] == 1
assert cnc["active"] == 1
assert cnc["total"] == 1
# Check computer vision
assert "computer_vision" in data
cv = data["computer_vision"]
assert cv["status"] == "ready"
assert cv["gpu_enabled"] is False
assert cv["models_loaded"] == 2
# Check quality control
assert "quality_control" in data
qc = data["quality_control"]
assert qc["status"] == "active"
assert qc["defect_detection"] == "enabled"
assert qc["quality_threshold"] == 0.95
# Check production
assert "production" in data
prod = data["production"]
assert prod["mode"] == "test"
assert prod["safety_enabled"] is False
@pytest.mark.asyncio
async def test_cnc_machines_endpoint(self):
"""Test the CNC machines endpoint"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/api/cnc/machines")
assert response.status_code == 200
data = response.json()
assert "machines" in data
machines = data["machines"]
assert len(machines) == 1
machine = machines[0]
assert machine["id"] == "cnc-001"
assert machine["name"] == "Test CNC Machine"
assert machine["status"] == "active"
assert machine["type"] == "mill"
assert "capabilities" in machine
assert "current_job" in machine
assert "metrics" in machine
@pytest.mark.asyncio
async def test_vision_models_endpoint(self):
"""Test the computer vision models endpoint"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/api/vision/models")
assert response.status_code == 200
data = response.json()
assert "models" in data
models = data["models"]
assert len(models) == 2
# Check defect detection model
defect_model = next(m for m in models if m["id"] == "defect-detection-v1")
assert defect_model["name"] == "Defect Detection Model"
assert defect_model["type"] == "YOLO"
assert defect_model["status"] == "loaded"
assert defect_model["accuracy"] == 0.94
# Check quality control model
quality_model = next(m for m in models if m["id"] == "quality-control-v1")
assert quality_model["name"] == "Quality Control Model"
assert quality_model["type"] == "ResNet"
assert quality_model["status"] == "loaded"
assert quality_model["accuracy"] == 0.97
@pytest.mark.asyncio
async def test_nonexistent_endpoint(self):
"""Test that nonexistent endpoints return 404"""
async with httpx.AsyncClient() as client:
response = await client.get(f"{BASE_URL}/api/nonexistent")
assert response.status_code == 404
def test_sync_api_call():
"""Test synchronous API call for comparison"""
import requests
response = requests.get(f"{BASE_URL}/")
assert response.status_code == 200
data = response.json()
assert data["message"] == "Computer Vision CNC Manufacturing Platform"
if __name__ == "__main__":
print("🧪 Running API tests...")
print("Make sure the API server is running: python main.py")
print()
# Run a quick connectivity test
try:
import requests
response = requests.get(f"{BASE_URL}/", timeout=5)
if response.status_code == 200:
print("✅ API server is running")
print("Run tests with: python -m pytest test_api.py -v")
else:
print(f"❌ API server returned status code: {response.status_code}")
except requests.exceptions.ConnectionError:
print("❌ Cannot connect to API server")
print("Make sure to start the server first: python main.py")
except Exception as e:
print(f"❌ Error testing connection: {e}")