Files
FrameTour-RenderWorker/tests/test_effects/test_base.py
2025-09-24 09:21:03 +08:00

188 lines
6.0 KiB
Python

"""测试特效基础类和注册表"""
import pytest
from unittest.mock import Mock
from entity.effects.base import EffectProcessor, EffectRegistry
class TestEffectProcessor:
"""测试特效处理器基类"""
def test_init(self):
"""测试初始化"""
processor = Mock(spec=EffectProcessor)
processor.__init__("test_params", {"key": "value"})
assert processor.params == "test_params"
assert processor.ext_data == {"key": "value"}
assert processor.frame_rate == 25
def test_init_default_ext_data(self):
"""测试默认扩展数据"""
processor = Mock(spec=EffectProcessor)
processor.__init__("test_params")
assert processor.ext_data == {}
def test_parse_params_empty(self):
"""测试解析空参数"""
processor = EffectProcessor("", {})
result = processor.parse_params()
assert result == []
def test_parse_params_single(self):
"""测试解析单个参数"""
processor = EffectProcessor("123", {})
result = processor.parse_params()
assert result == ["123"]
def test_parse_params_multiple(self):
"""测试解析多个参数"""
processor = EffectProcessor("1,2.5,test", {})
result = processor.parse_params()
assert result == ["1", "2.5", "test"]
def test_get_pos_json_empty(self):
"""测试获取空位置JSON"""
processor = EffectProcessor("", {})
result = processor.get_pos_json()
assert result == {}
def test_get_pos_json_valid(self):
"""测试获取有效位置JSON"""
ext_data = {
"posJson": '{"ltX": 100, "ltY": 200, "rbX": 300, "rbY": 400}'
}
processor = EffectProcessor("", ext_data)
result = processor.get_pos_json()
expected = {"ltX": 100, "ltY": 200, "rbX": 300, "rbY": 400}
assert result == expected
def test_get_pos_json_invalid(self):
"""测试获取无效位置JSON"""
ext_data = {"posJson": "invalid_json"}
processor = EffectProcessor("", ext_data)
result = processor.get_pos_json()
assert result == {}
def test_get_pos_json_default_value(self):
"""测试默认位置JSON值"""
ext_data = {"posJson": "{}"}
processor = EffectProcessor("", ext_data)
result = processor.get_pos_json()
assert result == {}
class TestEffectRegistry:
"""测试特效注册表"""
def test_init(self):
"""测试初始化"""
registry = EffectRegistry()
assert registry._processors == {}
def test_register_valid_processor(self):
"""测试注册有效处理器"""
registry = EffectRegistry()
class TestEffect(EffectProcessor):
def validate_params(self):
return True
def generate_filter_args(self, video_input, effect_index):
return [], video_input
def get_effect_name(self):
return "test"
registry.register("test_effect", TestEffect)
assert "test_effect" in registry._processors
assert registry._processors["test_effect"] == TestEffect
def test_register_invalid_processor(self):
"""测试注册无效处理器"""
registry = EffectRegistry()
class InvalidEffect:
pass
with pytest.raises(ValueError, match="must be a subclass of EffectProcessor"):
registry.register("invalid", InvalidEffect)
def test_get_processor_exists(self):
"""测试获取存在的处理器"""
registry = EffectRegistry()
class TestEffect(EffectProcessor):
def validate_params(self):
return True
def generate_filter_args(self, video_input, effect_index):
return [], video_input
def get_effect_name(self):
return "test"
registry.register("test_effect", TestEffect)
processor = registry.get_processor("test_effect", "params", {"data": "value"})
assert isinstance(processor, TestEffect)
assert processor.params == "params"
assert processor.ext_data == {"data": "value"}
def test_get_processor_not_exists(self):
"""测试获取不存在的处理器"""
registry = EffectRegistry()
processor = registry.get_processor("nonexistent")
assert processor is None
def test_list_effects_empty(self):
"""测试列出空特效"""
registry = EffectRegistry()
effects = registry.list_effects()
assert effects == []
def test_list_effects_with_processors(self):
"""测试列出已注册特效"""
registry = EffectRegistry()
class TestEffect(EffectProcessor):
def validate_params(self):
return True
def generate_filter_args(self, video_input, effect_index):
return [], video_input
def get_effect_name(self):
return "test"
registry.register("effect1", TestEffect)
registry.register("effect2", TestEffect)
effects = registry.list_effects()
assert set(effects) == {"effect1", "effect2"}
def test_parse_effect_string_with_params(self):
"""测试解析带参数的特效字符串"""
registry = EffectRegistry()
name, params = registry.parse_effect_string("zoom:0,2.0,3.0")
assert name == "zoom"
assert params == "0,2.0,3.0"
def test_parse_effect_string_without_params(self):
"""测试解析无参数的特效字符串"""
registry = EffectRegistry()
name, params = registry.parse_effect_string("zoom")
assert name == "zoom"
assert params == ""
def test_parse_effect_string_multiple_colons(self):
"""测试解析多个冒号的特效字符串"""
registry = EffectRegistry()
name, params = registry.parse_effect_string("effect:param1:param2")
assert name == "effect"
assert params == "param1:param2"