mirror of
https://git.oceanpay.cc/danial/kami_apple_exchage.git
synced 2025-12-18 22:29:09 +00:00
本次提交将后端的任务队列系统从Celery迁移到了Arq,以支持基于协程的任务处理。主要改动包括: - 更新文档和配置文件,反映架构变化。 - 修改健康检查和服务初始化逻辑,以适应Arq的使用。 - 移除与Celery相关的代码,并添加Arq任务定义和调度器。 - 更新Dockerfile和相关脚本,确保Arq worker能够正确运行。 - 调整API和业务服务中的任务处理逻辑,移除对Celery的依赖。 这些改动旨在提高系统的异步处理能力和整体性能。
187 lines
5.6 KiB
Python
187 lines
5.6 KiB
Python
"""
|
||
订单API单元测试
|
||
"""
|
||
|
||
from datetime import datetime
|
||
from unittest.mock import AsyncMock, patch
|
||
|
||
import pytest
|
||
from httpx import AsyncClient
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_order_stats(client: AsyncClient):
|
||
"""测试获取订单统计"""
|
||
mock_stats = {
|
||
"total_orders": 100,
|
||
"pending_orders": 10,
|
||
"completed_orders": 80,
|
||
"failed_orders": 10,
|
||
}
|
||
|
||
with patch(
|
||
"app.services.order_business_service.OrderService.get_order_statistics"
|
||
) as mock_get_stats:
|
||
mock_get_stats.return_value = mock_stats
|
||
|
||
response = await client.get("/api/v1/orders/stats")
|
||
|
||
assert response.status_code == 200
|
||
data = response.json()
|
||
assert data["total_orders"] == 100
|
||
assert data["pending_orders"] == 10
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_orders_list(client: AsyncClient):
|
||
"""测试获取订单列表"""
|
||
mock_orders = [
|
||
{
|
||
"id": "order_1",
|
||
"status": "pending",
|
||
"created_at": datetime.now().isoformat(),
|
||
"user_data": {"email": "test@example.com"},
|
||
},
|
||
{
|
||
"id": "order_2",
|
||
"status": "completed",
|
||
"created_at": datetime.now().isoformat(),
|
||
"user_data": {"email": "test2@example.com"},
|
||
},
|
||
]
|
||
|
||
with patch(
|
||
"app.services.order_business_service.OrderService.get_order_list"
|
||
) as mock_get_list:
|
||
mock_get_list.return_value = mock_orders
|
||
|
||
response = await client.get("/api/v1/orders/list")
|
||
|
||
assert response.status_code == 200
|
||
data = response.json()
|
||
assert len(data) == 2
|
||
assert data[0]["id"] == "order_1"
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_orders_list_with_filters(client: AsyncClient):
|
||
"""测试带过滤条件的订单列表"""
|
||
mock_orders = [
|
||
{"id": "order_1", "status": "pending", "created_at": datetime.now().isoformat()}
|
||
]
|
||
|
||
with patch(
|
||
"app.services.order_business_service.OrderService.get_order_list"
|
||
) as mock_get_list:
|
||
mock_get_list.return_value = mock_orders
|
||
|
||
response = await client.get(
|
||
"/api/v1/orders/list?skip=0&limit=10&status_filter=pending"
|
||
)
|
||
|
||
assert response.status_code == 200
|
||
data = response.json()
|
||
assert len(data) == 1
|
||
assert data[0]["status"] == "pending"
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_order_detail_exists(client: AsyncClient):
|
||
"""测试获取存在的订单详情"""
|
||
order_id = "order_123"
|
||
mock_order = {
|
||
"id": order_id,
|
||
"status": "pending",
|
||
"created_at": datetime.now().isoformat(),
|
||
"user_data": {"email": "test@example.com"},
|
||
}
|
||
|
||
with patch(
|
||
"app.services.order_business_service.OrderService.get_order_detail"
|
||
) as mock_get_detail:
|
||
mock_get_detail.return_value = mock_order
|
||
|
||
response = await client.get(f"/api/v1/orders/{order_id}")
|
||
|
||
assert response.status_code == 200
|
||
data = response.json()
|
||
assert data["id"] == order_id
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_order_detail_not_exists(client: AsyncClient):
|
||
"""测试获取不存在的订单详情"""
|
||
order_id = "nonexistent_order"
|
||
|
||
with patch(
|
||
"app.services.order_business_service.OrderService.get_order_detail"
|
||
) as mock_get_detail:
|
||
mock_get_detail.return_value = None
|
||
|
||
response = await client.get(f"/api/v1/orders/{order_id}")
|
||
|
||
assert response.status_code == 404
|
||
assert "订单不存在" in response.json()["detail"]
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_export_orders(client: AsyncClient):
|
||
"""测试导出订单数据"""
|
||
mock_excel_content = b"fake excel content"
|
||
mock_filename = "orders_export.xlsx"
|
||
|
||
with patch(
|
||
"app.services.order_business_service.OrderService.export_orders_to_excel"
|
||
) as mock_export:
|
||
mock_export.return_value = (mock_excel_content, mock_filename)
|
||
|
||
response = await client.get("/api/v1/orders/export")
|
||
|
||
assert response.status_code == 200
|
||
assert (
|
||
response.headers["content-type"]
|
||
== "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||
)
|
||
assert mock_filename in response.headers["content-disposition"]
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_workers_status(client: AsyncClient):
|
||
"""测试获取工作进程状态"""
|
||
response = await client.get("/api/v1/orders/workers/status")
|
||
|
||
assert response.status_code == 200
|
||
data = response.json()
|
||
assert isinstance(data, list)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_queue_stats(client: AsyncClient):
|
||
"""测试获取队列统计 - Arq版本"""
|
||
# 由于队列统计API已经更新为返回空统计数据(Celery被Arq替代)
|
||
# 这里测试API的基本功能
|
||
response = await client.get("/api/v1/orders/queue/stats")
|
||
|
||
assert response.status_code == 200
|
||
data = response.json()
|
||
assert "success" in data
|
||
assert "stats" in data
|
||
assert "timestamp" in data
|
||
# Arq版本的队列统计返回空数据
|
||
assert data["stats"]["total_active"] == 0
|
||
assert data["stats"]["total_scheduled"] == 0
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_get_order_stats_error(client: AsyncClient):
|
||
"""测试获取订单统计时发生错误"""
|
||
with patch(
|
||
"app.services.order_business_service.OrderService.get_order_statistics"
|
||
) as mock_get_stats:
|
||
mock_get_stats.side_effect = Exception("数据库连接失败")
|
||
|
||
response = await client.get("/api/v1/orders/stats")
|
||
|
||
assert response.status_code == 500
|
||
assert "获取订单统计失败" in response.json()["detail"]
|