Files
xiaoxia-saas/tests/unit/test_generation_worker_fixes.py
T
xiaoxia d494f368d0
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 42s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 51s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 57s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m23s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m38s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 1m53s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m52s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 3m20s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m28s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 3m1s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
AI Code Review / AI Code Review (pull_request) Failing after 3m57s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 2m45s
CI/CD Pipeline / CI Gate (pull_request) Successful in 6s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 50s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 1m8s
test: generation.py worker侧修复的单测(时长约束+segment加载)
2026-08-04 00:09:53 +08:00

210 lines
8.1 KiB
Python

"""Tests for generation.py worker-side fixes: segment durations + preview resolution."""
from __future__ import annotations
import os
import sys
from pathlib import Path
from unittest.mock import MagicMock, patch
# Add worker app to path
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "apps" / "worker"))
os.environ.setdefault("JWT_SECRET_KEY", "unit-test-secret")
os.environ.setdefault("DATABASE_URL", "sqlite:///test.db")
class TestLoadTemplateSegmentDurations:
"""_load_template_segment_durations 单元测试 (covers lines 198-226)."""
def test_empty_template_id(self):
"""空 template_id 直接返回空列表。"""
from worker_app.tasks.generation import _load_template_segment_durations
result = _load_template_segment_durations("")
assert result == []
def test_loads_durations_ordered(self):
"""按 segment_order 排序返回 duration_max 列表。"""
from worker_app.tasks.generation import _load_template_segment_durations
mock_seg1 = MagicMock(duration_max=5.0)
mock_seg2 = MagicMock(duration_max=8.0)
mock_seg3 = MagicMock(duration_max=3.0)
mock_query = MagicMock()
mock_query.filter.return_value.order_by.return_value.all.return_value = [
mock_seg1,
mock_seg2,
mock_seg3,
]
mock_session = MagicMock()
mock_session.query.return_value = mock_query
# Patch at the source module since it's imported inside the function
with patch("worker_app.db.SessionLocal", return_value=mock_session):
result = _load_template_segment_durations("tpl_123")
assert result == [5.0, 8.0, 3.0]
def test_filters_zero_and_negative(self):
"""duration_max <= 0 的 segment 被过滤。"""
from worker_app.tasks.generation import _load_template_segment_durations
mock_seg_valid = MagicMock(duration_max=5.0)
mock_seg_zero = MagicMock(duration_max=0.0)
mock_seg_none = MagicMock(duration_max=None)
mock_query = MagicMock()
mock_query.filter.return_value.order_by.return_value.all.return_value = [
mock_seg_valid,
mock_seg_zero,
mock_seg_none,
]
mock_session = MagicMock()
mock_session.query.return_value = mock_query
with patch("worker_app.db.SessionLocal", return_value=mock_session):
result = _load_template_segment_durations("tpl_456")
assert result == [5.0]
def test_db_error_returns_empty(self):
"""数据库异常返回空列表,不抛出。"""
from worker_app.tasks.generation import _load_template_segment_durations
with patch("worker_app.db.SessionLocal", side_effect=Exception("DB down")):
result = _load_template_segment_durations("tpl_789")
assert result == []
def test_empty_segments_returns_empty(self):
"""没有 segment 时返回空列表。"""
from worker_app.tasks.generation import _load_template_segment_durations
mock_query = MagicMock()
mock_query.filter.return_value.order_by.return_value.all.return_value = []
mock_session = MagicMock()
mock_session.query.return_value = mock_query
with patch("worker_app.db.SessionLocal", return_value=mock_session):
result = _load_template_segment_durations("tpl_empty")
assert result == []
class TestDurationCappingInBuildPlan:
"""_build_plan_and_clips_from_task 时长约束测试 (covers lines 322-333)."""
def _make_temp_video(self, tmpdir: Path, name: str = "v.mp4") -> Path:
p = tmpdir / name
p.write_bytes(b"\x00" * 100)
return p
def test_clips_capped_by_segment_max(self):
"""clip 时长超过 segment duration_max 时截断。"""
import tempfile
from worker_app.tasks.generation import _build_plan_and_clips_from_task
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
paths = [self._make_temp_video(tmpdir, f"v{i}.mp4") for i in range(3)]
with patch("worker_app.tasks.generation.probe_duration", return_value=30.0):
with patch(
"worker_app.tasks.generation._load_template_segment_durations",
return_value=[5.0, 4.0, 3.0],
):
with patch(
"worker_app.tasks.generation._load_template_clip_configs",
return_value=[],
):
_, clips, _ = _build_plan_and_clips_from_task(
task_id="test_cap",
downloaded_paths=paths,
mode="one_take",
template_id="tpl_test",
)
assert clips[0].duration == 5.0
assert clips[1].duration == 4.0
assert clips[2].duration == 3.0
def test_clips_not_capped_when_under_max(self):
"""clip 时长小于 segment duration_max 时不截断。"""
import tempfile
from worker_app.tasks.generation import _build_plan_and_clips_from_task
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
paths = [self._make_temp_video(tmpdir)]
with patch("worker_app.tasks.generation.probe_duration", return_value=3.0):
with patch(
"worker_app.tasks.generation._load_template_segment_durations",
return_value=[5.0],
):
with patch(
"worker_app.tasks.generation._load_template_clip_configs",
return_value=[],
):
_, clips, _ = _build_plan_and_clips_from_task(
task_id="test_no_cap",
downloaded_paths=paths,
mode="one_take",
template_id="tpl_test",
)
assert clips[0].duration == 3.0
def test_no_capping_without_template(self):
"""无 template_id 时不截断。"""
import tempfile
from worker_app.tasks.generation import _build_plan_and_clips_from_task
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
paths = [self._make_temp_video(tmpdir)]
with patch("worker_app.tasks.generation.probe_duration", return_value=30.0):
_, clips, _ = _build_plan_and_clips_from_task(
task_id="test_no_tpl",
downloaded_paths=paths,
mode="one_take",
template_id="",
)
assert clips[0].duration == 30.0
def test_partial_segments_only_caps_matching(self):
"""segment 数量少于 clip 时,只截断有对应 segment 的 clip。"""
import tempfile
from worker_app.tasks.generation import _build_plan_and_clips_from_task
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
paths = [self._make_temp_video(tmpdir, f"v{i}.mp4") for i in range(3)]
with patch("worker_app.tasks.generation.probe_duration", return_value=20.0):
with patch(
"worker_app.tasks.generation._load_template_segment_durations",
return_value=[5.0], # only 1 segment for 3 clips
):
with patch(
"worker_app.tasks.generation._load_template_clip_configs",
return_value=[],
):
_, clips, _ = _build_plan_and_clips_from_task(
task_id="test_partial",
downloaded_paths=paths,
mode="one_take",
template_id="tpl_test",
)
assert clips[0].duration == 5.0 # capped
assert clips[1].duration == 20.0 # not capped (no matching segment)
assert clips[2].duration == 20.0 # not capped