56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
"""视频生成 积分扣点单元测试 (#1895 P2 step 2.4)"""
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
import packages.middleware.points_gate as _pg_module
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _enable(monkeypatch):
|
|
monkeypatch.setattr(_pg_module, "_points_gate_enabled", lambda: True)
|
|
yield
|
|
|
|
|
|
class TestGenerationTasksPoints:
|
|
def test_ai_video_base_cost(self):
|
|
from packages.domain.points_rules import calculate_points_cost
|
|
assert calculate_points_cost("ai_video", is_member=False) == 4
|
|
assert calculate_points_cost("ai_video", is_member=True, member_type="monthly") == 2
|
|
|
|
def test_ai_video_quantity_scales(self):
|
|
from packages.domain.points_rules import calculate_points_cost
|
|
c1 = calculate_points_cost("ai_video", is_member=False, quantity=1)
|
|
c3 = calculate_points_cost("ai_video", is_member=False, quantity=3)
|
|
assert c3 > c1
|
|
|
|
def test_insufficient_raises_402(self):
|
|
from fastapi import HTTPException
|
|
from app.api.routes.generation_tasks import create_generation_task
|
|
from app.schemas.generation_task import CreateGenerationTaskRequest
|
|
|
|
db = MagicMock()
|
|
cu = MagicMock()
|
|
cu.user.id = "u1"
|
|
cu.user.is_member = False
|
|
cu.user.member_type = None
|
|
req = CreateGenerationTaskRequest(template_id="t1", asset_ids=["a1"], count=1)
|
|
with patch("packages.domain.points_service.PointsService") as MS:
|
|
svc = MagicMock()
|
|
svc.check_daily_free_clip.return_value = False
|
|
svc.deduct_points.return_value = {"success": False, "balance": 0}
|
|
MS.return_value = svc
|
|
with pytest.raises(HTTPException) as ei:
|
|
create_generation_task(
|
|
request=req, authenticated_user=cu, db=db,
|
|
generation_task_repository=MagicMock(), project_repository=MagicMock(),
|
|
asset_library_repository=MagicMock(), asset_repository=MagicMock(),
|
|
)
|
|
assert ei.value.status_code == 402
|
|
|
|
def test_decorator_attached(self):
|
|
from app.api.routes.generation_tasks import create_generation_task
|
|
assert hasattr(create_generation_task, "__wrapped__"), "missing @points_gate"
|