Files
AIPortPilot/backend/tests/test_report_export.py
T
selfrelease 63031dd183 feat: T1.7 投后报告导出 + T1.8 docker-compose.dev.yml + T0.4 补全
- 后端:reports_export 路由(summary 聚合 + JSON 导出)
- 测试:3 个导出测试,总计 67 passed
- docker-compose.dev.yml:开发覆盖(源码挂载 + hot reload + debugpy)
- 前端 14 路由构建成功
2026-07-18 22:21:51 +08:00

102 lines
3.1 KiB
Python

"""投后报告导出测试。"""
import pytest
from fastapi.testclient import TestClient
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from app.core.database import Base, get_db
from app.main import app
TEST_DATABASE_URL = "sqlite+aiosqlite:///file::memory:?cache=shared&uri=true"
test_engine = create_async_engine(TEST_DATABASE_URL, echo=False)
test_session_factory = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
async def override_get_db():
async with test_session_factory() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
app.dependency_overrides[get_db] = override_get_db
@pytest.fixture(scope="module", autouse=True)
async def setup_db():
async with test_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield
async with test_engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
@pytest.fixture
def client():
return TestClient(app)
@pytest.fixture
def auth_headers(client: TestClient):
client.post(
"/api/v1/auth/register",
json={
"email": "export_test@example.com",
"password": "password123",
"name": "测试投资经理",
"tenant_name": "测试机构",
"role": "investor",
},
)
resp = client.post(
"/api/v1/auth/login",
json={"email": "export_test@example.com", "password": "password123"},
)
token = resp.json()["data"]["access_token"]
return {"Authorization": f"Bearer {token}"}
@pytest.fixture
def company_id(client: TestClient, auth_headers: dict):
resp = client.post(
"/api/v1/companies",
json={"name": "导出测试公司", "industry": "AI"},
headers=auth_headers,
)
return resp.json()["data"]["id"]
class TestReportExport:
"""投后报告导出。"""
def test_summary_success(self, client: TestClient, auth_headers: dict, company_id: str):
"""获取报告汇总。"""
response = client.get(
f"/api/v1/reports-export/{company_id}/summary",
headers=auth_headers,
)
assert response.status_code == 200
data = response.json()
assert data["code"] == 0
assert data["data"]["company"]["name"] == "导出测试公司"
def test_summary_nonexistent(self, client: TestClient, auth_headers: dict):
"""不存在的企业应返回 404。"""
response = client.get(
"/api/v1/reports-export/nonexistent/summary",
headers=auth_headers,
)
assert response.status_code == 404
def test_export_json(self, client: TestClient, auth_headers: dict, company_id: str):
"""导出 JSON 报告。"""
response = client.get(
f"/api/v1/reports-export/{company_id}/pdf",
headers=auth_headers,
)
assert response.status_code == 200
assert "attachment" in response.headers.get("content-disposition", "")