Files
AIPortPilot/backend/tests/test_companies.py
T
selfrelease fad458b2a7 docs(uiux): UIUX 设计方案大改 + 5 份作业指导书对齐 + 开发任务文档
- UIUX 文档:填充 19 个缺口(多主体画像/健康度/AI+看板/增长域/洞察域/创始人端/OODA/助推/商密)
- UIUX 文档:插入 6 个新章节(十四~十九),旧章节重编号为二十~三十一,更新目录和交叉引用
- 作业指导书 x5:导航改为 6 域分组,新增 Context Bar/工作模式/Insight Rail/决策线程/多工作区等 UI 概念
- 新建 docs/2-task-uiux.md:50 个代码落地开发任务,按 P0-P6 分优先级 + 8 Sprint 规划
- 后端/前端:大量新增模型、路由、组件(来自之前 Phase 开发)
2026-07-19 11:53:38 +08:00

188 lines
6.0 KiB
Python

"""企业档案 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