Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| af60c9cc4b | |||
| 53f85d42bf | |||
| f9c2b43e61 | |||
| d47d495803 | |||
| 9bbfea8b90 | |||
| eb65fcc609 |
@@ -0,0 +1,181 @@
|
||||
"""classification 单测.
|
||||
|
||||
domain 层素材分类模块纯逻辑,0 外部依赖。
|
||||
覆盖:4个枚举 + ClassificationJob 工厂/校验。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.classification import (
|
||||
AssetClassification,
|
||||
AssetLibraryKind,
|
||||
ClassificationJob,
|
||||
ClassificationJobStatus,
|
||||
IngestJobStatus,
|
||||
)
|
||||
|
||||
|
||||
class TestAssetLibraryKind:
|
||||
"""AssetLibraryKind 枚举测试."""
|
||||
|
||||
def test_two_values(self):
|
||||
"""视频和配音两类."""
|
||||
assert len(AssetLibraryKind) == 2
|
||||
|
||||
def test_video(self):
|
||||
assert AssetLibraryKind.VIDEO == "video"
|
||||
|
||||
def test_voice(self):
|
||||
assert AssetLibraryKind.VOICE == "voice"
|
||||
|
||||
def test_str_compatible(self):
|
||||
"""StrEnum 字符串兼容."""
|
||||
assert AssetLibraryKind.VIDEO == "video"
|
||||
|
||||
|
||||
class TestIngestJobStatus:
|
||||
"""IngestJobStatus 枚举测试."""
|
||||
|
||||
def test_four_statuses(self):
|
||||
assert len(IngestJobStatus) == 4
|
||||
|
||||
def test_pending(self):
|
||||
assert IngestJobStatus.PENDING == "pending"
|
||||
|
||||
def test_processing(self):
|
||||
assert IngestJobStatus.PROCESSING == "processing"
|
||||
|
||||
def test_completed(self):
|
||||
assert IngestJobStatus.COMPLETED == "completed"
|
||||
|
||||
def test_failed(self):
|
||||
assert IngestJobStatus.FAILED == "failed"
|
||||
|
||||
|
||||
class TestClassificationJobStatus:
|
||||
"""ClassificationJobStatus 枚举测试."""
|
||||
|
||||
def test_four_statuses(self):
|
||||
assert len(ClassificationJobStatus) == 4
|
||||
|
||||
def test_pending(self):
|
||||
assert ClassificationJobStatus.PENDING == "pending"
|
||||
|
||||
def test_processing(self):
|
||||
assert ClassificationJobStatus.PROCESSING == "processing"
|
||||
|
||||
def test_completed(self):
|
||||
assert ClassificationJobStatus.COMPLETED == "completed"
|
||||
|
||||
def test_failed(self):
|
||||
assert ClassificationJobStatus.FAILED == "failed"
|
||||
|
||||
def test_same_values_as_ingest(self):
|
||||
"""两种任务状态值相同."""
|
||||
assert set(ClassificationJobStatus) == set(IngestJobStatus)
|
||||
|
||||
|
||||
class TestAssetClassification:
|
||||
"""AssetClassification 枚举测试."""
|
||||
|
||||
def test_nine_categories(self):
|
||||
"""9个分类."""
|
||||
assert len(AssetClassification) == 9
|
||||
|
||||
def test_scenic(self):
|
||||
assert AssetClassification.SCENIC == "scenic"
|
||||
|
||||
def test_product(self):
|
||||
assert AssetClassification.PRODUCT == "product"
|
||||
|
||||
def test_person(self):
|
||||
assert AssetClassification.PERSON == "person"
|
||||
|
||||
def test_animal(self):
|
||||
assert AssetClassification.ANIMAL == "animal"
|
||||
|
||||
def test_food(self):
|
||||
assert AssetClassification.FOOD == "food"
|
||||
|
||||
def test_tech(self):
|
||||
assert AssetClassification.TECH == "tech"
|
||||
|
||||
def test_sport(self):
|
||||
assert AssetClassification.SPORT == "sport"
|
||||
|
||||
def test_music(self):
|
||||
assert AssetClassification.MUSIC == "music"
|
||||
|
||||
def test_other(self):
|
||||
assert AssetClassification.OTHER == "other"
|
||||
|
||||
def test_all_values_unique(self):
|
||||
"""所有分类值唯一."""
|
||||
values = [c.value for c in AssetClassification]
|
||||
assert len(values) == len(set(values))
|
||||
|
||||
|
||||
class TestClassificationJobCreate:
|
||||
"""ClassificationJob.create 测试."""
|
||||
|
||||
def test_create_valid(self):
|
||||
"""正常创建."""
|
||||
job = ClassificationJob.create(project_id="proj1", asset_id="asset1")
|
||||
assert job.project_id == "proj1"
|
||||
assert job.asset_id == "asset1"
|
||||
assert job.status == ClassificationJobStatus.PENDING
|
||||
assert job.classification == ""
|
||||
assert job.confidence == 0.0
|
||||
assert job.error_message == ""
|
||||
assert isinstance(job.id, str)
|
||||
assert len(job.id) > 0
|
||||
|
||||
def test_create_strips(self):
|
||||
"""project_id 和 asset_id 会 strip."""
|
||||
job = ClassificationJob.create(project_id=" proj1 ", asset_id=" asset1 ")
|
||||
assert job.project_id == "proj1"
|
||||
assert job.asset_id == "asset1"
|
||||
|
||||
def test_create_empty_project_id(self):
|
||||
"""空 project_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id="", asset_id="a1")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_whitespace_project_id(self):
|
||||
"""纯空白 project_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id=" ", asset_id="a1")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "project_id" in str(e)
|
||||
|
||||
def test_create_empty_asset_id(self):
|
||||
"""空 asset_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id="p1", asset_id="")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_create_whitespace_asset_id(self):
|
||||
"""纯空白 asset_id 无效."""
|
||||
try:
|
||||
ClassificationJob.create(project_id="p1", asset_id=" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同 job id 不同."""
|
||||
j1 = ClassificationJob.create("p", "a")
|
||||
j2 = ClassificationJob.create("p", "a")
|
||||
assert j1.id != j2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
job = ClassificationJob.create("p", "a")
|
||||
assert job.created_at is not None
|
||||
assert job.updated_at is not None
|
||||
@@ -0,0 +1,362 @@
|
||||
"""edit_plan_clip 单测.
|
||||
|
||||
domain 层剪辑计划片段纯逻辑模块,0 外部依赖。
|
||||
覆盖:枚举常量、create工厂/校验、素材分配、状态流转、属性计算。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from packages.domain.edit_plan_clip import EditPlanClip, EditPlanClipStatus
|
||||
|
||||
|
||||
class TestEditPlanClipStatus:
|
||||
"""EditPlanClipStatus 枚举测试."""
|
||||
|
||||
def test_four_statuses(self):
|
||||
"""四种状态."""
|
||||
assert len(EditPlanClipStatus) == 4
|
||||
|
||||
def test_pending(self):
|
||||
"""pending 状态."""
|
||||
assert EditPlanClipStatus.PENDING == "pending"
|
||||
|
||||
def test_ready(self):
|
||||
"""ready 状态."""
|
||||
assert EditPlanClipStatus.READY == "ready"
|
||||
|
||||
def test_rendered(self):
|
||||
"""rendered 状态."""
|
||||
assert EditPlanClipStatus.RENDERED == "rendered"
|
||||
|
||||
def test_failed(self):
|
||||
"""failed 状态."""
|
||||
assert EditPlanClipStatus.FAILED == "failed"
|
||||
|
||||
def test_is_string(self):
|
||||
"""枚举值是字符串."""
|
||||
for status in EditPlanClipStatus:
|
||||
assert isinstance(status.value, str)
|
||||
assert len(status.value) > 0
|
||||
|
||||
def test_str_compatible(self):
|
||||
"""StrEnum 可与字符串比较."""
|
||||
assert EditPlanClipStatus.PENDING == "pending"
|
||||
assert EditPlanClipStatus.READY + "" == "ready"
|
||||
|
||||
|
||||
class TestEditPlanClipCreate:
|
||||
"""EditPlanClip.create 工厂方法测试."""
|
||||
|
||||
def test_create_minimal(self):
|
||||
"""最简创建."""
|
||||
clip = EditPlanClip.create(plan_id="plan1", clip_type="video", order=0)
|
||||
assert clip.plan_id == "plan1"
|
||||
assert clip.clip_type == "video"
|
||||
assert clip.order == 0
|
||||
assert clip.status == EditPlanClipStatus.PENDING
|
||||
assert clip.template_clip_config_id == ""
|
||||
assert clip.asset_id == ""
|
||||
assert clip.text_content == ""
|
||||
assert clip.start_time == 0.0
|
||||
assert clip.duration == 0.0
|
||||
assert clip.transition_effect == "cut"
|
||||
assert clip.config == {}
|
||||
assert isinstance(clip.id, str)
|
||||
assert len(clip.id) > 0
|
||||
|
||||
def test_create_with_all_fields(self):
|
||||
"""带全部字段创建."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="plan1",
|
||||
clip_type="video",
|
||||
order=2,
|
||||
template_clip_config_id="tpl1",
|
||||
asset_id="asset1",
|
||||
text_content=" 你好世界 ",
|
||||
start_time=10.5,
|
||||
duration=5.0,
|
||||
transition_effect="fade",
|
||||
config={"key": "value"},
|
||||
)
|
||||
assert clip.order == 2
|
||||
assert clip.template_clip_config_id == "tpl1"
|
||||
assert clip.asset_id == "asset1"
|
||||
assert clip.text_content == "你好世界" # strip了
|
||||
assert clip.start_time == 10.5
|
||||
assert clip.duration == 5.0
|
||||
assert clip.transition_effect == "fade"
|
||||
assert clip.config == {"key": "value"}
|
||||
|
||||
def test_create_strips_ids(self):
|
||||
"""plan_id 和 clip_type 会 strip."""
|
||||
clip = EditPlanClip.create(plan_id=" plan1 ", clip_type=" video ", order=0)
|
||||
assert clip.plan_id == "plan1"
|
||||
assert clip.clip_type == "video"
|
||||
|
||||
def test_create_empty_plan_id(self):
|
||||
"""空 plan_id 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="", clip_type="video", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "plan_id" in str(e)
|
||||
|
||||
def test_create_whitespace_plan_id(self):
|
||||
"""纯空白 plan_id 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id=" ", clip_type="video", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "plan_id" in str(e)
|
||||
|
||||
def test_create_empty_clip_type(self):
|
||||
"""空 clip_type 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type="", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "clip_type" in str(e)
|
||||
|
||||
def test_create_whitespace_clip_type(self):
|
||||
"""纯空白 clip_type 无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type=" ", order=0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "clip_type" in str(e)
|
||||
|
||||
def test_create_negative_start_time(self):
|
||||
"""start_time 为负无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type="v", order=0, start_time=-1.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "start_time" in str(e)
|
||||
|
||||
def test_create_negative_duration(self):
|
||||
"""duration 为负无效."""
|
||||
try:
|
||||
EditPlanClip.create(plan_id="p1", clip_type="v", order=0, duration=-1.0)
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "duration" in str(e)
|
||||
|
||||
def test_create_zero_duration_valid(self):
|
||||
"""duration 为 0 合法."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, duration=0.0)
|
||||
assert clip.duration == 0.0
|
||||
|
||||
def test_create_empty_transition_defaults_to_cut(self):
|
||||
"""空 transition_effect 默认 cut."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, transition_effect="")
|
||||
assert clip.transition_effect == "cut"
|
||||
|
||||
def test_create_whitespace_transition_defaults_to_cut(self):
|
||||
"""空白 transition_effect 默认 cut."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, transition_effect=" ")
|
||||
assert clip.transition_effect == "cut"
|
||||
|
||||
def test_create_config_none_defaults_empty_dict(self):
|
||||
"""config=None 默认为空 dict."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, config=None)
|
||||
assert clip.config == {}
|
||||
|
||||
def test_create_unique_id(self):
|
||||
"""不同 clip id 不同."""
|
||||
c1 = EditPlanClip.create("p1", "v", 0)
|
||||
c2 = EditPlanClip.create("p1", "v", 0)
|
||||
assert c1.id != c2.id
|
||||
|
||||
def test_create_has_timestamps(self):
|
||||
"""有创建和更新时间."""
|
||||
clip = EditPlanClip.create("p1", "v", 0)
|
||||
assert clip.created_at is not None
|
||||
assert clip.updated_at is not None
|
||||
|
||||
def test_create_negative_order_valid(self):
|
||||
"""order 可以为负(表示排序位置)."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=-1)
|
||||
assert clip.order == -1
|
||||
|
||||
|
||||
class TestEditPlanClipAssignAsset:
|
||||
"""素材分配测试."""
|
||||
|
||||
def _make_clip(self):
|
||||
return EditPlanClip.create(plan_id="p1", clip_type="video", order=0)
|
||||
|
||||
def test_assign_asset(self):
|
||||
"""正常分配素材."""
|
||||
clip = self._make_clip()
|
||||
old_updated = clip.updated_at
|
||||
clip.assign_asset("asset123")
|
||||
assert clip.asset_id == "asset123"
|
||||
assert clip.updated_at >= old_updated
|
||||
|
||||
def test_assign_asset_strips(self):
|
||||
"""asset_id 会 strip."""
|
||||
clip = self._make_clip()
|
||||
clip.assign_asset(" asset123 ")
|
||||
assert clip.asset_id == "asset123"
|
||||
|
||||
def test_assign_asset_empty(self):
|
||||
"""空 asset_id 无效."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.assign_asset("")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_assign_asset_whitespace(self):
|
||||
"""纯空白 asset_id 无效."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.assign_asset(" ")
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "asset_id" in str(e)
|
||||
|
||||
def test_has_asset_false_initially(self):
|
||||
"""初始无素材."""
|
||||
clip = self._make_clip()
|
||||
assert clip.has_asset is False
|
||||
|
||||
def test_has_asset_true_after_assign(self):
|
||||
"""分配后有素材."""
|
||||
clip = self._make_clip()
|
||||
clip.assign_asset("a1")
|
||||
assert clip.has_asset is True
|
||||
|
||||
|
||||
class TestEditPlanClipStatusFlow:
|
||||
"""状态流转测试."""
|
||||
|
||||
def _make_clip(self):
|
||||
return EditPlanClip.create(plan_id="p1", clip_type="video", order=0)
|
||||
|
||||
def test_initial_status_pending(self):
|
||||
"""初始状态 pending."""
|
||||
clip = self._make_clip()
|
||||
assert clip.status == EditPlanClipStatus.PENDING
|
||||
|
||||
def test_pending_to_ready(self):
|
||||
"""pending -> ready."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
assert clip.status == EditPlanClipStatus.READY
|
||||
|
||||
def test_ready_to_rendered(self):
|
||||
"""ready -> rendered."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_rendered()
|
||||
assert clip.status == EditPlanClipStatus.RENDERED
|
||||
|
||||
def test_ready_to_failed(self):
|
||||
"""ready -> failed."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_failed()
|
||||
assert clip.status == EditPlanClipStatus.FAILED
|
||||
|
||||
def test_cannot_ready_from_rendered(self):
|
||||
"""rendered 状态不能再 mark_ready."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_rendered()
|
||||
try:
|
||||
clip.mark_ready()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "pending" in str(e).lower()
|
||||
|
||||
def test_cannot_ready_from_failed(self):
|
||||
"""failed 状态不能 mark_ready."""
|
||||
clip = self._make_clip()
|
||||
clip.mark_ready()
|
||||
clip.mark_failed()
|
||||
try:
|
||||
clip.mark_ready()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "pending" in str(e).lower()
|
||||
|
||||
def test_cannot_render_from_pending(self):
|
||||
"""pending 不能直接 mark_rendered."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.mark_rendered()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "ready" in str(e).lower()
|
||||
|
||||
def test_cannot_failed_from_pending(self):
|
||||
"""pending 不能直接 mark_failed."""
|
||||
clip = self._make_clip()
|
||||
try:
|
||||
clip.mark_failed()
|
||||
assert False
|
||||
except ValueError as e:
|
||||
assert "ready" in str(e).lower()
|
||||
|
||||
def test_status_change_updates_timestamp(self):
|
||||
"""状态变更更新 updated_at."""
|
||||
clip = self._make_clip()
|
||||
old_updated = clip.updated_at
|
||||
clip.mark_ready()
|
||||
assert clip.updated_at >= old_updated
|
||||
|
||||
|
||||
class TestEditPlanClipProperties:
|
||||
"""属性计算测试."""
|
||||
|
||||
def test_end_time(self):
|
||||
"""end_time = start_time + duration."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="p1",
|
||||
clip_type="v",
|
||||
order=0,
|
||||
start_time=10.0,
|
||||
duration=5.5,
|
||||
)
|
||||
assert clip.end_time == 15.5
|
||||
|
||||
def test_end_time_zero_duration(self):
|
||||
"""零时长 end_time = start_time."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="p1",
|
||||
clip_type="v",
|
||||
order=0,
|
||||
start_time=10.0,
|
||||
duration=0.0,
|
||||
)
|
||||
assert clip.end_time == 10.0
|
||||
|
||||
def test_end_time_zero_start(self):
|
||||
"""零起点 end_time = duration."""
|
||||
clip = EditPlanClip.create(
|
||||
plan_id="p1",
|
||||
clip_type="v",
|
||||
order=0,
|
||||
start_time=0.0,
|
||||
duration=7.0,
|
||||
)
|
||||
assert clip.end_time == 7.0
|
||||
|
||||
def test_has_asset_empty_string(self):
|
||||
"""空字符串无素材."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, asset_id="")
|
||||
assert clip.has_asset is False
|
||||
|
||||
def test_has_asset_with_value(self):
|
||||
"""有值则有素材."""
|
||||
clip = EditPlanClip.create(plan_id="p1", clip_type="v", order=0, asset_id="a1")
|
||||
assert clip.has_asset is True
|
||||
|
||||
def test_config_independent_between_clips(self):
|
||||
"""不同 clip 的 config 独立."""
|
||||
c1 = EditPlanClip.create(plan_id="p1", clip_type="v", order=0)
|
||||
c2 = EditPlanClip.create(plan_id="p1", clip_type="v", order=1)
|
||||
c1.config["key"] = "value"
|
||||
assert "key" not in c2.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