""" 联系人模块测试 —— /api/customers/{cid}/contacts & /api/contacts/{id} 覆盖: CRUD 完整链路 """ import uuid import pytest from httpx import AsyncClient from tests.conftest import CUSTOMER_ID class TestContactsCRUD: """联系人 CRUD 全链路""" async def test_list_contacts_empty(self, client: AsyncClient, admin_headers): """初始无联系人 → 200 + 空数组""" resp = await client.get( f"/api/customers/{CUSTOMER_ID}/contacts", headers=admin_headers ) assert resp.status_code == 200 assert isinstance(resp.json()["data"], list) async def test_create_contact(self, client: AsyncClient, admin_headers): """新增联系人 → 200""" resp = await client.post( f"/api/customers/{CUSTOMER_ID}/contacts", headers=admin_headers, json={"name": "王采购", "phone": "13700137000", "title": "采购总监"} ) assert resp.status_code == 200 data = resp.json()["data"] assert data["name"] == "王采购" return data.get("id") async def test_create_and_update_contact(self, client: AsyncClient, admin_headers): """新增后编辑联系人 → 200""" # 新增 create_resp = await client.post( f"/api/customers/{CUSTOMER_ID}/contacts", headers=admin_headers, json={"name": "临时联系人", "phone": "10000"} ) assert create_resp.status_code == 200 contact_id = create_resp.json()["data"]["id"] # 编辑 update_resp = await client.put( f"/api/contacts/{contact_id}", headers=admin_headers, json={"name": "正式联系人", "title": "技术经理"} ) assert update_resp.status_code == 200 async def test_create_and_delete_contact(self, client: AsyncClient, admin_headers): """新增后删除联系人 → 200""" create_resp = await client.post( f"/api/customers/{CUSTOMER_ID}/contacts", headers=admin_headers, json={"name": "待删联系人"} ) contact_id = create_resp.json()["data"]["id"] del_resp = await client.delete( f"/api/contacts/{contact_id}", headers=admin_headers ) assert del_resp.status_code == 200 async def test_create_contact_no_name(self, client: AsyncClient, admin_headers): """缺少 name → 422""" resp = await client.post( f"/api/customers/{CUSTOMER_ID}/contacts", headers=admin_headers, json={"phone": "123"} ) assert resp.status_code == 422