feat(backend): T1.3 月报管理 CRUD — 列表/详情/创建/更新/提交/删除
- 路由:GET/POST/PUT/DELETE /api/v1/reports + POST /reports/{id}/submit
- 状态机:draft → submitted → ai_parsed → reviewed
- 防重复:同年同月同企业不可重复创建
- 租户隔离:通过 company 关联校验
- 测试:7 个月报测试(总计 33 tests passed)
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
"""月报管理 CRUD 测试。"""
|
||||
|
||||
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": "report_test@example.com",
|
||||
"password": "password123",
|
||||
"name": "测试投资经理",
|
||||
"tenant_name": "测试机构",
|
||||
"role": "investor",
|
||||
},
|
||||
)
|
||||
resp = client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={"email": "report_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):
|
||||
"""创建测试企业,返回 ID。"""
|
||||
resp = client.post(
|
||||
"/api/v1/companies",
|
||||
json={"name": "月报测试公司", "industry": "AI"},
|
||||
headers=auth_headers,
|
||||
)
|
||||
return resp.json()["data"]["id"]
|
||||
|
||||
|
||||
class TestCreateReport:
|
||||
"""创建月报测试。"""
|
||||
|
||||
def test_create_success(self, client: TestClient, auth_headers: dict, company_id: str):
|
||||
"""正常创建月报。"""
|
||||
response = client.post(
|
||||
"/api/v1/reports",
|
||||
json={
|
||||
"company_id": company_id,
|
||||
"period_year": 2026,
|
||||
"period_month": 7,
|
||||
"raw_content": "本月营收增长20%",
|
||||
},
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert data["code"] == 0
|
||||
assert data["data"]["period_year"] == 2026
|
||||
assert data["data"]["period_month"] == 7
|
||||
assert data["data"]["status"] == "draft"
|
||||
|
||||
def test_create_duplicate(self, client: TestClient, auth_headers: dict, company_id: str):
|
||||
"""同年月重复创建应返回 409。"""
|
||||
# 先创建一个
|
||||
client.post(
|
||||
"/api/v1/reports",
|
||||
json={"company_id": company_id, "period_year": 2026, "period_month": 6},
|
||||
headers=auth_headers,
|
||||
)
|
||||
# 再创建同月
|
||||
response = client.post(
|
||||
"/api/v1/reports",
|
||||
json={"company_id": company_id, "period_year": 2026, "period_month": 6},
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert response.status_code == 409
|
||||
|
||||
|
||||
class TestListReports:
|
||||
"""月报列表测试。"""
|
||||
|
||||
def test_list_success(self, client: TestClient, auth_headers: dict, company_id: str):
|
||||
"""获取月报列表。"""
|
||||
response = client.get("/api/v1/reports", headers=auth_headers)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["code"] == 0
|
||||
assert data["data"]["total"] >= 1
|
||||
|
||||
def test_list_by_company(self, client: TestClient, auth_headers: dict, company_id: str):
|
||||
"""按企业筛选。"""
|
||||
response = client.get(
|
||||
f"/api/v1/reports?company_id={company_id}",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert all(item["company_id"] == company_id for item in data["data"]["items"])
|
||||
|
||||
|
||||
class TestSubmitReport:
|
||||
"""提交月报测试。"""
|
||||
|
||||
def test_submit_success(self, client: TestClient, auth_headers: dict, company_id: str):
|
||||
"""正常提交月报。"""
|
||||
create_resp = client.post(
|
||||
"/api/v1/reports",
|
||||
json={"company_id": company_id, "period_year": 2026, "period_month": 5},
|
||||
headers=auth_headers,
|
||||
)
|
||||
report_id = create_resp.json()["data"]["id"]
|
||||
|
||||
response = client.post(
|
||||
f"/api/v1/reports/{report_id}/submit",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["data"]["status"] == "submitted"
|
||||
assert data["data"]["submitted_at"] is not None
|
||||
|
||||
def test_submit_nonexistent(self, client: TestClient, auth_headers: dict):
|
||||
"""提交不存在的月报应返回 404。"""
|
||||
response = client.post(
|
||||
"/api/v1/reports/nonexistent-id/submit",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
class TestDeleteReport:
|
||||
"""删除月报测试。"""
|
||||
|
||||
def test_delete_success(self, client: TestClient, auth_headers: dict, company_id: str):
|
||||
"""正常删除。"""
|
||||
create_resp = client.post(
|
||||
"/api/v1/reports",
|
||||
json={"company_id": company_id, "period_year": 2026, "period_month": 4},
|
||||
headers=auth_headers,
|
||||
)
|
||||
report_id = create_resp.json()["data"]["id"]
|
||||
|
||||
response = client.delete(
|
||||
f"/api/v1/reports/{report_id}",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
# 验证已删除
|
||||
get_resp = client.get(
|
||||
f"/api/v1/reports/{report_id}",
|
||||
headers=auth_headers,
|
||||
)
|
||||
assert get_resp.status_code == 404
|
||||
Reference in New Issue
Block a user