Files
xiaoxia-saas/tests/unit/test_noise_reduction_engine.py
T
CI Bot 7a72cfd709
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Validate - Migration (alembic) (push) Successful in 2m9s
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / Validate - Type Check (mypy) (push) Successful in 2m19s
CI/CD Pipeline / Validate - Code Quality (push) Failing after 3m10s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 3m7s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 3m7s
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 1m17s
CI/CD Pipeline / Frontend Lint (push) Successful in 3m37s
CI/CD Pipeline / Integration Tests (push) Successful in 2m18s
CI/CD Pipeline / Unit Tests (push) Failing after 4m59s
CI/CD Pipeline / Build Staging API Image (push) Successful in 8m39s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 3m25s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 42s
CI/CD Pipeline / Staging E2E Tests (push) Failing after 27m2s
CI/CD Pipeline / Staging API Integration Tests (push) Failing after 30m15s
style: auto-format with black + isort + prettier
2026-07-25 00:16:10 +00:00

233 lines
7.5 KiB
Python
Executable File

"""降噪引擎单元测试 - 配置解析等纯逻辑."""
from __future__ import annotations
import pytest
from video_processing.noise_reduction_engine import (
NoiseReductionConfig,
NoiseReductionLevel,
)
class TestNoiseReductionLevel:
"""降噪等级枚举测试."""
def test_level_values(self):
"""等级枚举值正确."""
assert NoiseReductionLevel.LOW.value == "low"
assert NoiseReductionLevel.MEDIUM.value == "medium"
assert NoiseReductionLevel.HIGH.value == "high"
assert NoiseReductionLevel.CUSTOM.value == "custom"
def test_from_string(self):
"""从字符串创建."""
assert NoiseReductionLevel("low") == NoiseReductionLevel.LOW
assert NoiseReductionLevel("medium") == NoiseReductionLevel.MEDIUM
assert NoiseReductionLevel("high") == NoiseReductionLevel.HIGH
assert NoiseReductionLevel("custom") == NoiseReductionLevel.CUSTOM
def test_invalid_string_raises(self):
"""无效字符串抛异常."""
with pytest.raises(ValueError):
NoiseReductionLevel("invalid")
class TestNoiseReductionConfigDefaults:
"""默认配置测试."""
def test_default_values(self):
"""默认值正确."""
config = NoiseReductionConfig()
assert config.enabled is False
assert config.level == NoiseReductionLevel.MEDIUM
assert config.noise_floor == -25.0
assert config.voice_enhance is False
class TestNoiseReductionConfigFromDict:
"""from_dict 配置解析测试."""
def test_none_returns_disabled(self):
"""None 返回禁用配置."""
config = NoiseReductionConfig.from_dict(None)
assert config.enabled is False
def test_empty_dict_returns_disabled(self):
"""空字典返回禁用配置."""
config = NoiseReductionConfig.from_dict({})
assert config.enabled is False
def test_disabled_returns_disabled(self):
"""enabled=False 返回禁用."""
config = NoiseReductionConfig.from_dict({"enabled": False})
assert config.enabled is False
def test_enabled_default_level(self):
"""启用时默认等级为 medium."""
config = NoiseReductionConfig.from_dict({"enabled": True})
assert config.enabled is True
assert config.level == NoiseReductionLevel.MEDIUM
def test_level_low(self):
"""low 等级."""
config = NoiseReductionConfig.from_dict({"enabled": True, "level": "low"})
assert config.level == NoiseReductionLevel.LOW
def test_level_high(self):
"""high 等级."""
config = NoiseReductionConfig.from_dict({"enabled": True, "level": "high"})
assert config.level == NoiseReductionLevel.HIGH
def test_level_custom(self):
"""custom 等级."""
config = NoiseReductionConfig.from_dict({"enabled": True, "level": "custom"})
assert config.level == NoiseReductionLevel.CUSTOM
def test_level_case_insensitive(self):
"""等级大小写不敏感."""
config = NoiseReductionConfig.from_dict({"enabled": True, "level": "HIGH"})
assert config.level == NoiseReductionLevel.HIGH
def test_invalid_level_falls_back_to_medium(self):
"""无效等级 fallback 到 medium."""
config = NoiseReductionConfig.from_dict({"enabled": True, "level": "ultra"})
assert config.level == NoiseReductionLevel.MEDIUM
def test_noise_floor_parsed(self):
"""噪音阈值解析."""
config = NoiseReductionConfig.from_dict(
{
"enabled": True,
"level": "custom",
"noise_floor": -30.0,
}
)
assert config.noise_floor == -30.0
def test_noise_floor_clamped_min(self):
"""噪音阈值下限钳制 (-60)."""
config = NoiseReductionConfig.from_dict(
{
"enabled": True,
"level": "custom",
"noise_floor": -100.0,
}
)
assert config.noise_floor == -60.0
def test_noise_floor_clamped_max(self):
"""噪音阈值上限钳制 (-5)."""
config = NoiseReductionConfig.from_dict(
{
"enabled": True,
"level": "custom",
"noise_floor": 0.0,
}
)
assert config.noise_floor == -5.0
def test_noise_floor_boundary_low(self):
"""噪音阈值边界值 -60."""
config = NoiseReductionConfig.from_dict(
{
"enabled": True,
"level": "custom",
"noise_floor": -60.0,
}
)
assert config.noise_floor == -60.0
def test_noise_floor_boundary_high(self):
"""噪音阈值边界值 -5."""
config = NoiseReductionConfig.from_dict(
{
"enabled": True,
"level": "custom",
"noise_floor": -5.0,
}
)
assert config.noise_floor == -5.0
def test_invalid_noise_floor_falls_back(self):
"""无效噪音阈值 fallback 到默认值."""
config = NoiseReductionConfig.from_dict(
{
"enabled": True,
"level": "custom",
"noise_floor": "not_a_number",
}
)
assert config.noise_floor == -25.0
def test_voice_enhance_enabled(self):
"""人声增强启用."""
config = NoiseReductionConfig.from_dict(
{
"enabled": True,
"voice_enhance": True,
}
)
assert config.voice_enhance is True
def test_voice_enhance_disabled_default(self):
"""人声增强默认禁用."""
config = NoiseReductionConfig.from_dict({"enabled": True})
assert config.voice_enhance is False
class TestHasEffect:
"""has_effect 方法测试."""
def test_disabled_no_effect(self):
"""禁用时无效果."""
config = NoiseReductionConfig(enabled=False)
assert config.has_effect() is False
def test_enabled_has_effect(self):
"""启用时有效果."""
config = NoiseReductionConfig(enabled=True)
assert config.has_effect() is True
class TestGetEffectiveNoiseFloor:
"""get_effective_noise_floor 方法测试."""
def test_custom_level_returns_noise_floor(self):
"""custom 等级返回配置的 noise_floor."""
config = NoiseReductionConfig(
enabled=True,
level=NoiseReductionLevel.CUSTOM,
noise_floor=-35.0,
)
assert config.get_effective_noise_floor() == -35.0
def test_low_level_returns_params(self):
"""low 等级返回对应参数值."""
config = NoiseReductionConfig(
enabled=True,
level=NoiseReductionLevel.LOW,
)
result = config.get_effective_noise_floor()
assert isinstance(result, float)
assert result < 0 # dB值为负数
def test_medium_level_returns_params(self):
"""medium 等级返回对应参数值."""
config = NoiseReductionConfig(
enabled=True,
level=NoiseReductionLevel.MEDIUM,
)
result = config.get_effective_noise_floor()
assert isinstance(result, float)
assert result < 0
def test_high_level_returns_params(self):
"""high 等级返回对应参数值."""
config = NoiseReductionConfig(
enabled=True,
level=NoiseReductionLevel.HIGH,
)
result = config.get_effective_noise_floor()
assert isinstance(result, float)
assert result < 0