Files
security-ai-edge/tests/test_config_sync.py

203 lines
6.1 KiB
Python
Raw Normal View History

2026-01-29 18:33:12 +08:00
"""
配置同步模块单元测试
"""
import unittest
from unittest.mock import MagicMock, patch
from datetime import datetime
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
class TestConfigCache(unittest.TestCase):
"""测试配置缓存"""
def test_cache_set_get(self):
"""测试缓存设置和获取"""
from config.config_models import ConfigCache
cache = ConfigCache()
cache.set("test_key", {"data": "test_value"})
result = cache.get("test_key")
self.assertIsNotNone(result)
self.assertEqual(result["data"], "test_value")
def test_cache_miss(self):
"""测试缓存未命中"""
from config.config_models import ConfigCache
cache = ConfigCache()
result = cache.get("non_existent")
self.assertIsNone(result)
def test_cache_delete(self):
"""测试缓存删除"""
from config.config_models import ConfigCache
cache = ConfigCache()
cache.set("test_key", "test_value")
cache.delete("test_key")
result = cache.get("test_key")
self.assertIsNone(result)
def test_cache_clear(self):
"""测试缓存清空"""
from config.config_models import ConfigCache
cache = ConfigCache()
cache.set("key1", "value1")
cache.set("key2", "value2")
cache.clear()
self.assertIsNone(cache.get("key1"))
self.assertIsNone(cache.get("key2"))
class TestCameraInfoModel(unittest.TestCase):
"""测试摄像头信息模型"""
def test_camera_info_to_dict(self):
"""测试转换为字典"""
from config.config_models import CameraInfo
camera = CameraInfo(
camera_id="cam001",
camera_name="测试摄像头",
rtsp_url="rtsp://192.168.1.1:554/stream1",
status=True,
enabled=True,
location="门口"
)
result = camera.to_dict()
self.assertEqual(result["camera_id"], "cam001")
self.assertEqual(result["camera_name"], "测试摄像头")
self.assertEqual(result["rtsp_url"], "rtsp://192.168.1.1:554/stream1")
self.assertTrue(result["status"])
self.assertTrue(result["enabled"])
def test_camera_info_from_dict(self):
"""测试从字典创建"""
from config.config_models import CameraInfo
data = {
"camera_id": "cam002",
"camera_name": "另一个摄像头",
"rtsp_url": "rtsp://192.168.1.2:554/stream1",
"status": False,
"enabled": True,
}
camera = CameraInfo.from_dict(data)
self.assertEqual(camera.camera_id, "cam002")
self.assertEqual(camera.camera_name, "另一个摄像头")
class TestROIInfoModel(unittest.TestCase):
"""测试ROI信息模型"""
def test_roi_info_to_dict(self):
"""测试转换为字典"""
from config.config_models import ROIInfo, ROIType, AlgorithmType
roi = ROIInfo(
roi_id="roi001",
camera_id="cam001",
roi_type=ROIType.POLYGON,
coordinates=[[100, 100], [200, 100], [200, 200], [100, 200]],
algorithm_type=AlgorithmType.LEAVE_POST,
alert_threshold=3
)
result = roi.to_dict()
self.assertEqual(result["roi_id"], "roi001")
self.assertEqual(result["roi_type"], "polygon")
self.assertEqual(result["algorithm_type"], "leave_post")
self.assertEqual(result["alert_threshold"], 3)
def test_point_in_rectangle(self):
"""测试点在矩形内"""
from config.config_models import ROIInfo, ROIType
roi = ROIInfo(
roi_id="roi001",
camera_id="cam001",
roi_type=ROIType.RECTANGLE,
coordinates=[[0, 0], [100, 100]],
algorithm_type=AlgorithmType.LEAVE_POST,
)
self.assertTrue(roi.is_point_inside([50, 50]))
self.assertTrue(roi.is_point_inside([0, 0]))
self.assertTrue(roi.is_point_inside([100, 100]))
self.assertFalse(roi.is_point_inside([150, 150]))
def test_point_in_polygon(self):
"""测试点多边形内"""
from config.config_models import ROIInfo, ROIType, AlgorithmType
roi = ROIInfo(
roi_id="roi001",
camera_id="cam001",
roi_type=ROIType.POLYGON,
coordinates=[[0, 0], [100, 0], [100, 100], [0, 100]],
algorithm_type=AlgorithmType.LEAVE_POST,
)
self.assertTrue(roi.is_point_inside([50, 50]))
self.assertTrue(roi.is_point_inside([0, 0]))
self.assertFalse(roi.is_point_inside([150, 50]))
class TestAlertInfoModel(unittest.TestCase):
"""测试告警信息模型"""
def test_alert_info_to_dict(self):
"""测试转换为字典"""
from config.config_models import AlertInfo, AlertLevel
alert = AlertInfo(
alert_id="alert001",
camera_id="cam001",
roi_id="roi001",
alert_type="leave_post",
target_class="person",
confidence=0.95,
bbox=[100, 100, 200, 200],
message="离岗告警",
level=AlertLevel.HIGH
)
result = alert.to_dict()
self.assertEqual(result["alert_id"], "alert001")
self.assertEqual(result["alert_type"], "leave_post")
self.assertEqual(result["level"], "high")
self.assertEqual(result["confidence"], 0.95)
def test_alert_info_to_json(self):
"""测试转换为JSON"""
from config.config_models import AlertInfo
alert = AlertInfo(
alert_id="alert001",
camera_id="cam001",
roi_id="roi001",
alert_type="intrusion"
)
json_str = alert.to_json()
self.assertIn("alert001", json_str)
self.assertIn("intrusion", json_str)
if __name__ == "__main__":
unittest.main()