Spaces:
Running
Running
File size: 6,126 Bytes
ad1bda5 |
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 |
import sys
import os
import traceback
import importlib.util
def compile_file(filepath):
try:
with open(filepath, 'r', encoding='utf-8') as f:
code = f.read()
compile(code, filepath, 'exec')
return True, None
except SyntaxError as e:
return False, f"Syntax Error: {e.msg} at line {e.lineno}"
except Exception as e:
return False, f"Compilation Error: {str(e)}"
def test_imports():
results = {}
modules = [
('queue_monitor', 'queue_monitor.py'),
('llm_analyzer', 'llm_analyzer.py'),
('utils', 'utils.py'),
('app', 'app.py'),
]
for module_name, filepath in modules:
if not os.path.exists(filepath):
results[module_name] = (False, f"File not found: {filepath}")
continue
try:
spec = importlib.util.spec_from_file_location(module_name, filepath)
if spec is None or spec.loader is None:
results[module_name] = (False, "Could not create module spec")
continue
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
results[module_name] = (True, "Import successful")
except ImportError as e:
results[module_name] = (False, f"Import Error: {str(e)}")
except Exception as e:
results[module_name] = (False, f"Error: {str(e)}\n{traceback.format_exc()}")
return results
def test_queue_monitor():
try:
import supervision
except ImportError:
return True, "QueueMonitor test skipped (supervision not installed)"
try:
from queue_monitor import QueueMonitor
import numpy as np
monitor = QueueMonitor(confidence=0.5, fps=30.0)
dummy_frame = np.zeros((720, 1280, 3), dtype=np.uint8)
polygon = np.array([[100, 100], [600, 100], [600, 600], [100, 600]])
monitor.setup_zones([polygon])
processed, stats = monitor.process_frame(dummy_frame)
assert processed is not None, "Processed frame is None"
assert isinstance(stats, list), "Stats should be a list"
assert len(stats) > 0, "Stats should contain zone data"
return True, "QueueMonitor test passed"
except ImportError as e:
return True, f"QueueMonitor test skipped (missing dependency: {str(e)})"
except Exception as e:
return False, f"QueueMonitor test failed: {str(e)}\n{traceback.format_exc()}"
def test_utils():
try:
from utils import (
is_valid_youtube_url,
extract_video_id,
YT_DOWNLOADER_AVAILABLE
)
test_url = "https://youtu.be/5rkwqp6nnr4?si=itvwJ-oSR0S8xSZQ"
assert is_valid_youtube_url(test_url), "Should validate YouTube URL"
video_id = extract_video_id(test_url)
assert video_id == "5rkwqp6nnr4", f"Expected video ID '5rkwqp6nnr4', got '{video_id}'"
return True, "Utils test passed"
except Exception as e:
return False, f"Utils test failed: {str(e)}\n{traceback.format_exc()}"
def test_app_components():
try:
import app
assert hasattr(app, 'EXAMPLE_VIDEO_URL'), "app should have EXAMPLE_VIDEO_URL"
assert app.EXAMPLE_VIDEO_URL == "https://youtu.be/5rkwqp6nnr4?si=itvwJ-oSR0S8xSZQ", "Example URL should match"
assert hasattr(app, 'QUEUE_MONITOR_AVAILABLE'), "app should have QUEUE_MONITOR_AVAILABLE"
assert hasattr(app, 'LLM_ANALYZER_AVAILABLE'), "app should have LLM_ANALYZER_AVAILABLE"
assert hasattr(app, 'UTILS_AVAILABLE'), "app should have UTILS_AVAILABLE"
return True, "App components test passed"
except Exception as e:
return False, f"App components test failed: {str(e)}\n{traceback.format_exc()}"
def main():
print("=" * 60)
print("COMPILATION AND TEST SUITE")
print("=" * 60)
all_passed = True
print("\n1. COMPILING FILES...")
print("-" * 60)
files_to_check = [
'queue_monitor.py',
'llm_analyzer.py',
'utils.py',
'app.py',
'test_backend.py'
]
for filepath in files_to_check:
if not os.path.exists(filepath):
print(f"β {filepath}: File not found")
all_passed = False
continue
success, error = compile_file(filepath)
if success:
print(f"β
{filepath}: Compilation successful")
else:
print(f"β {filepath}: {error}")
all_passed = False
print("\n2. TESTING IMPORTS...")
print("-" * 60)
import_results = test_imports()
for module_name, (success, message) in import_results.items():
if success:
print(f"β
{module_name}: {message}")
else:
if "No module named" in message or "Import Error" in message:
print(f"β οΈ {module_name}: {message} (expected if dependencies not installed)")
else:
print(f"β {module_name}: {message}")
all_passed = False
print("\n3. TESTING FUNCTIONALITY...")
print("-" * 60)
success, message = test_queue_monitor()
if success:
print(f"β
QueueMonitor: {message}")
else:
print(f"β QueueMonitor: {message}")
all_passed = False
success, message = test_utils()
if success:
print(f"β
Utils: {message}")
else:
print(f"β Utils: {message}")
all_passed = False
success, message = test_app_components()
if success:
print(f"β
App Components: {message}")
else:
print(f"β App Components: {message}")
all_passed = False
print("\n" + "=" * 60)
if all_passed:
print("β
ALL TESTS PASSED")
print("=" * 60)
return 0
else:
print("β SOME TESTS FAILED")
print("=" * 60)
return 1
if __name__ == "__main__":
sys.exit(main())
|