Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 945a85dcf9 | |||
| 0677ba3e71 | |||
| eb4b1add9c | |||
| e032a36732 | |||
| e594b8c5c1 | |||
| 7abb9bea1c | |||
| f9f899b29f |
@@ -0,0 +1,421 @@
|
||||
"""domain层小模块批量单测.
|
||||
|
||||
覆盖:recipe / tag / editing_mode / voice_library / title_library / template / edit_template
|
||||
共 7 个模块,纯逻辑 0 外部依赖。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.editing_mode import EditingMode
|
||||
from packages.domain.edit_template import EditTemplate, EditTemplateStatus
|
||||
from packages.domain.recipe import Recipe, RecipeItem
|
||||
from packages.domain.tag import Tag
|
||||
from packages.domain.template import Template, TemplateCategory, TemplateSegment
|
||||
from packages.domain.title_library import TitleLibraryItem
|
||||
from packages.domain.voice_library import VoiceLibraryItem
|
||||
|
||||
|
||||
class TestEditingMode:
|
||||
"""EditingMode 枚举测试."""
|
||||
|
||||
def test_four_modes(self):
|
||||
"""四种剪辑模式."""
|
||||
assert len(EditingMode) == 4
|
||||
|
||||
def test_one_take(self):
|
||||
assert EditingMode.ONE_TAKE == "one_take"
|
||||
|
||||
def test_pip(self):
|
||||
assert EditingMode.PIP == "pip"
|
||||
|
||||
def test_voice_over(self):
|
||||
assert EditingMode.VOICE_OVER == "voice_over"
|
||||
|
||||
def test_voice_pip(self):
|
||||
assert EditingMode.VOICE_PIP == "voice_pip"
|
||||
|
||||
def test_all_values_unique(self):
|
||||
values = [m.value for m in EditingMode]
|
||||
assert len(values) == len(set(values))
|
||||
|
||||
|
||||
class TestTag:
|
||||
"""Tag 测试."""
|
||||
|
||||
def test_create_valid(self):
|
||||
"""正常创建."""
|
||||
tag = Tag.create(user_id="u1", name=" 搞笑 ")
|
||||
assert tag.user_id == "u1"
|
||||
assert tag.name == "搞笑"
|
||||
assert isinstance(tag.id, str)
|
||||
assert len(tag.id) > 0
|
||||
assert tag.created_at is not None
|
||||
|
||||
def test_create_empty_name(self):
|
||||
"""空名称无效."""
|
||||
try:
|
||||
Tag.create("u1", "")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_create_whitespace_name(self):
|
||||
"""纯空白名称无效."""
|
||||
try:
|
||||
Tag.create("u1", " ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_create_unique_id(self):
|
||||
t1 = Tag.create("u", "t1")
|
||||
t2 = Tag.create("u", "t2")
|
||||
assert t1.id != t2.id
|
||||
|
||||
|
||||
class TestRecipeItem:
|
||||
"""RecipeItem 测试."""
|
||||
|
||||
def test_create_asset_item(self):
|
||||
"""素材配方项."""
|
||||
item = RecipeItem(
|
||||
id="item1",
|
||||
recipe_id="r1",
|
||||
item_type="asset",
|
||||
item_id="a1",
|
||||
position=0,
|
||||
)
|
||||
assert item.item_type == "asset"
|
||||
assert item.item_id == "a1"
|
||||
assert item.position == 0
|
||||
|
||||
def test_create_title_item(self):
|
||||
"""标题配方项."""
|
||||
item = RecipeItem(id="i2", recipe_id="r1", item_type="title", item_id="t1", position=1)
|
||||
assert item.item_type == "title"
|
||||
assert item.position == 1
|
||||
|
||||
def test_default_metadata(self):
|
||||
"""默认 metadata 为空 dict."""
|
||||
item = RecipeItem(id="i1", recipe_id="r1", item_type="voice", item_id="v1")
|
||||
assert item.metadata_ == {}
|
||||
|
||||
|
||||
class TestRecipe:
|
||||
"""Recipe 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简配方."""
|
||||
recipe = Recipe(id="r1", user_id="u1", name="我的配方")
|
||||
assert recipe.name == "我的配方"
|
||||
assert recipe.description == ""
|
||||
assert recipe.template_id == ""
|
||||
assert recipe.generation_params == {}
|
||||
assert recipe.items == []
|
||||
assert recipe.is_active is True
|
||||
assert recipe.created_at is not None
|
||||
assert recipe.updated_at is not None
|
||||
|
||||
def test_create_with_items(self):
|
||||
"""带配方项."""
|
||||
items = [
|
||||
RecipeItem(id="i1", recipe_id="r1", item_type="asset", item_id="a1", position=0),
|
||||
RecipeItem(id="i2", recipe_id="r1", item_type="title", item_id="t1", position=1),
|
||||
]
|
||||
recipe = Recipe(id="r1", user_id="u1", name="配方", items=items)
|
||||
assert len(recipe.items) == 2
|
||||
assert recipe.items[0].item_type == "asset"
|
||||
assert recipe.items[1].position == 1
|
||||
|
||||
def test_default_items_empty_list(self):
|
||||
"""默认 items 为空列表."""
|
||||
r1 = Recipe(id="r1", user_id="u1", name="r1")
|
||||
r2 = Recipe(id="r2", user_id="u2", name="r2")
|
||||
r1.items.append("fake")
|
||||
assert r2.items == []
|
||||
|
||||
|
||||
class TestVoiceLibraryItem:
|
||||
"""VoiceLibraryItem 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
item = VoiceLibraryItem(id="v1", user_id="u1", name="我的配音")
|
||||
assert item.name == "我的配音"
|
||||
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_create_full(self):
|
||||
"""带全部字段."""
|
||||
item = VoiceLibraryItem(
|
||||
id="v1",
|
||||
user_id="u1",
|
||||
name="旁白",
|
||||
text="大家好",
|
||||
voice_provider="xf",
|
||||
voice_id="v1",
|
||||
voice_name="小云",
|
||||
audio_url="http://x/a.mp3",
|
||||
duration=10.5,
|
||||
file_size=102400,
|
||||
status="processing",
|
||||
project_id="p1",
|
||||
tags=["旁白", "正式"],
|
||||
)
|
||||
assert item.text == "大家好"
|
||||
assert item.duration == 10.5
|
||||
assert item.file_size == 102400
|
||||
assert item.project_id == "p1"
|
||||
assert item.tags == ["旁白", "正式"]
|
||||
|
||||
def test_tags_independent(self):
|
||||
"""不同实例的 tags 独立."""
|
||||
i1 = VoiceLibraryItem(id="v1", user_id="u", name="n1")
|
||||
i2 = VoiceLibraryItem(id="v2", user_id="u", name="n2")
|
||||
i1.tags.append("x")
|
||||
assert i2.tags == []
|
||||
|
||||
|
||||
class TestTitleLibraryItem:
|
||||
"""TitleLibraryItem 测试."""
|
||||
|
||||
def test_create_required(self):
|
||||
"""必填字段."""
|
||||
item = TitleLibraryItem(id="t1", user_id="u1", name="爆款标题", text="这也太牛了")
|
||||
assert item.name == "爆款标题"
|
||||
assert item.text == "这也太牛了"
|
||||
assert item.category == "default"
|
||||
assert item.description == ""
|
||||
assert item.tags == []
|
||||
assert item.usage_count == 0
|
||||
assert item.is_active is True
|
||||
assert item.metadata_ == {}
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
item = TitleLibraryItem(
|
||||
id="t1",
|
||||
user_id="u1",
|
||||
name="科技标题",
|
||||
text="震惊!",
|
||||
category="tech",
|
||||
description="科技类标题",
|
||||
tags=["科技", "爆款"],
|
||||
usage_count=100,
|
||||
is_active=False,
|
||||
)
|
||||
assert item.category == "tech"
|
||||
assert item.usage_count == 100
|
||||
assert item.is_active is False
|
||||
assert item.tags == ["科技", "爆款"]
|
||||
|
||||
def test_tags_independent(self):
|
||||
i1 = TitleLibraryItem(id="t1", user_id="u", name="n", text="t")
|
||||
i2 = TitleLibraryItem(id="t2", user_id="u", name="n", text="t")
|
||||
i1.tags.append("x")
|
||||
assert i2.tags == []
|
||||
|
||||
|
||||
class TestTemplateSegment:
|
||||
"""TemplateSegment 测试."""
|
||||
|
||||
def test_create(self):
|
||||
"""正常创建."""
|
||||
seg = TemplateSegment(
|
||||
id="s1",
|
||||
template_id="t1",
|
||||
segment_order=0,
|
||||
duration_min=2.0,
|
||||
duration_max=5.0,
|
||||
)
|
||||
assert seg.segment_order == 0
|
||||
assert seg.duration_min == 2.0
|
||||
assert seg.duration_max == 5.0
|
||||
assert seg.material_type is None
|
||||
|
||||
def test_with_material_type(self):
|
||||
"""带素材类型(voice_over模式)."""
|
||||
seg = TemplateSegment(
|
||||
id="s1",
|
||||
template_id="t1",
|
||||
segment_order=0,
|
||||
duration_min=3.0,
|
||||
duration_max=8.0,
|
||||
material_type="person",
|
||||
)
|
||||
assert seg.material_type == "person"
|
||||
|
||||
def test_has_timestamps(self):
|
||||
seg = TemplateSegment(id="s1", template_id="t1", segment_order=0, duration_min=1, duration_max=2)
|
||||
assert seg.created_at is not None
|
||||
assert seg.updated_at is not None
|
||||
|
||||
|
||||
class TestTemplate:
|
||||
"""Template 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简模板."""
|
||||
tpl = Template(id="t1", user_id="u1", name="通用模板", mode="one_take")
|
||||
assert tpl.name == "通用模板"
|
||||
assert tpl.mode == "one_take"
|
||||
assert tpl.category == ""
|
||||
assert tpl.tags == []
|
||||
assert tpl.title_config == {}
|
||||
assert tpl.subtitle_config == {}
|
||||
assert tpl.bgm_config == {}
|
||||
assert tpl.estimated_duration == 0.0
|
||||
assert tpl.segments == []
|
||||
assert tpl.is_active is True
|
||||
|
||||
def test_create_with_segments(self):
|
||||
"""带片段."""
|
||||
segs = [
|
||||
TemplateSegment(id="s1", template_id="t1", segment_order=0, duration_min=2, duration_max=5),
|
||||
TemplateSegment(id="s2", template_id="t1", segment_order=1, duration_min=3, duration_max=7),
|
||||
]
|
||||
tpl = Template(id="t1", user_id="u1", name="模板", mode="pip", segments=segs)
|
||||
assert len(tpl.segments) == 2
|
||||
assert tpl.segments[0].segment_order == 0
|
||||
|
||||
def test_segments_independent(self):
|
||||
t1 = Template(id="t1", user_id="u", name="n1", mode="one_take")
|
||||
t2 = Template(id="t2", user_id="u", name="n2", mode="pip")
|
||||
t1.segments.append("fake")
|
||||
assert t2.segments == []
|
||||
|
||||
|
||||
class TestTemplateCategory:
|
||||
"""TemplateCategory 测试."""
|
||||
|
||||
def test_create(self):
|
||||
cat = TemplateCategory(id="c1", user_id="u1", name="科技")
|
||||
assert cat.name == "科技"
|
||||
assert cat.created_at is not None
|
||||
|
||||
|
||||
class TestEditTemplateStatus:
|
||||
"""EditTemplateStatus 枚举测试."""
|
||||
|
||||
def test_two_statuses(self):
|
||||
assert len(EditTemplateStatus) == 2
|
||||
|
||||
def test_active(self):
|
||||
assert EditTemplateStatus.ACTIVE == "active"
|
||||
|
||||
def test_inactive(self):
|
||||
assert EditTemplateStatus.INACTIVE == "inactive"
|
||||
|
||||
|
||||
class TestEditTemplate:
|
||||
"""EditTemplate 测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
tpl = EditTemplate.create(name=" 通用模板 ")
|
||||
assert tpl.name == "通用模板"
|
||||
assert tpl.description == ""
|
||||
assert tpl.template_type == "default"
|
||||
assert tpl.config == {}
|
||||
assert tpl.preview_url == ""
|
||||
assert tpl.sort_weight == 0
|
||||
assert tpl.status == EditTemplateStatus.ACTIVE
|
||||
assert isinstance(tpl.id, str)
|
||||
assert len(tpl.id) > 0
|
||||
|
||||
def test_create_full(self):
|
||||
"""带全部字段."""
|
||||
tpl = EditTemplate.create(
|
||||
name="口播模板",
|
||||
description="口播类模板",
|
||||
template_type="voice_over",
|
||||
config={"style": "formal"},
|
||||
preview_url="https://x/preview.mp4",
|
||||
sort_weight=100,
|
||||
status=EditTemplateStatus.INACTIVE,
|
||||
)
|
||||
assert tpl.name == "口播模板"
|
||||
assert tpl.description == "口播类模板"
|
||||
assert tpl.template_type == "voice_over"
|
||||
assert tpl.config == {"style": "formal"}
|
||||
assert tpl.preview_url == "https://x/preview.mp4"
|
||||
assert tpl.sort_weight == 100
|
||||
assert tpl.status == EditTemplateStatus.INACTIVE
|
||||
|
||||
def test_create_empty_name(self):
|
||||
"""空名称无效."""
|
||||
try:
|
||||
EditTemplate.create(name="")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_create_whitespace_name(self):
|
||||
"""空白名称无效."""
|
||||
try:
|
||||
EditTemplate.create(name=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "不能为空" in str(e)
|
||||
|
||||
def test_empty_template_type_defaults(self):
|
||||
"""空 template_type 默认 default."""
|
||||
tpl = EditTemplate.create(name="t", template_type="")
|
||||
assert tpl.template_type == "default"
|
||||
|
||||
def test_whitespace_template_type_defaults(self):
|
||||
"""空白 template_type 默认 default."""
|
||||
tpl = EditTemplate.create(name="t", template_type=" ")
|
||||
assert tpl.template_type == "default"
|
||||
|
||||
def test_config_none_defaults_empty(self):
|
||||
"""config=None 默认为空 dict."""
|
||||
tpl = EditTemplate.create(name="t", config=None)
|
||||
assert tpl.config == {}
|
||||
|
||||
def test_activate(self):
|
||||
"""激活."""
|
||||
tpl = EditTemplate.create(name="t", status=EditTemplateStatus.INACTIVE)
|
||||
old = tpl.updated_at
|
||||
tpl.activate()
|
||||
assert tpl.is_active is True
|
||||
assert tpl.status == EditTemplateStatus.ACTIVE
|
||||
assert tpl.updated_at >= old
|
||||
|
||||
def test_deactivate(self):
|
||||
"""停用."""
|
||||
tpl = EditTemplate.create(name="t")
|
||||
old = tpl.updated_at
|
||||
tpl.deactivate()
|
||||
assert tpl.is_active is False
|
||||
assert tpl.status == EditTemplateStatus.INACTIVE
|
||||
assert tpl.updated_at >= old
|
||||
|
||||
def test_is_active_property(self):
|
||||
"""is_active 属性."""
|
||||
tpl = EditTemplate.create(name="t")
|
||||
assert tpl.is_active is True
|
||||
tpl.deactivate()
|
||||
assert tpl.is_active is False
|
||||
tpl.activate()
|
||||
assert tpl.is_active is True
|
||||
|
||||
def test_unique_id(self):
|
||||
t1 = EditTemplate.create(name="t1")
|
||||
t2 = EditTemplate.create(name="t2")
|
||||
assert t1.id != t2.id
|
||||
|
||||
def test_config_independent(self):
|
||||
t1 = EditTemplate.create(name="t1")
|
||||
t2 = EditTemplate.create(name="t2")
|
||||
t1.config["k"] = "v"
|
||||
assert "k" not in t2.config
|
||||
@@ -0,0 +1,170 @@
|
||||
"""generated_video 单测.
|
||||
|
||||
domain 层生成视频实体纯逻辑模块,0 外部依赖。
|
||||
覆盖:create工厂/校验、默认值、数据完整性。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.generated_video import GeneratedVideo
|
||||
|
||||
|
||||
class TestGeneratedVideoCreate:
|
||||
"""GeneratedVideo.create 工厂测试."""
|
||||
|
||||
def test_create_required_fields(self):
|
||||
"""最简创建(仅必填字段)."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id="proj1",
|
||||
generation_task_id="task1",
|
||||
name="我的视频",
|
||||
file_url="https://cdn.example.com/v.mp4",
|
||||
)
|
||||
assert v.project_id == "proj1"
|
||||
assert v.generation_task_id == "task1"
|
||||
assert v.name == "我的视频"
|
||||
assert v.file_url == "https://cdn.example.com/v.mp4"
|
||||
assert isinstance(v.id, str)
|
||||
assert len(v.id) > 0
|
||||
|
||||
def test_create_defaults(self):
|
||||
"""默认值正确."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id="p1",
|
||||
generation_task_id="t1",
|
||||
name="v",
|
||||
file_url="http://x/v.mp4",
|
||||
)
|
||||
assert v.file_size == 0
|
||||
assert v.duration == 0.0
|
||||
assert v.width == 0
|
||||
assert v.height == 0
|
||||
assert v.fps == 0.0
|
||||
assert v.thumbnail_url is None
|
||||
assert v.status == "completed"
|
||||
assert v.review_status == "pending_review"
|
||||
assert v.generation_params == {}
|
||||
assert v.video_fingerprint is None
|
||||
assert v.is_duplicate is False
|
||||
assert v.duplicate_of is None
|
||||
|
||||
def test_create_with_all_fields(self):
|
||||
"""带全部字段创建."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id="proj1",
|
||||
generation_task_id="task1",
|
||||
name=" 测试视频 ",
|
||||
file_url=" https://cdn.example.com/v.mp4 ",
|
||||
file_size=1024000,
|
||||
duration=120.5,
|
||||
width=1920,
|
||||
height=1080,
|
||||
fps=30.0,
|
||||
thumbnail_url="https://cdn.example.com/thumb.jpg",
|
||||
generation_params={"template": "tpl1", "bgm": "bgm1"},
|
||||
)
|
||||
assert v.name == "测试视频" # strip
|
||||
assert v.file_url == "https://cdn.example.com/v.mp4" # strip
|
||||
assert v.file_size == 1024000
|
||||
assert v.duration == 120.5
|
||||
assert v.width == 1920
|
||||
assert v.height == 1080
|
||||
assert v.fps == 30.0
|
||||
assert v.thumbnail_url == "https://cdn.example.com/thumb.jpg"
|
||||
assert v.generation_params == {"template": "tpl1", "bgm": "bgm1"}
|
||||
|
||||
def test_create_strips_fields(self):
|
||||
"""字符串字段会 strip."""
|
||||
v = GeneratedVideo.create(
|
||||
project_id=" p1 ",
|
||||
generation_task_id=" t1 ",
|
||||
name=" v ",
|
||||
file_url=" http://x/v ",
|
||||
)
|
||||
assert v.project_id == "p1"
|
||||
assert v.generation_task_id == "t1"
|
||||
assert v.name == "v"
|
||||
assert v.file_url == "http://x/v"
|
||||
|
||||
def test_create_empty_project_id(self):
|
||||
"""空 project_id 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("", "t1", "v", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_whitespace_project_id(self):
|
||||
"""纯空白 project_id 无效."""
|
||||
try:
|
||||
GeneratedVideo.create(" ", "t1", "v", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_empty_task_id(self):
|
||||
"""空 generation_task_id 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "", "v", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "generation_task_id" in str(e)
|
||||
|
||||
def test_create_empty_name(self):
|
||||
"""空 name 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "t1", "", "http://x/v")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "name" in str(e)
|
||||
|
||||
def test_create_empty_file_url(self):
|
||||
"""空 file_url 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "t1", "v", "")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "file_url" in str(e)
|
||||
|
||||
def test_create_whitespace_file_url(self):
|
||||
"""纯空白 file_url 无效."""
|
||||
try:
|
||||
GeneratedVideo.create("p1", "t1", "v", " ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "file_url" in str(e)
|
||||
|
||||
def test_create_generation_params_none_defaults_empty(self):
|
||||
"""generation_params=None 默认为空 dict."""
|
||||
v = GeneratedVideo.create("p1", "t1", "v", "http://x/v", generation_params=None)
|
||||
assert v.generation_params == {}
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同视频 id 不同."""
|
||||
v1 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
v2 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
assert v1.id != v2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有生成和创建时间."""
|
||||
v = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
assert v.generated_at is not None
|
||||
assert v.created_at is not None
|
||||
|
||||
def test_zero_dimensions_valid(self):
|
||||
"""宽高为 0 合法(未指定分辨率)."""
|
||||
v = GeneratedVideo.create("p", "t", "v", "http://x/v", width=0, height=0)
|
||||
assert v.width == 0
|
||||
assert v.height == 0
|
||||
|
||||
def test_zero_fps_valid(self):
|
||||
"""fps 为 0 合法."""
|
||||
v = GeneratedVideo.create("p", "t", "v", "http://x/v", fps=0.0)
|
||||
assert v.fps == 0.0
|
||||
|
||||
def test_generation_params_independent(self):
|
||||
"""不同实例的 generation_params 独立."""
|
||||
v1 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
v2 = GeneratedVideo.create("p", "t", "v", "http://x/v")
|
||||
v1.generation_params["key"] = "value"
|
||||
assert "key" not in v2.generation_params
|
||||
@@ -0,0 +1,208 @@
|
||||
"""generation_task 单测.
|
||||
|
||||
domain 层生成任务实体纯逻辑模块,0 外部依赖。
|
||||
覆盖:枚举、create工厂/校验、列表拷贝。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.generation_task import GenerationTask, GenerationTaskStatus
|
||||
|
||||
|
||||
class TestGenerationTaskStatus:
|
||||
"""GenerationTaskStatus 枚举测试."""
|
||||
|
||||
def test_five_statuses(self):
|
||||
"""五种状态."""
|
||||
assert len(GenerationTaskStatus) == 5
|
||||
|
||||
def test_pending(self):
|
||||
assert GenerationTaskStatus.PENDING == "pending"
|
||||
|
||||
def test_running(self):
|
||||
assert GenerationTaskStatus.RUNNING == "running"
|
||||
|
||||
def test_completed(self):
|
||||
assert GenerationTaskStatus.COMPLETED == "completed"
|
||||
|
||||
def test_failed(self):
|
||||
assert GenerationTaskStatus.FAILED == "failed"
|
||||
|
||||
def test_cancelled(self):
|
||||
assert GenerationTaskStatus.CANCELLED == "cancelled"
|
||||
|
||||
|
||||
class TestGenerationTaskCreate:
|
||||
"""GenerationTask.create 工厂测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建(project_id + asset_library_id)."""
|
||||
task = GenerationTask.create(project_id="proj1", asset_library_id="lib1")
|
||||
assert task.project_id == "proj1"
|
||||
assert task.asset_library_id == "lib1"
|
||||
assert task.status == GenerationTaskStatus.PENDING
|
||||
assert task.progress == 0.0
|
||||
assert task.result_count == 0
|
||||
assert task.error_message == ""
|
||||
assert task.asset_ids == []
|
||||
assert task.title_ids == []
|
||||
assert task.voice_ids == []
|
||||
assert isinstance(task.id, str)
|
||||
assert len(task.id) > 0
|
||||
|
||||
def test_create_with_all_fields(self):
|
||||
"""带全部字段创建."""
|
||||
task = GenerationTask.create(
|
||||
project_id=" proj1 ",
|
||||
asset_library_id=" lib1 ",
|
||||
strategy_id=" strat1 ",
|
||||
voice_library_id=" vlib1 ",
|
||||
template_id=" tpl1 ",
|
||||
asset_ids=["a1", "a2", "a3"],
|
||||
title_ids=["t1", "t2"],
|
||||
voice_ids=["v1"],
|
||||
created_by_user_id=" user1 ",
|
||||
source_edit_plan_id=" plan1 ",
|
||||
asset_select_mode="random",
|
||||
batch_id="batch1",
|
||||
)
|
||||
assert task.project_id == "proj1"
|
||||
assert task.asset_library_id == "lib1"
|
||||
assert task.strategy_id == "strat1"
|
||||
assert task.voice_library_id == "vlib1"
|
||||
assert task.template_id == "tpl1"
|
||||
assert task.asset_ids == ["a1", "a2", "a3"]
|
||||
assert task.title_ids == ["t1", "t2"]
|
||||
assert task.voice_ids == ["v1"]
|
||||
assert task.created_by_user_id == "user1"
|
||||
assert task.source_edit_plan_id == "plan1"
|
||||
assert task.asset_select_mode == "random"
|
||||
assert task.batch_id == "batch1"
|
||||
|
||||
def test_create_with_template_instead_of_project(self):
|
||||
"""有 template_id 但 project_id 为空也可以."""
|
||||
task = GenerationTask.create(
|
||||
project_id="",
|
||||
asset_library_id="lib1",
|
||||
template_id="tpl1",
|
||||
)
|
||||
assert task.template_id == "tpl1"
|
||||
assert task.project_id == ""
|
||||
|
||||
def test_create_neither_project_nor_template(self):
|
||||
"""project_id 和 template_id 都为空,抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id="", asset_library_id="lib1")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e) and "template_id" in str(e)
|
||||
|
||||
def test_create_whitespace_project_and_template(self):
|
||||
"""都是空白也抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id=" ", asset_library_id="lib1", template_id=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e) and "template_id" in str(e)
|
||||
|
||||
def test_create_no_asset_library_and_no_ids(self):
|
||||
"""asset_library_id 为空且没有素材列表,抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id="p1", asset_library_id="")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_library_id" in str(e)
|
||||
|
||||
def test_create_whitespace_asset_library_and_no_ids(self):
|
||||
"""空白 asset_library 且无素材列表,抛错."""
|
||||
try:
|
||||
GenerationTask.create(project_id="p1", asset_library_id=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_library_id" in str(e)
|
||||
|
||||
def test_create_with_asset_ids_instead_of_library(self):
|
||||
"""用 asset_ids 替代 asset_library_id."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="",
|
||||
asset_ids=["a1", "a2"],
|
||||
)
|
||||
assert task.asset_library_id == ""
|
||||
assert task.asset_ids == ["a1", "a2"]
|
||||
|
||||
def test_create_with_title_ids_instead_of_library(self):
|
||||
"""用 title_ids 替代 asset_library_id."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="",
|
||||
title_ids=["t1"],
|
||||
)
|
||||
assert task.title_ids == ["t1"]
|
||||
|
||||
def test_create_with_voice_ids_instead_of_library(self):
|
||||
"""用 voice_ids 替代 asset_library_id."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="",
|
||||
voice_ids=["v1"],
|
||||
)
|
||||
assert task.voice_ids == ["v1"]
|
||||
|
||||
def test_create_asset_ids_copied(self):
|
||||
"""asset_ids 是拷贝不是引用."""
|
||||
original = ["a1", "a2"]
|
||||
task = GenerationTask.create(project_id="p1", asset_library_id="lib1", asset_ids=original)
|
||||
original.append("a3")
|
||||
assert task.asset_ids == ["a1", "a2"]
|
||||
|
||||
def test_create_title_ids_copied(self):
|
||||
"""title_ids 是拷贝不是引用."""
|
||||
original = ["t1"]
|
||||
task = GenerationTask.create(project_id="p1", asset_library_id="lib1", title_ids=original)
|
||||
original.append("t2")
|
||||
assert task.title_ids == ["t1"]
|
||||
|
||||
def test_create_voice_ids_copied(self):
|
||||
"""voice_ids 是拷贝不是引用."""
|
||||
original = ["v1"]
|
||||
task = GenerationTask.create(project_id="p1", asset_library_id="lib1", voice_ids=original)
|
||||
original.append("v2")
|
||||
assert task.voice_ids == ["v1"]
|
||||
|
||||
def test_create_none_lists_default_empty(self):
|
||||
"""None 列表默认为空."""
|
||||
task = GenerationTask.create(
|
||||
project_id="p1",
|
||||
asset_library_id="lib1",
|
||||
asset_ids=None,
|
||||
title_ids=None,
|
||||
voice_ids=None,
|
||||
)
|
||||
assert task.asset_ids == []
|
||||
assert task.title_ids == []
|
||||
assert task.voice_ids == []
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同任务 id 不同."""
|
||||
t1 = GenerationTask.create("p", "l")
|
||||
t2 = GenerationTask.create("p", "l")
|
||||
assert t1.id != t2.id
|
||||
|
||||
def test_create_has_created_at(self):
|
||||
"""有创建时间."""
|
||||
task = GenerationTask.create("p", "l")
|
||||
assert task.created_at is not None
|
||||
|
||||
def test_create_defaults_started_completed_none(self):
|
||||
"""started_at 和 completed_at 默认 None."""
|
||||
task = GenerationTask.create("p", "l")
|
||||
assert task.started_at is None
|
||||
assert task.completed_at is None
|
||||
|
||||
def test_empty_lists_independent(self):
|
||||
"""不同任务的空列表互不影响."""
|
||||
t1 = GenerationTask.create("p", "l")
|
||||
t2 = GenerationTask.create("p", "l")
|
||||
t1.asset_ids.append("x")
|
||||
assert t2.asset_ids == []
|
||||
Reference in New Issue
Block a user