68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
"""
|
|
pytest 全局配置和 fixtures。
|
|
单元测试使用 mock,集成测试需要真实服务。
|
|
"""
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
from typing import Dict, Any
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def mock_config_dict() -> Dict[str, Any]:
|
|
"""
|
|
模拟配置数据,用于不依赖真实 config.json 的测试。
|
|
"""
|
|
return {
|
|
"db_engine": {
|
|
"engine": "sqlite+pysqlite:///:memory:",
|
|
"async_engine": "sqlite+aiosqlite:///:memory:",
|
|
"engine_option": {"echo": False},
|
|
},
|
|
"redis": {
|
|
"connection": {
|
|
"url": "redis://localhost:6379/15",
|
|
},
|
|
},
|
|
"rbac": {
|
|
"table": {
|
|
"rule": "rbac_rule",
|
|
"user": "rbac_user",
|
|
"item": "rbac_item",
|
|
"assignment": "rbac_assignment",
|
|
"item_child": "rbac_item_child",
|
|
},
|
|
"user_class": "paste.rbac.rbac_user.RbacUser",
|
|
},
|
|
"logger": {
|
|
"default": {
|
|
"basic": {
|
|
"level": 40,
|
|
},
|
|
},
|
|
},
|
|
"tornado": {
|
|
"demo": {
|
|
"port": 9000,
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_config_file(mock_config_dict):
|
|
"""
|
|
创建临时配置文件,测试后自动清理。
|
|
"""
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="w", suffix=".json", delete=False, encoding="utf-8"
|
|
) as f:
|
|
json.dump(mock_config_dict, f, ensure_ascii=False)
|
|
temp_path = Path(f.name)
|
|
|
|
yield temp_path
|
|
|
|
# 清理
|
|
temp_path.unlink(missing_ok=True) |