fix(test): 修复 MagicMock 属性自动创建导致的断言失败
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 44s
AI Code Review / AI Code Review (pull_request) Successful in 1m51s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m51s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m54s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 1m56s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 2m8s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m39s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m33s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 3m53s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 4m19s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 7m8s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 4m1s
CI/CD Pipeline / CI Gate (pull_request) Failing after 7s
CI/CD Pipeline / Production Browser E2E (pull_request) Failing after 711h41m11s
CI/CD Pipeline / Build Production Worker Image (pull_request) Failing after 711h41m13s
CI/CD Pipeline / Canary Release to Production (pull_request) Failing after 711h41m11s
CI/CD Pipeline / Build Production Web Image (pull_request) Failing after 711h41m13s
CI/CD Pipeline / PR Build Web Image (pull_request) Failing after 711h47m34s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 711h47m36s
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 711h47m36s
CI/CD Pipeline / Staging E2E Tests (pull_request) Failing after 711h48m18s
CI/CD Pipeline / ACR Image Cleanup (pull_request) Failing after 711h48m18s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 711h48m20s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 711h48m22s
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 711h48m22s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 711h48m22s
CI/CD Pipeline / Deploy Production (pull_request) Failing after 712h14m50s
CI/CD Pipeline / Build Production API Image (pull_request) Failing after 712h14m52s
CI/CD Pipeline / Staging API Integration Tests (pull_request) Failing after 712h21m57s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 44s
AI Code Review / AI Code Review (pull_request) Successful in 1m51s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m51s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m54s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 1m56s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 2m8s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m39s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m33s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 3m53s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 4m19s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 7m8s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 4m1s
CI/CD Pipeline / CI Gate (pull_request) Failing after 7s
CI/CD Pipeline / Production Browser E2E (pull_request) Failing after 711h41m11s
CI/CD Pipeline / Build Production Worker Image (pull_request) Failing after 711h41m13s
CI/CD Pipeline / Canary Release to Production (pull_request) Failing after 711h41m11s
CI/CD Pipeline / Build Production Web Image (pull_request) Failing after 711h41m13s
CI/CD Pipeline / PR Build Web Image (pull_request) Failing after 711h47m34s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 711h47m36s
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 711h47m36s
CI/CD Pipeline / Staging E2E Tests (pull_request) Failing after 711h48m18s
CI/CD Pipeline / ACR Image Cleanup (pull_request) Failing after 711h48m18s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 711h48m20s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 711h48m22s
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 711h48m22s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 711h48m22s
CI/CD Pipeline / Deploy Production (pull_request) Failing after 712h14m50s
CI/CD Pipeline / Build Production API Image (pull_request) Failing after 712h14m52s
CI/CD Pipeline / Staging API Integration Tests (pull_request) Failing after 712h21m57s
使用 FakeGenTask 替代 MagicMock,通过追踪 _assigned 字典 准确判断 cover_url 是否被显式设置。
This commit is contained in:
@@ -6,9 +6,32 @@ from cover_candidates to gen_task.cover_url.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Add worker app to sys.path
|
||||
_WORKER_ROOT = Path(__file__).resolve().parents[2] / "apps" / "worker"
|
||||
if str(_WORKER_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(_WORKER_ROOT))
|
||||
|
||||
|
||||
class FakeGenTask:
|
||||
"""Simple stand-in for GenerationTask that tracks attribute assignment."""
|
||||
|
||||
def __init__(self):
|
||||
self.id = "task-1"
|
||||
self.status = MagicMock()
|
||||
self.status.value = "running"
|
||||
self._assigned = {}
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if not name.startswith("_"):
|
||||
self._assigned[name] = value
|
||||
super().__setattr__(name, value)
|
||||
|
||||
|
||||
def _make_plan():
|
||||
plan = MagicMock()
|
||||
@@ -19,19 +42,11 @@ def _make_plan():
|
||||
return plan
|
||||
|
||||
|
||||
def _make_gen_task():
|
||||
gen_task = MagicMock()
|
||||
gen_task.id = "task-1"
|
||||
gen_task.status = MagicMock()
|
||||
gen_task.status.value = "running"
|
||||
return gen_task
|
||||
|
||||
|
||||
def _call_finalize(cover_candidates=None, gen_task=None, plan=None):
|
||||
from worker_app.tasks.edit_plan_generation import _finalize_render_success
|
||||
|
||||
plan = plan or _make_plan()
|
||||
gen_task = gen_task or _make_gen_task()
|
||||
gen_task = gen_task or FakeGenTask()
|
||||
|
||||
plan_repo = MagicMock()
|
||||
clip_repo = MagicMock()
|
||||
@@ -86,25 +101,26 @@ class TestFinalizeCoverUrl:
|
||||
def test_cover_url_not_set_when_empty_list(self):
|
||||
"""Empty cover_candidates should not set cover_url"""
|
||||
_, gen_task, _ = _call_finalize(cover_candidates=[])
|
||||
assert not getattr(gen_task, "cover_url", None)
|
||||
assert "cover_url" not in gen_task._assigned
|
||||
|
||||
def test_cover_url_not_set_when_none(self):
|
||||
"""None cover_candidates should not set cover_url"""
|
||||
_, gen_task, _ = _call_finalize(cover_candidates=None)
|
||||
assert not getattr(gen_task, "cover_url", None)
|
||||
assert "cover_url" not in gen_task._assigned
|
||||
|
||||
def test_cover_url_not_set_when_url_empty(self):
|
||||
"""Empty URL strings in candidates should not set cover_url"""
|
||||
candidates = [{"image_url": "", "url": ""}]
|
||||
_, gen_task, _ = _call_finalize(cover_candidates=candidates)
|
||||
assert not getattr(gen_task, "cover_url", None)
|
||||
assert "cover_url" not in gen_task._assigned
|
||||
|
||||
def test_no_generation_task_no_crash(self):
|
||||
"""Should not crash when gen_task is None"""
|
||||
candidates = [{"image_url": "https://oss.example.com/cover.jpg"}]
|
||||
gen_task_repo = MagicMock()
|
||||
gen_task_repo.get.return_value = None
|
||||
_, _, _ = _call_finalize(cover_candidates=candidates)
|
||||
result, _, _ = _call_finalize(cover_candidates=candidates)
|
||||
assert result["status"] == "completed"
|
||||
|
||||
def test_image_url_priority_over_url(self):
|
||||
"""image_url should take priority over url key"""
|
||||
|
||||
Reference in New Issue
Block a user