Files
xiaoxia-saas/tests/unit/test_scripts_routes.py
T
xiaoxia-agent 83616ae4a3
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 49s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 1m11s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m42s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 3m44s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 4m16s
CI/CD Pipeline / Validate - Style (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been cancelled
PR Automation / Auto Approve on CI Green (pull_request) Has been cancelled
feat(#1894): 清理文案库多余字段 + 废弃标题库 API + 清理旧定价档位
P0: 删除 scripts 表 title_text/title_category/title_config 三列
- migration 078_drop_script_title_fields 批量删列
- schemas/script.py: ScriptResponse/Create/Update 精简到 title/content/segments/tags
- services/script_service.py: 移除 get_title_config_for_script 和三字段参数
- routes/scripts.py: _to_response 不再映射三字段,create/update 不传三字段
- models.py: ScriptModel 移除三列;保留 TemplateModel/GenerationTaskModel/AiAvatarRenderJob 的 title_config(渲染层配置)

P1: 废弃 /api/v1/titles/*
- 所有子路径统一返回 HTTP 410 Gone + Deprecation/Sunset header
- 前端应直接调 GET /api/v1/scripts 取文案列表,用 title 作为标题

P1: 清理旧定价档位
- 新会员体系统一为 free/monthly/quarterly/yearly(#1951/#1955)
- schemas/subscription.py: MembershipType 替代旧 PlanType,BillingCycle 新增 QUARTERLY
- routes/subscription.py: _PLAN_NAMES 新映射;payment-callback 接受 quarterly(90天);旧档位 plan 值拒绝
- routes/_helpers.py: get_user_plan() 归一化 standard/basic→monthly、pro/premium/enterprise→quarterly
- packages/domain/quota.py: QUOTA_TIERS 改为 free/monthly/quarterly/yearly;保留 basic/standard/premium/pro/enterprise 别名兼容历史数据
- 单元测试 15527 passed
2026-09-16 17:52:38 +08:00

256 lines
8.3 KiB
Python

"""Scripts routes 单元测试 — Issue #1795 + #1894 清理.
CI 增量映射: scripts.py → test_scripts.py
本文件同时覆盖 routes/scripts.py 和 schemas/script.py 的增量覆盖率。
#1894: 删除 title_text/title_category/title_config 三字段,仅保留 title/content/segments/tags。
"""
from __future__ import annotations
from datetime import UTC, datetime, timezone
from unittest.mock import MagicMock, patch
import pytest
from app.schemas.script import (
CreateScriptRequest,
ScriptListResponse,
ScriptResponse,
ScriptSegment,
UpdateScriptRequest,
)
class TestScriptSegment:
def test_segment_with_duration(self):
s = ScriptSegment(text="测试", duration=5.0)
assert s.text == "测试"
assert s.duration == 5.0
def test_segment_null_duration(self):
s = ScriptSegment(text="测试", duration=None)
assert s.duration is None
def test_segment_default_duration(self):
s = ScriptSegment(text="测试")
assert s.duration is None
class TestCreateScriptRequest:
def test_minimal(self):
r = CreateScriptRequest(title="标题")
assert r.title == "标题"
assert r.content == ""
assert r.segments == []
assert r.tags == []
def test_full(self):
r = CreateScriptRequest(
title="标题",
content="正文",
segments=[ScriptSegment(text="段1", duration=3.0)],
tags=["口播"],
)
assert len(r.segments) == 1
assert r.tags == ["口播"]
def test_title_required(self):
with pytest.raises(ValueError):
CreateScriptRequest(title="")
def test_title_max_length(self):
with pytest.raises(ValueError):
CreateScriptRequest(title="x" * 256)
class TestUpdateScriptRequest:
def test_all_none_default(self):
r = UpdateScriptRequest()
assert r.title is None
assert r.content is None
assert r.segments is None
assert r.tags is None
def test_partial_update(self):
r = UpdateScriptRequest(title="新标题")
assert r.title == "新标题"
assert r.content is None
def test_partial_update_content_only(self):
r = UpdateScriptRequest(content="新内容")
assert r.title is None
assert r.content == "新内容"
class TestScriptResponse:
def test_response_construction(self):
now = datetime(2026, 9, 8, 12, 0, 0, tzinfo=UTC)
r = ScriptResponse(
id="s1",
user_id="u1",
title="标题",
content="内容",
segments=[ScriptSegment(text="段1")],
tags=["t1"],
created_at=now,
updated_at=now,
)
assert r.id == "s1"
assert len(r.segments) == 1
assert r.title == "标题"
assert r.tags == ["t1"]
def test_response_defaults(self):
now = datetime(2026, 9, 8, 12, 0, 0, tzinfo=UTC)
r = ScriptResponse(
id="s1", user_id="u1", title="标题", content="",
segments=[], tags=[], created_at=now, updated_at=now,
)
assert r.segments == []
assert r.tags == []
class TestScriptListResponse:
def test_empty_list(self):
r = ScriptListResponse(items=[], total=0)
assert r.total == 0
assert r.items == []
def test_with_items(self):
now = datetime(2026, 9, 8, 12, 0, 0, tzinfo=UTC)
item = ScriptResponse(
id="s1",
user_id="u1",
title="标题",
content="内容",
segments=[],
tags=[],
created_at=now,
updated_at=now,
)
r = ScriptListResponse(items=[item], total=1)
assert r.total == 1
assert len(r.items) == 1
class TestRouteHandlers:
def _make_auth_user(self, user_id="u1"):
user = MagicMock()
user.id = user_id
auth = MagicMock()
auth.user = user
return auth
def _make_mock_script(self, **overrides):
m = MagicMock()
m.id = overrides.get("id", "s1")
m.user_id = overrides.get("user_id", "u1")
m.title = overrides.get("title", "测试")
m.content = overrides.get("content", "内容")
m.segments = overrides.get("segments", [{"text": "段1", "duration": None}])
m.tags = overrides.get("tags", [])
m.created_at = overrides.get("created_at", datetime(2026, 9, 8, tzinfo=UTC))
m.updated_at = overrides.get("updated_at", datetime(2026, 9, 8, tzinfo=UTC))
return m
def test_create_route_calls_service(self):
from app.api.routes.scripts import create_script
svc = MagicMock()
svc.create_script.return_value = self._make_mock_script(content="内容")
req = CreateScriptRequest(title="测试", content="内容")
auth = self._make_auth_user()
result = create_script(req, authenticated_user=auth, svc=svc)
assert result.id == "s1"
svc.create_script.assert_called_once()
call_kwargs = svc.create_script.call_args.kwargs
assert "title_text" not in call_kwargs
assert "title_category" not in call_kwargs
assert "title_config" not in call_kwargs
def test_list_route_returns_paginated(self):
from app.api.routes.scripts import list_scripts
svc = MagicMock()
svc.list_scripts.return_value = ([self._make_mock_script()], 1)
auth = self._make_auth_user()
result = list_scripts(skip=0, limit=50, tag=None, authenticated_user=auth, svc=svc)
assert result.total == 1
assert len(result.items) == 1
def test_get_route_found(self):
from app.api.routes.scripts import get_script
svc = MagicMock()
svc.get_script.return_value = self._make_mock_script()
auth = self._make_auth_user()
result = get_script("s1", authenticated_user=auth, svc=svc)
assert result.id == "s1"
def test_get_route_not_found(self):
from app.api.routes.scripts import get_script
from app.services.script_service import ScriptNotFoundError
from fastapi import HTTPException
svc = MagicMock()
svc.get_script.side_effect = ScriptNotFoundError("not found")
auth = self._make_auth_user()
with pytest.raises(HTTPException) as exc_info:
get_script("nonexistent", authenticated_user=auth, svc=svc)
assert exc_info.value.status_code == 404
def test_update_route_success(self):
from app.api.routes.scripts import update_script
svc = MagicMock()
svc.update_script.return_value = self._make_mock_script(title="新标题")
req = UpdateScriptRequest(title="新标题")
auth = self._make_auth_user()
result = update_script("s1", req, authenticated_user=auth, svc=svc)
assert result.title == "新标题"
call_kwargs = svc.update_script.call_args.kwargs
assert "title_text" not in call_kwargs
assert "title_category" not in call_kwargs
assert "title_config" not in call_kwargs
def test_update_route_not_found(self):
from app.api.routes.scripts import update_script
from app.services.script_service import ScriptNotFoundError
from fastapi import HTTPException
svc = MagicMock()
svc.update_script.side_effect = ScriptNotFoundError("not found")
auth = self._make_auth_user()
req = UpdateScriptRequest(title="x")
with pytest.raises(HTTPException) as exc_info:
update_script("bad", req, authenticated_user=auth, svc=svc)
assert exc_info.value.status_code == 404
def test_delete_route_success(self):
from app.api.routes.scripts import delete_script
svc = MagicMock()
svc.delete_script.return_value = True
auth = self._make_auth_user()
result = delete_script("s1", authenticated_user=auth, svc=svc)
assert result is None
def test_delete_route_not_found(self):
from app.api.routes.scripts import delete_script
from fastapi import HTTPException
svc = MagicMock()
svc.delete_script.return_value = False
auth = self._make_auth_user()
with pytest.raises(HTTPException) as exc_info:
delete_script("bad", authenticated_user=auth, svc=svc)
assert exc_info.value.status_code == 404