"""月报管理 CRUD 测试。""" import pytest from fastapi.testclient import TestClient @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