40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
|
|
"""pytest 共享fixtures 与约定入口(可按 backend-testing-strategy 逐步扩展)。"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import uuid
|
|||
|
|
from datetime import datetime, timezone
|
|||
|
|
from typing import Callable
|
|||
|
|
|
|||
|
|
import pytest
|
|||
|
|
|
|||
|
|
from app.features.user.models import User
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def unique_phone() -> str:
|
|||
|
|
"""避免与测试库中已存在手机号冲突(11 位)。"""
|
|||
|
|
return f"138{uuid.uuid4().int % 100_000_000:08d}"
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def make_test_user(unique_phone: str) -> Callable[..., User]:
|
|||
|
|
"""工厂:构造仅用于响应序列化的 User 行(未入库)。"""
|
|||
|
|
|
|||
|
|
def _make(
|
|||
|
|
*,
|
|||
|
|
user_id: str | None = None,
|
|||
|
|
phone: str | None = None,
|
|||
|
|
nickname: str = "测试用户",
|
|||
|
|
) -> User:
|
|||
|
|
return User(
|
|||
|
|
id=user_id or str(uuid.uuid4()),
|
|||
|
|
phone=phone or unique_phone,
|
|||
|
|
password_hash="x",
|
|||
|
|
nickname=nickname,
|
|||
|
|
subscription_type="free",
|
|||
|
|
created_at=datetime.now(timezone.utc),
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
return _make
|