"""企业档案 CRUD 测试。""" import pytest from fastapi.testclient import TestClient @pytest.fixture def auth_headers(client: TestClient): """注册并登录,返回认证头。""" client.post( "/api/v1/auth/register", json={ "email": "company_test@example.com", "password": "password123", "name": "测试投资经理", "tenant_name": "测试机构", "role": "investor", }, ) resp = client.post( "/api/v1/auth/login", json={"email": "company_test@example.com", "password": "password123"}, ) token = resp.json()["data"]["access_token"] return {"Authorization": f"Bearer {token}"} class TestCreateCompany: """创建企业测试。""" def test_create_success(self, client: TestClient, auth_headers: dict): """正常创建企业。""" response = client.post( "/api/v1/companies", json={ "name": "AI科技初创公司", "industry": "人工智能", "stage": "seed", "description": "专注于AI投后管理", "website": "https://example.com", }, headers=auth_headers, ) assert response.status_code == 201 data = response.json() assert data["code"] == 0 assert data["data"]["name"] == "AI科技初创公司" assert data["data"]["industry"] == "人工智能" def test_create_without_auth(self, client: TestClient): """无认证应返回 401。""" response = client.post( "/api/v1/companies", json={"name": "测试公司"}, ) assert response.status_code == 401 def test_create_empty_name(self, client: TestClient, auth_headers: dict): """空名称应返回 422。""" response = client.post( "/api/v1/companies", json={"name": ""}, headers=auth_headers, ) assert response.status_code == 422 class TestListCompanies: """企业列表测试。""" def test_list_success(self, client: TestClient, auth_headers: dict): """获取企业列表。""" response = client.get("/api/v1/companies", headers=auth_headers) assert response.status_code == 200 data = response.json() assert data["code"] == 0 assert data["data"]["total"] >= 1 assert len(data["data"]["items"]) >= 1 def test_list_with_keyword(self, client: TestClient, auth_headers: dict): """关键词搜索。""" response = client.get( "/api/v1/companies?keyword=AI科技", headers=auth_headers, ) assert response.status_code == 200 data = response.json() assert all("AI科技" in item["name"] for item in data["data"]["items"]) def test_list_pagination(self, client: TestClient, auth_headers: dict): """分页参数。""" response = client.get( "/api/v1/companies?page=1&page_size=5", headers=auth_headers, ) assert response.status_code == 200 data = response.json() assert data["data"]["page"] == 1 assert data["data"]["page_size"] == 5 class TestGetCompany: """获取企业详情测试。""" def test_get_success(self, client: TestClient, auth_headers: dict): """正常获取详情。""" # 先创建 create_resp = client.post( "/api/v1/companies", json={"name": "详情测试公司", "industry": "SaaS"}, headers=auth_headers, ) company_id = create_resp.json()["data"]["id"] response = client.get( f"/api/v1/companies/{company_id}", headers=auth_headers, ) assert response.status_code == 200 data = response.json() assert data["data"]["name"] == "详情测试公司" def test_get_nonexistent(self, client: TestClient, auth_headers: dict): """不存在的 ID 应返回 404。""" response = client.get( "/api/v1/companies/nonexistent-id", headers=auth_headers, ) assert response.status_code == 404 class TestUpdateCompany: """更新企业测试。""" def test_update_success(self, client: TestClient, auth_headers: dict): """正常更新。""" create_resp = client.post( "/api/v1/companies", json={"name": "更新前公司", "industry": "电商"}, headers=auth_headers, ) company_id = create_resp.json()["data"]["id"] response = client.put( f"/api/v1/companies/{company_id}", json={"name": "更新后公司", "stage": "a"}, headers=auth_headers, ) assert response.status_code == 200 data = response.json() assert data["data"]["name"] == "更新后公司" assert data["data"]["stage"] == "a" assert data["data"]["industry"] == "电商" # 未更新的字段保持不变 class TestDeleteCompany: """删除企业测试。""" def test_delete_success(self, client: TestClient, auth_headers: dict): """正常删除。""" create_resp = client.post( "/api/v1/companies", json={"name": "待删除公司"}, headers=auth_headers, ) company_id = create_resp.json()["data"]["id"] response = client.delete( f"/api/v1/companies/{company_id}", headers=auth_headers, ) assert response.status_code == 200 # 验证已删除 get_resp = client.get( f"/api/v1/companies/{company_id}", headers=auth_headers, ) assert get_resp.status_code == 404 def test_delete_nonexistent(self, client: TestClient, auth_headers: dict): """删除不存在的企业应返回 404。""" response = client.delete( "/api/v1/companies/nonexistent-id", headers=auth_headers, ) assert response.status_code == 404