Files
trade-assistant/backend/tests/test_customer_api.py
T
TradeMate Dev c6206787da Initial commit: TradeMate 外贸小助手 MVP
项目结构:
- backend/     Python FastAPI 后端
- uni-app/     uni-app跨端前端
- docs/        设计文档
- docker-compose.yml  Docker编排
- nginx/scripts/systemd 运维配置

已完成功能:
- 用户认证 (JWT)
- 智能翻译 + 回复建议
- 营销素材生成
- 客户管理 + 沉默检测
- 报价单管理
- 产品库管理
- 汇率换算
- 推送通知 (uni-push)
- WhatsApp Webhook框架
- Celery定时任务
2026-05-08 18:17:12 +08:00

147 lines
4.9 KiB
Python

import pytest
from httpx import AsyncClient
from app.models.customer import Customer
import uuid
class TestCustomerAPI:
async def test_list_customers_empty(self, client: AsyncClient, auth_headers):
response = await client.get("/api/v1/customers", headers=auth_headers)
assert response.status_code == 200
data = response.json()
assert "items" in data
assert data["items"] == []
assert data["total"] == 0
async def test_create_customer(self, client: AsyncClient, auth_headers):
response = await client.post(
"/api/v1/customers",
headers=auth_headers,
json={
"name": "John Smith",
"company": "ABC Corp",
"country": "USA",
"phone": "+1234567890",
"whatsapp_id": "john123",
"email": "john@abc.com",
"status": "lead",
},
)
assert response.status_code == 200
data = response.json()
assert data["name"] == "John Smith"
assert data["company"] == "ABC Corp"
assert data["country"] == "USA"
async def test_create_customer_minimal(self, client: AsyncClient, auth_headers):
response = await client.post(
"/api/v1/customers",
headers=auth_headers,
json={"name": "Minimal Customer"},
)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Minimal Customer"
async def test_list_customers_with_data(self, client: AsyncClient, auth_headers, db_session, test_user):
customer = Customer(
user_id=test_user.id,
name="Test Customer",
company="Test Co",
country="China",
status="lead",
)
db_session.add(customer)
await db_session.commit()
response = await client.get("/api/v1/customers", headers=auth_headers)
assert response.status_code == 200
data = response.json()
assert len(data["items"]) == 1
assert data["items"][0]["name"] == "Test Customer"
async def test_get_customer(self, client: AsyncClient, auth_headers, db_session, test_user):
customer = Customer(
user_id=test_user.id,
name="Get Test",
company="Get Co",
status="negotiating",
)
db_session.add(customer)
await db_session.commit()
response = await client.get(
f"/api/v1/customers/{customer.id}",
headers=auth_headers,
)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Get Test"
async def test_get_customer_not_found(self, client: AsyncClient, auth_headers):
fake_id = str(uuid.uuid4())
response = await client.get(
f"/api/v1/customers/{fake_id}",
headers=auth_headers,
)
assert response.status_code == 404
async def test_update_customer(self, client: AsyncClient, auth_headers, db_session, test_user):
customer = Customer(
user_id=test_user.id,
name="Original Name",
status="lead",
)
db_session.add(customer)
await db_session.commit()
response = await client.patch(
f"/api/v1/customers/{customer.id}",
headers=auth_headers,
json={"name": "Updated Name", "status": "negotiating"},
)
assert response.status_code == 200
data = response.json()
assert data["name"] == "Updated Name"
assert data["status"] == "negotiating"
async def test_delete_customer(self, client: AsyncClient, auth_headers, db_session, test_user):
customer = Customer(
user_id=test_user.id,
name="To Delete",
)
db_session.add(customer)
await db_session.commit()
customer_id = customer.id
response = await client.delete(
f"/api/v1/customers/{customer_id}",
headers=auth_headers,
)
assert response.status_code == 200
get_response = await client.get(
f"/api/v1/customers/{customer_id}",
headers=auth_headers,
)
assert get_response.status_code == 404
async def test_get_silent_customers(self, client: AsyncClient, auth_headers, db_session, test_user):
from datetime import datetime, timedelta
customer = Customer(
user_id=test_user.id,
name="Silent Customer",
status="lead",
last_contact_at=datetime.utcnow() - timedelta(days=5),
)
db_session.add(customer)
await db_session.commit()
response = await client.get(
"/api/v1/customers/silent?days=3",
headers=auth_headers,
)
assert response.status_code == 200
data = response.json()
assert data["count"] >= 1