109 lines
2.9 KiB
Python
109 lines
2.9 KiB
Python
"""
|
||
Pytest 配置和共享 fixtures
|
||
"""
|
||
import pytest
|
||
from fastapi.testclient import TestClient
|
||
import sys
|
||
import os
|
||
|
||
# 添加项目根目录到 sys.path
|
||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
|
||
from main import app
|
||
from config.settings import settings
|
||
from routers.api import file_service
|
||
|
||
|
||
class FakeStaffIdentityRepository:
|
||
def select_random_staff_with_families(self):
|
||
return {
|
||
"staff_name": "测试员工",
|
||
"staff_id_card": "320101199001010030",
|
||
"family_id_cards": ["320101199201010051"],
|
||
}
|
||
|
||
|
||
@pytest.fixture(autouse=True)
|
||
def reset_file_service_state():
|
||
"""避免 file_service 单例状态影响测试顺序。"""
|
||
file_service.file_records.clear()
|
||
file_service.log_counter = settings.INITIAL_LOG_ID
|
||
file_service.staff_identity_repository = FakeStaffIdentityRepository()
|
||
yield
|
||
file_service.file_records.clear()
|
||
file_service.log_counter = settings.INITIAL_LOG_ID
|
||
|
||
|
||
@pytest.fixture
|
||
def client():
|
||
"""创建测试客户端"""
|
||
original_routes = list(app.router.routes)
|
||
try:
|
||
from routers import credit_api
|
||
|
||
if not any(route.path == "/xfeature-mngs/conversation/htmlEval" for route in app.routes):
|
||
app.include_router(credit_api.router, tags=["征信解析接口"])
|
||
app.openapi_schema = None
|
||
except ModuleNotFoundError:
|
||
pass
|
||
|
||
try:
|
||
yield TestClient(app)
|
||
finally:
|
||
app.router.routes[:] = original_routes
|
||
app.openapi_schema = None
|
||
|
||
|
||
@pytest.fixture
|
||
def sample_token_request():
|
||
"""示例 Token 请求 - 返回 form-data 格式的数据"""
|
||
return {
|
||
"projectNo": "test_project_001",
|
||
"entityName": "测试企业",
|
||
"userId": "902001",
|
||
"userName": "902001",
|
||
"appId": "remote_app",
|
||
"appSecretCode": "test_secret_code_12345",
|
||
"role": "VIEWER",
|
||
"orgCode": "902000",
|
||
"departmentCode": "902000",
|
||
}
|
||
|
||
|
||
@pytest.fixture
|
||
def sample_inner_flow_request():
|
||
"""示例拉取行内流水请求"""
|
||
return {
|
||
"groupId": 1001,
|
||
"customerNo": "test_customer_001",
|
||
"dataChannelCode": "test_code",
|
||
"requestDateId": 20240101,
|
||
"dataStartDateId": 20240101,
|
||
"dataEndDateId": 20240131,
|
||
"uploadUserId": 902001,
|
||
}
|
||
|
||
|
||
@pytest.fixture
|
||
def sample_credit_html_file():
|
||
"""示例征信 HTML 文件。"""
|
||
html = """
|
||
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<meta name="ccdi-staff-name" content="测试员工" />
|
||
<meta name="ccdi-staff-id-card" content="320101199001010030" />
|
||
<title>征信解析员工样本</title>
|
||
</head>
|
||
<body>
|
||
<main>
|
||
<h1>征信解析员工样本</h1>
|
||
<p>姓名:测试员工</p>
|
||
<p>身份证号:320101199001010030</p>
|
||
</main>
|
||
</body>
|
||
</html>
|
||
"""
|
||
return ("credit.html", html.encode("utf-8"), "text/html")
|