forked from darachubarova/deflicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
198 lines (155 loc) · 6.2 KB
/
test_api.py
File metadata and controls
198 lines (155 loc) · 6.2 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
API endpoint validation tests.
These tests verify that the API endpoints are properly defined
and can be imported without runtime errors.
"""
import sys
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent))
def test_api_structure():
"""Test that the API can be imported and has expected structure."""
print("\n" + "="*60)
print("Testing API Structure")
print("="*60)
try:
# Try importing with mock dependencies if needed
import unittest.mock as mock
# Mock external dependencies
sys.modules['cv2'] = mock.MagicMock()
sys.modules['torch'] = mock.MagicMock()
sys.modules['torchvision'] = mock.MagicMock()
sys.modules['torchvision.transforms'] = mock.MagicMock()
sys.modules['torchvision.models'] = mock.MagicMock()
sys.modules['torchvision.models.segmentation'] = mock.MagicMock()
from src.main import app
print("✓ FastAPI app imported successfully")
# Check that app has expected attributes
assert hasattr(app, 'routes'), "App should have routes"
print(f"✓ API has {len(app.routes)} routes defined")
# List all routes
print("\nDefined Routes:")
for route in app.routes:
if hasattr(route, 'path') and hasattr(route, 'methods'):
methods = ', '.join(route.methods) if route.methods else 'N/A'
print(f" {methods:12} {route.path}")
# Check expected endpoints exist
expected_paths = [
'/',
'/api/upload',
'/api/segment',
'/api/stabilize',
'/api/status/{job_id}',
'/api/results/{job_id}',
'/api/metrics/{job_id}',
'/api/frames/{job_id}/{frame_type}/{frame_num}',
'/api/classes',
'/api/job/{job_id}',
]
actual_paths = [route.path for route in app.routes if hasattr(route, 'path')]
print("\nExpected Endpoints:")
for path in expected_paths:
# Handle path parameters
found = any(
path.replace('{', '').replace('}', '') in actual_path.replace('{', '').replace('}', '')
for actual_path in actual_paths
)
status = "✓" if found else "✗"
print(f" {status} {path}")
return True
except ImportError as e:
print(f"⚠ Could not fully import API (dependencies missing): {e}")
return False
except Exception as e:
print(f"✗ Error testing API: {e}")
import traceback
traceback.print_exc()
return False
def test_pydantic_models():
"""Test that Pydantic models can be imported."""
print("\n" + "="*60)
print("Testing Pydantic Models")
print("="*60)
try:
import unittest.mock as mock
# Mock external dependencies
sys.modules['cv2'] = mock.MagicMock()
sys.modules['torch'] = mock.MagicMock()
sys.modules['torchvision'] = mock.MagicMock()
from src.main import SegmentRequest, StabilizeRequest, JobStatus
print("✓ SegmentRequest model imported")
print("✓ StabilizeRequest model imported")
print("✓ JobStatus model imported")
# Test model instantiation
seg_req = SegmentRequest(job_id="test-123", target_class="person")
print(f"✓ SegmentRequest can be instantiated: {seg_req.job_id}")
stab_req = StabilizeRequest(
job_id="test-123",
method="moving_average",
window_size=5
)
print(f"✓ StabilizeRequest can be instantiated: {stab_req.method}")
return True
except ImportError as e:
print(f"⚠ Could not import models (dependencies missing): {e}")
return False
except Exception as e:
print(f"✗ Error testing models: {e}")
return False
def test_module_functions():
"""Test that individual module functions work."""
print("\n" + "="*60)
print("Testing Module Functions")
print("="*60)
try:
import numpy as np
from src.metrics import calculate_iou, compare_stability
from src.stabilization import MaskStabilizer
# Test IoU calculation
mask1 = np.ones((10, 10), dtype=np.uint8)
mask2 = np.ones((10, 10), dtype=np.uint8)
iou = calculate_iou(mask1, mask2)
assert iou == 1.0, "IoU of identical masks should be 1.0"
print(f"✓ IoU calculation works: {iou}")
# Test stabilization
masks = [np.random.rand(10, 10).astype(np.float32) for _ in range(5)]
smoothed = MaskStabilizer.moving_average(masks, window_size=3)
assert len(smoothed) == len(masks), "Output should have same length"
print(f"✓ Moving average stabilization works")
# Test metrics comparison
metrics = compare_stability(masks, smoothed)
assert 'iou_before' in metrics, "Metrics should contain iou_before"
assert 'iou_after' in metrics, "Metrics should contain iou_after"
print(f"✓ Metrics comparison works")
return True
except Exception as e:
print(f"✗ Error testing functions: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Run all tests."""
print("="*60)
print("API Validation Test Suite")
print("="*60)
results = {
'API Structure': test_api_structure(),
'Pydantic Models': test_pydantic_models(),
'Module Functions': test_module_functions(),
}
print("\n" + "="*60)
print("Test Results Summary")
print("="*60)
for test_name, passed in results.items():
status = "✓ PASS" if passed else "✗ FAIL"
print(f"{status:8} {test_name}")
all_passed = all(results.values())
print("\n" + "="*60)
if all_passed:
print("✓ All tests passed!")
else:
print("⚠ Some tests failed (may be due to missing dependencies)")
print("="*60)
return 0 if all_passed else 1
if __name__ == '__main__':
sys.exit(main())