2df7bc9dc8
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 1m41s
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 / Build Staging Web Image (push) Successful in 1m45s
CI/CD Pipeline / Validate - Type Check (mypy) (push) Successful in 1m54s
CI/CD Pipeline / Validate - Code Quality (push) Failing after 3m1s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 1m34s
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Build Staging API Image (push) Successful in 5m49s
CI/CD Pipeline / Frontend Lint (push) Successful in 6m0s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 4m27s
CI/CD Pipeline / Integration Tests (push) Successful in 2m20s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 1m11s
CI/CD Pipeline / Unit Tests (push) Failing after 5m52s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 30s
CI/CD Pipeline / Staging E2E Tests (push) Failing after 1m48s
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 5m10s
509 lines
18 KiB
Python
Executable File
509 lines
18 KiB
Python
Executable File
"""Job 领域层单元测试 - job.py"""
|
|
|
|
import pytest
|
|
|
|
from packages.domain.job import (
|
|
TERMINAL_STATUSES,
|
|
Job,
|
|
JobStatus,
|
|
JobType,
|
|
)
|
|
|
|
|
|
class TestJobType:
|
|
"""JobType 枚举测试"""
|
|
|
|
def test_all_types_have_values(self):
|
|
"""所有枚举成员都有字符串值"""
|
|
for jt in JobType:
|
|
assert isinstance(jt.value, str)
|
|
assert jt.value
|
|
|
|
def test_str_enum_behavior(self):
|
|
"""是 str 枚举"""
|
|
assert JobType.VIDEO_COMPOSE == "video_compose"
|
|
assert isinstance(JobType.VIDEO_COMPOSE, str)
|
|
|
|
def test_known_types_exist(self):
|
|
"""核心任务类型都存在"""
|
|
assert JobType.VIDEO_COMPOSE
|
|
assert JobType.RENDER_EDIT_PLAN
|
|
assert JobType.ASSET_INGEST
|
|
assert JobType.CLASSIFICATION
|
|
assert JobType.GENERATION
|
|
|
|
|
|
class TestJobStatus:
|
|
"""JobStatus 枚举测试"""
|
|
|
|
def test_all_statuses_have_values(self):
|
|
for js in JobStatus:
|
|
assert isinstance(js.value, str)
|
|
assert js.value
|
|
|
|
def test_str_enum_behavior(self):
|
|
assert JobStatus.PENDING == "pending"
|
|
assert isinstance(JobStatus.PENDING, str)
|
|
|
|
def test_terminal_statuses(self):
|
|
"""终态集合包含成功/失败/取消"""
|
|
assert JobStatus.SUCCESS in TERMINAL_STATUSES
|
|
assert JobStatus.FAILED in TERMINAL_STATUSES
|
|
assert JobStatus.CANCELLED in TERMINAL_STATUSES
|
|
|
|
def test_pending_not_terminal(self):
|
|
assert JobStatus.PENDING not in TERMINAL_STATUSES
|
|
|
|
def test_running_not_terminal(self):
|
|
assert JobStatus.RUNNING not in TERMINAL_STATUSES
|
|
|
|
|
|
class TestJobCreate:
|
|
"""Job.create 工厂方法测试"""
|
|
|
|
def test_create_basic(self):
|
|
"""基本创建"""
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
)
|
|
assert job.id
|
|
assert len(job.id) == 32 # uuid4 hex
|
|
assert job.project_id == "proj-1"
|
|
assert job.job_type == JobType.VIDEO_COMPOSE
|
|
assert job.status == JobStatus.PENDING
|
|
assert job.progress == 0.0
|
|
assert job.payload == {}
|
|
assert job.result == {}
|
|
assert job.retry_count == 0
|
|
assert job.max_retries == 3
|
|
assert job.created_at
|
|
assert job.updated_at
|
|
|
|
def test_create_with_string_job_type(self):
|
|
"""用字符串创建任务类型"""
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type="video_compose",
|
|
)
|
|
assert job.job_type == JobType.VIDEO_COMPOSE
|
|
|
|
def test_create_invalid_string_job_type_raises(self):
|
|
"""无效的任务类型字符串抛 ValueError"""
|
|
with pytest.raises(ValueError, match="不支持的任务类型"):
|
|
Job.create(project_id="proj-1", job_type="invalid_type")
|
|
|
|
def test_create_empty_project_id_raises(self):
|
|
"""空 project_id 抛 ValueError"""
|
|
with pytest.raises(ValueError, match="project_id 不能为空"):
|
|
Job.create(project_id=" ", job_type=JobType.VIDEO_COMPOSE)
|
|
|
|
def test_create_with_payload(self):
|
|
"""带 payload 创建"""
|
|
payload = {"video_id": "v1", "quality": "1080p"}
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
payload=payload,
|
|
)
|
|
assert job.payload == payload
|
|
|
|
def test_create_with_source_id(self):
|
|
"""带 source_id 创建"""
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
source_id="plan-123",
|
|
)
|
|
assert job.source_id == "plan-123"
|
|
|
|
def test_create_with_created_by(self):
|
|
"""带创建人"""
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
created_by_user_id="user-1",
|
|
)
|
|
assert job.created_by_user_id == "user-1"
|
|
|
|
def test_create_with_custom_max_retries(self):
|
|
"""自定义最大重试次数"""
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
max_retries=5,
|
|
)
|
|
assert job.max_retries == 5
|
|
|
|
def test_create_project_id_stripped(self):
|
|
"""project_id 会被 strip"""
|
|
job = Job.create(
|
|
project_id=" proj-1 ",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
)
|
|
assert job.project_id == "proj-1"
|
|
|
|
def test_create_source_id_stripped(self):
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
source_id=" src-1 ",
|
|
)
|
|
assert job.source_id == "src-1"
|
|
|
|
def test_create_created_by_stripped(self):
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
created_by_user_id=" user-1 ",
|
|
)
|
|
assert job.created_by_user_id == "user-1"
|
|
|
|
def test_create_none_payload_defaults_to_empty_dict(self):
|
|
"""payload=None 时默认为空 dict"""
|
|
job = Job.create(
|
|
project_id="proj-1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
payload=None,
|
|
)
|
|
assert job.payload == {}
|
|
|
|
|
|
class TestJobIsTerminal:
|
|
"""is_terminal 属性测试"""
|
|
|
|
def test_pending_not_terminal(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
assert job.is_terminal is False
|
|
|
|
def test_running_not_terminal(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
assert job.is_terminal is False
|
|
|
|
def test_success_is_terminal(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
job.transition_to(JobStatus.SUCCESS)
|
|
assert job.is_terminal is True
|
|
|
|
def test_failed_is_terminal(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
job.transition_to(JobStatus.FAILED)
|
|
assert job.is_terminal is True
|
|
|
|
def test_cancelled_is_terminal(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.CANCELLED)
|
|
assert job.is_terminal is True
|
|
|
|
|
|
class TestJobTransitions:
|
|
"""状态转换测试"""
|
|
|
|
def test_pending_to_running(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
assert job.status == JobStatus.RUNNING
|
|
assert job.started_at is not None
|
|
|
|
def test_pending_to_success(self):
|
|
"""pending 可以直接到 success(快速成功)"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.SUCCESS)
|
|
assert job.status == JobStatus.SUCCESS
|
|
assert job.completed_at is not None
|
|
|
|
def test_pending_to_cancelled(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.CANCELLED)
|
|
assert job.status == JobStatus.CANCELLED
|
|
|
|
def test_running_to_success(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
job.transition_to(JobStatus.SUCCESS)
|
|
assert job.status == JobStatus.SUCCESS
|
|
assert job.completed_at is not None
|
|
|
|
def test_running_to_failed(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
job.transition_to(JobStatus.FAILED)
|
|
assert job.status == JobStatus.FAILED
|
|
assert job.completed_at is not None
|
|
|
|
def test_running_to_cancelled(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
job.transition_to(JobStatus.CANCELLED)
|
|
assert job.status == JobStatus.CANCELLED
|
|
|
|
def test_failed_to_pending_retry(self):
|
|
"""失败后可以回到 pending(重试)"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
job.transition_to(JobStatus.FAILED)
|
|
job.transition_to(JobStatus.PENDING)
|
|
assert job.status == JobStatus.PENDING
|
|
|
|
def test_invalid_transition_raises(self):
|
|
"""非法状态转换抛 ValueError"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
# pending 不能直接到 failed
|
|
with pytest.raises(ValueError, match="非法状态转换"):
|
|
job.transition_to(JobStatus.FAILED)
|
|
|
|
def test_success_to_pending_raises(self):
|
|
"""成功后不能回到 pending"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
job.transition_to(JobStatus.SUCCESS)
|
|
with pytest.raises(ValueError):
|
|
job.transition_to(JobStatus.PENDING)
|
|
|
|
def test_transition_with_string_status(self):
|
|
"""用字符串做状态转换"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to("running")
|
|
assert job.status == JobStatus.RUNNING
|
|
|
|
def test_transition_invalid_string_raises(self):
|
|
"""无效状态字符串抛 ValueError"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
with pytest.raises(ValueError, match="无效状态"):
|
|
job.transition_to("invalid_status")
|
|
|
|
def test_transition_updates_updated_at(self):
|
|
"""状态转换更新 updated_at"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
old_updated = job.updated_at
|
|
import time
|
|
|
|
time.sleep(0.001)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
assert job.updated_at >= old_updated
|
|
|
|
def test_started_at_only_set_once(self):
|
|
"""started_at 只在第一次 RUNNING 时设置"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.transition_to(JobStatus.RUNNING)
|
|
first_started = job.started_at
|
|
job.transition_to(JobStatus.SUCCESS)
|
|
# 回到 pending 再 running(模拟重试场景,但started_at是None时才设置)
|
|
# 注意:正常重试是通过 prepare_retry 重置的
|
|
assert first_started is not None
|
|
|
|
|
|
class TestJobMarkMethods:
|
|
"""便捷标记方法测试"""
|
|
|
|
def test_mark_running(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_running("合成中")
|
|
assert job.status == JobStatus.RUNNING
|
|
assert job.current_stage == "合成中"
|
|
|
|
def test_mark_running_no_stage(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_running()
|
|
assert job.status == JobStatus.RUNNING
|
|
assert job.current_stage == ""
|
|
|
|
def test_mark_success(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_running()
|
|
job.mark_success({"output_url": "http://..."})
|
|
assert job.status == JobStatus.SUCCESS
|
|
assert job.progress == 100.0
|
|
assert job.current_stage == "完成"
|
|
assert job.result == {"output_url": "http://..."}
|
|
|
|
def test_mark_success_no_result(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_running()
|
|
job.mark_success()
|
|
assert job.status == JobStatus.SUCCESS
|
|
assert job.result == {}
|
|
|
|
def test_mark_failed(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_running()
|
|
job.mark_failed("网络超时")
|
|
assert job.status == JobStatus.FAILED
|
|
assert job.error_message == "网络超时"
|
|
assert job.current_stage == "失败"
|
|
|
|
def test_mark_cancelled(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_cancelled()
|
|
assert job.status == JobStatus.CANCELLED
|
|
assert job.current_stage == "已取消"
|
|
|
|
|
|
class TestJobProgress:
|
|
"""进度更新测试"""
|
|
|
|
def test_update_progress(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.update_progress(50.0, "渲染中")
|
|
assert job.progress == 50.0
|
|
assert job.current_stage == "渲染中"
|
|
|
|
def test_update_progress_zero(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.update_progress(0.0)
|
|
assert job.progress == 0.0
|
|
|
|
def test_update_progress_100(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.update_progress(100.0)
|
|
assert job.progress == 100.0
|
|
|
|
def test_update_progress_negative_raises(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
with pytest.raises(ValueError, match="进度必须在 0~100 之间"):
|
|
job.update_progress(-1.0)
|
|
|
|
def test_update_progress_over_100_raises(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
with pytest.raises(ValueError, match="进度必须在 0~100 之间"):
|
|
job.update_progress(101.0)
|
|
|
|
def test_update_progress_without_stage(self):
|
|
"""不传 stage 时不修改 current_stage"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.current_stage = "初始阶段"
|
|
job.update_progress(30.0)
|
|
assert job.progress == 30.0
|
|
assert job.current_stage == "初始阶段"
|
|
|
|
def test_update_progress_updates_updated_at(self):
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
old_updated = job.updated_at
|
|
import time
|
|
|
|
time.sleep(0.001)
|
|
job.update_progress(50.0)
|
|
assert job.updated_at >= old_updated
|
|
|
|
|
|
class TestJobRetry:
|
|
"""重试逻辑测试"""
|
|
|
|
def test_is_retryable_failed_within_limit(self):
|
|
"""失败且未超过重试上限时可重试"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE, max_retries=3)
|
|
job.mark_running()
|
|
job.mark_failed("错误")
|
|
assert job.is_retryable is True
|
|
|
|
def test_is_retryable_failed_at_limit(self):
|
|
"""达到重试上限时不可重试"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE, max_retries=1)
|
|
job.mark_running()
|
|
job.mark_failed("错误")
|
|
job.retry_count = 1
|
|
assert job.is_retryable is False
|
|
|
|
def test_is_retryable_pending_false(self):
|
|
"""pending 状态不可重试"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
assert job.is_retryable is False
|
|
|
|
def test_is_retryable_success_false(self):
|
|
"""成功状态不可重试"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_running()
|
|
job.mark_success()
|
|
assert job.is_retryable is False
|
|
|
|
def test_prepare_retry(self):
|
|
"""准备重试"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE, max_retries=3)
|
|
job.mark_running()
|
|
job.mark_failed("网络错误")
|
|
job.celery_task_id = "task-123"
|
|
|
|
job.prepare_retry()
|
|
|
|
assert job.status == JobStatus.PENDING
|
|
assert job.retry_count == 1
|
|
assert job.progress == 0.0
|
|
assert "第 1 次重试" in job.current_stage
|
|
assert job.error_message == ""
|
|
assert job.started_at is None
|
|
assert job.completed_at is None
|
|
assert job.celery_task_id == ""
|
|
|
|
def test_prepare_retry_not_retryable_raises(self):
|
|
"""不可重试时抛 ValueError"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE, max_retries=0)
|
|
job.mark_running()
|
|
job.mark_failed("错误")
|
|
with pytest.raises(ValueError, match="任务不可重试"):
|
|
job.prepare_retry()
|
|
|
|
def test_prepare_retry_increments_correctly(self):
|
|
"""多次重试计数正确"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE, max_retries=3)
|
|
job.mark_running()
|
|
job.mark_failed("错误1")
|
|
job.prepare_retry()
|
|
assert job.retry_count == 1
|
|
|
|
job.mark_running()
|
|
job.mark_failed("错误2")
|
|
job.prepare_retry()
|
|
assert job.retry_count == 2
|
|
|
|
|
|
class TestJobToDict:
|
|
"""to_dict 序列化测试"""
|
|
|
|
def test_to_dict_contains_all_fields(self):
|
|
job = Job.create(
|
|
project_id="p1",
|
|
job_type=JobType.VIDEO_COMPOSE,
|
|
payload={"key": "value"},
|
|
source_id="src-1",
|
|
created_by_user_id="user-1",
|
|
)
|
|
d = job.to_dict()
|
|
assert d["id"] == job.id
|
|
assert d["project_id"] == "p1"
|
|
assert d["job_type"] == "video_compose"
|
|
assert d["status"] == "pending"
|
|
assert d["progress"] == 0.0
|
|
assert d["payload"] == {"key": "value"}
|
|
assert d["source_id"] == "src-1"
|
|
assert d["created_by_user_id"] == "user-1"
|
|
assert d["is_retryable"] is False
|
|
|
|
def test_to_dict_datetime_fields_are_strings(self):
|
|
"""时间字段序列化为 ISO 字符串"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
d = job.to_dict()
|
|
assert isinstance(d["created_at"], str)
|
|
assert isinstance(d["updated_at"], str)
|
|
|
|
def test_to_dict_none_datetime_fields(self):
|
|
"""未设置的时间字段为 None"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
d = job.to_dict()
|
|
assert d["started_at"] is None
|
|
assert d["completed_at"] is None
|
|
|
|
def test_to_dict_after_success(self):
|
|
"""成功后 to_dict 状态正确"""
|
|
job = Job.create(project_id="p1", job_type=JobType.VIDEO_COMPOSE)
|
|
job.mark_running()
|
|
job.mark_success({"url": "http://..."})
|
|
d = job.to_dict()
|
|
assert d["status"] == "success"
|
|
assert d["progress"] == 100.0
|
|
assert d["result"] == {"url": "http://..."}
|
|
assert d["started_at"] is not None
|
|
assert d["completed_at"] is not None
|