ForcePilot/backend/test/unit/external_systems/adapters/persistence/conftest.py
Kris 08617091dc refactor: 整理项目包结构与导入路径
- 新增多个业务域的__init__.py模块文件,规范包导出结构
- 调整多个DTO文件的导入路径,统一模块组织方式
- 移除测试文件中多余的空行与导入语句
- 优化部分业务模块的包层级划分
2026-07-18 02:04:03 +08:00

52 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""persistence 适配器层单元测试共享 fixture。
提供:
- ``crypto_with_key``:注入带密钥的 CryptoHelper使 encrypt/decrypt 真实往返,
测试后自动 reset。
- ``orm_factory``:用 ``SimpleNamespace`` 构造类 ORM 对象,按字段覆盖。
"""
from __future__ import annotations
from types import SimpleNamespace
from typing import Any
import pytest
from cryptography.fernet import Fernet
from yuxi.utils.crypto import CryptoHelper, reset_crypto_helper, set_crypto_helper
@pytest.fixture
def crypto_with_key() -> str:
"""注入带密钥的 CryptoHelper返回密钥字符串。
使用后自动 reset 为无密钥实例,保证测试隔离。
"""
key = Fernet.generate_key().decode()
set_crypto_helper(CryptoHelper(key=key))
yield key
reset_crypto_helper()
def make_orm(**fields: Any) -> SimpleNamespace:
"""构造类 ORM 对象SimpleNamespace按字段覆盖。
mapper 直接读 ORM 属性SimpleNamespace 足以模拟 ORM 实例,
无需依赖真实 SQLAlchemy 模型。
"""
return SimpleNamespace(**fields)
@pytest.fixture
def orm_factory() -> type[SimpleNamespace]:
"""返回 ORM 工厂,调用时按需传字段。"""
return make_orm
@pytest.fixture
def mock_db() -> Any:
"""返回 AsyncSession mock仅作为仓储构造参数占位实际由 repo._repo mock 接管)。"""
from unittest.mock import AsyncMock
return AsyncMock()