186 lines
6.3 KiB
Python
Executable File
186 lines
6.3 KiB
Python
Executable File
"""
|
|
VoiceLibraryItem 配音库领域模型单元测试
|
|
"""
|
|
|
|
from packages.domain.voice_library import VoiceLibraryItem
|
|
|
|
|
|
class TestVoiceLibraryItem:
|
|
"""VoiceLibraryItem 测试"""
|
|
|
|
def test_create_minimal(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="我的配音")
|
|
assert item.id == "v1"
|
|
assert item.user_id == "u1"
|
|
assert item.name == "我的配音"
|
|
|
|
def test_default_values(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n")
|
|
assert item.text == ""
|
|
assert item.voice_provider == ""
|
|
assert item.voice_id == ""
|
|
assert item.voice_name == ""
|
|
assert item.audio_url == ""
|
|
assert item.duration == 0
|
|
assert item.file_size == 0
|
|
assert item.status == "completed"
|
|
assert item.project_id is None
|
|
assert item.tags == []
|
|
assert item.metadata_ == {}
|
|
|
|
def test_with_voice_info(self):
|
|
item = VoiceLibraryItem(
|
|
id="v1",
|
|
user_id="u1",
|
|
name="温柔女声",
|
|
text="大家好",
|
|
voice_provider="cosyvoice",
|
|
voice_id="longxiaochun_v3",
|
|
voice_name="龙小淳",
|
|
)
|
|
assert item.voice_provider == "cosyvoice"
|
|
assert item.voice_id == "longxiaochun_v3"
|
|
assert item.voice_name == "龙小淳"
|
|
|
|
def test_with_audio_info(self):
|
|
item = VoiceLibraryItem(
|
|
id="v1",
|
|
user_id="u1",
|
|
name="n",
|
|
audio_url="https://example.com/audio.wav",
|
|
duration=15.5,
|
|
file_size=102400,
|
|
)
|
|
assert item.audio_url == "https://example.com/audio.wav"
|
|
assert item.duration == 15.5
|
|
assert item.file_size == 102400
|
|
|
|
def test_with_project_id(self):
|
|
item = VoiceLibraryItem(
|
|
id="v1",
|
|
user_id="u1",
|
|
name="n",
|
|
project_id="proj-123",
|
|
)
|
|
assert item.project_id == "proj-123"
|
|
|
|
def test_status_values(self):
|
|
for status in ["pending", "processing", "completed", "failed"]:
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", status=status)
|
|
assert item.status == status
|
|
|
|
def test_with_tags(self):
|
|
item = VoiceLibraryItem(
|
|
id="v1",
|
|
user_id="u1",
|
|
name="n",
|
|
tags=["温柔", "女声", "解说"],
|
|
)
|
|
assert len(item.tags) == 3
|
|
assert "温柔" in item.tags
|
|
|
|
def test_with_metadata(self):
|
|
item = VoiceLibraryItem(
|
|
id="v1",
|
|
user_id="u1",
|
|
name="n",
|
|
metadata_={"speed": 1.0, "pitch": 0.5},
|
|
)
|
|
assert item.metadata_["speed"] == 1.0
|
|
assert item.metadata_["pitch"] == 0.5
|
|
|
|
def test_has_timestamps(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n")
|
|
assert item.created_at is not None
|
|
assert item.updated_at is not None
|
|
|
|
|
|
class TestVoiceLibraryItemExtended:
|
|
"""VoiceLibraryItem 深度补充测试"""
|
|
|
|
def test_zero_duration(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", duration=0)
|
|
assert item.duration == 0
|
|
|
|
def test_negative_duration(self):
|
|
"""负数 duration 领域层不校验"""
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", duration=-1.5)
|
|
assert item.duration == -1.5
|
|
|
|
def test_large_duration(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", duration=9999.99)
|
|
assert item.duration == 9999.99
|
|
|
|
def test_zero_file_size(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", file_size=0)
|
|
assert item.file_size == 0
|
|
|
|
def test_large_file_size(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", file_size=10**9)
|
|
assert item.file_size == 10**9
|
|
|
|
def test_empty_audio_url(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", audio_url="")
|
|
assert item.audio_url == ""
|
|
|
|
def test_tags_independence(self):
|
|
item1 = VoiceLibraryItem(id="v1", user_id="u1", name="n1")
|
|
item2 = VoiceLibraryItem(id="v2", user_id="u1", name="n2")
|
|
item1.tags.append("新标签")
|
|
assert "新标签" not in item2.tags
|
|
assert len(item2.tags) == 0
|
|
|
|
def test_metadata_independence(self):
|
|
item1 = VoiceLibraryItem(id="v1", user_id="u1", name="n1")
|
|
item2 = VoiceLibraryItem(id="v2", user_id="u1", name="n2")
|
|
item1.metadata_["key"] = "val"
|
|
assert "key" not in item2.metadata_
|
|
|
|
def test_many_tags(self):
|
|
tags = [f"tag_{i}" for i in range(30)]
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", tags=tags)
|
|
assert len(item.tags) == 30
|
|
assert item.tags[0] == "tag_0"
|
|
|
|
def test_empty_text(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", text="")
|
|
assert item.text == ""
|
|
|
|
def test_long_text(self):
|
|
long_text = "这是一段很长的配音文本" * 100
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", text=long_text)
|
|
assert item.text == long_text
|
|
assert len(item.text) == 1100
|
|
|
|
def test_empty_voice_id(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", voice_id="")
|
|
assert item.voice_id == ""
|
|
|
|
def test_empty_project_id(self):
|
|
"""project_id 默认是 None 不是空字符串"""
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n")
|
|
assert item.project_id is None
|
|
|
|
def test_project_id_with_string(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", project_id="")
|
|
# 传空字符串的话就是空字符串
|
|
assert item.project_id == ""
|
|
|
|
def test_status_empty_string(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="n", status="")
|
|
assert item.status == ""
|
|
|
|
def test_unicode_name(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name="🎙️ 专业配音 · 龙小淳")
|
|
assert "🎙️" in item.name
|
|
assert "龙小淳" in item.name
|
|
|
|
def test_special_characters_in_name(self):
|
|
special = "配!@#$%^&*()音"
|
|
item = VoiceLibraryItem(id="v1", user_id="u1", name=special)
|
|
assert item.name == special
|
|
|
|
def test_empty_user_id(self):
|
|
item = VoiceLibraryItem(id="v1", user_id="", name="n")
|
|
assert item.user_id == ""
|