feat(project): move edge_inference_service contents to root and update paths

- Moved all project files and directories (config, core, models, etc.) from
  edge_inference_service/ to the repository root ai_edge/
- Updated model path in config/settings.py to reflect new structure
- Revised usage paths in __init__.py documentation
This commit is contained in:
2026-01-29 18:43:19 +08:00
parent 6733344b11
commit b0ddb6ee1a
50 changed files with 407 additions and 627 deletions

237
tests/test_utils.py Normal file
View File

@@ -0,0 +1,237 @@
"""
公共工具模块单元测试
"""
import unittest
import time
import sys
import os
from datetime import datetime
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
class TestCommonFunctions(unittest.TestCase):
"""测试公共工具函数"""
def test_generate_unique_id(self):
"""测试生成唯一标识符"""
from utils.common import generate_unique_id
id1 = generate_unique_id()
id2 = generate_unique_id()
self.assertNotEqual(id1, id2)
self.assertTrue(id1.startswith("tf-"))
def test_generate_unique_id_with_prefix(self):
"""测试带前缀的唯一标识符"""
from utils.common import generate_unique_id
id1 = generate_unique_id("camera")
self.assertTrue(id1.startswith("camera_"))
def test_calculate_md5(self):
"""测试MD5计算"""
from utils.common import calculate_md5
result = calculate_md5("hello world")
self.assertEqual(len(result), 32)
self.assertEqual(result, "5eb63bbbe01eeed093cb22bb8f5acdc3")
def test_get_current_timestamp(self):
"""测试获取时间戳"""
from utils.common import get_current_timestamp
result = get_current_timestamp()
self.assertIsInstance(result, str)
self.assertIn(":", result)
def test_get_current_timestamp_ms(self):
"""测试获取毫秒时间戳"""
from utils.common import get_current_timestamp_ms
result = get_current_timestamp_ms()
self.assertIsInstance(result, int)
self.assertGreater(result, 0)
def test_parse_rtsp_url(self):
"""测试解析RTSP URL"""
from utils.common import parse_rtsp_url
result = parse_rtsp_url("rtsp://192.168.1.1:554/stream1")
self.assertEqual(result["protocol"], "rtsp")
self.assertEqual(result["host"], "192.168.1.1")
self.assertEqual(result["port"], "554")
def test_retry_operation_success(self):
"""测试重试成功"""
from utils.common import retry_operation
call_count = [0]
@retry_operation(max_retries=3, delay=0.01)
def success_func():
call_count[0] += 1
return "success"
result = success_func()
self.assertEqual(result, "success")
self.assertEqual(call_count[0], 1)
def test_retry_operation_failure(self):
"""测试重试失败"""
from utils.common import retry_operation
@retry_operation(max_retries=2, delay=0.01)
def fail_func():
raise ValueError("test error")
with self.assertRaises(ValueError):
fail_func()
def test_exponential_backoff(self):
"""测试指数退避"""
from utils.common import ExponentialBackoff
backoff = ExponentialBackoff(
base_delay=0.01,
max_delay=0.1,
max_attempts=3
)
delays = []
for _ in range(3):
delays.append(backoff.get_delay())
backoff.next_attempt()
self.assertEqual(len(delays), 3)
self.assertTrue(delays[0] <= delays[1] <= delays[2])
def test_ensure_directory_exists(self):
"""测试确保目录存在"""
from utils.common import ensure_directory_exists
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
test_path = os.path.join(tmpdir, "test_dir", "sub_dir")
result = ensure_directory_exists(test_path)
self.assertTrue(result)
self.assertTrue(os.path.exists(test_path))
def test_format_file_size(self):
"""测试格式化文件大小"""
from utils.common import format_file_size
self.assertEqual(format_file_size(500), "500.00 B")
self.assertEqual(format_file_size(1024), "1.00 KB")
self.assertEqual(format_file_size(1024 * 1024), "1.00 MB")
self.assertEqual(format_file_size(1024 * 1024 * 1024), "1.00 GB")
class TestPerformanceTimer(unittest.TestCase):
"""测试性能计时器"""
def test_timer_basic(self):
"""测试基本计时"""
from utils.common import PerformanceTimer
timer = PerformanceTimer()
timer.start("test")
time.sleep(0.01)
elapsed = timer.stop("test")
self.assertGreater(elapsed, 0)
self.assertLess(elapsed, 1)
def test_timer_get_elapsed(self):
"""测试获取已记录时间"""
from utils.common import PerformanceTimer
timer = PerformanceTimer()
timer.start("test")
time.sleep(0.01)
timer.stop("test")
elapsed = timer.get_elapsed("test")
self.assertGreater(elapsed, 0)
def test_timer_reset(self):
"""测试重置计时器"""
from utils.common import PerformanceTimer
timer = PerformanceTimer()
timer.start("test")
timer.stop("test")
timer.reset()
self.assertEqual(timer.get_elapsed("test"), 0)
class TestVersionControl(unittest.TestCase):
"""测试版本控制"""
def test_version_control_creation(self):
"""测试版本控制创建"""
from utils.version_control import VersionControl
vc = VersionControl()
self.assertIsNotNone(vc)
def test_record_update(self):
"""测试记录更新"""
from utils.version_control import VersionControl
import tempfile
with tempfile.NamedTemporaryFile(suffix=".md", delete=False) as f:
path = f.name
try:
vc = VersionControl(path)
vc.record_update(
version="1.0.1",
update_type="测试",
description="测试更新",
updated_by="测试人员"
)
history = vc.get_version_history()
self.assertGreater(len(history), 0)
finally:
if os.path.exists(path):
os.remove(path)
def test_get_changelog_content(self):
"""测试获取CHANGELOG内容"""
from utils.version_control import VersionControl
import tempfile
with tempfile.NamedTemporaryFile(suffix=".md", delete=False) as f:
path = f.name
try:
vc = VersionControl(path)
content = vc.get_changelog_content()
self.assertIn("CHANGELOG", content)
finally:
if os.path.exists(path):
os.remove(path)
if __name__ == "__main__":
unittest.main()