Files
ai-talk-callback/TEST_README.md
2025-12-02 18:36:58 +08:00

156 lines
4.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# AI Talk Callback 接口测试说明
## 概述
本项目为 AI Talk 回调接口编写了完整的测试用例,覆盖了各种场景和边界条件。
## 测试文件
- `test_callback.py` - 主要测试文件,包含所有接口测试用例
- `conftest.py` - pytest配置文件,包含测试fixtures
- `run_tests.py` - 测试运行脚本
## 测试覆盖的场景
### 1. 基础功能测试
- ✅ 根接口测试 (`GET /`)
- ✅ 健康检查接口测试 (`GET /health`)
### 2. 回调接口测试 (`POST /ai-talk/callback/{siteId}/failure`)
#### 2.1 正常业务流程
- ✅ count < 3 且外部API调用成功
- ✅ count >= 3 直接返回(不调用外部API)
- ✅ 空数据列表处理
#### 2.2 异常情况处理
- ✅ 外部API调用失败
- ✅ 数据库连接错误
- ✅ Redis锁获取失败
- ✅ 服务器内部错误
#### 2.3 数据验证
- ✅ 无效请求数据(缺少必需字段)
- ✅ 负数count值验证
## 运行测试
### 方法1: 使用pytest直接运行
```bash
# 安装测试依赖
pip install -r requirements.txt
# 运行所有测试
pytest test_callback.py -v
# 运行特定测试
pytest test_callback.py::TestAITalkCallback::test_callback_count_below_threshold_success -v
# 运行并显示覆盖率
pytest test_callback.py -v --cov=app --cov-report=html
```
### 方法2: 使用运行脚本
```bash
# 运行所有测试
python run_tests.py
# 运行特定测试
python run_tests.py test_callback_count_below_threshold_success
```
## 测试数据
测试使用了模拟的回调数据,包含完整的字段结构:
```python
{
"count": 2,
"data": [
{
"bill": 100,
"duration": 60,
"callid": "test-call-123",
"calldate": "2024-01-01 12:00:00",
"number": "13800138000",
"numberid": "number-123",
"customer_id": "customer-123",
"status": 0,
"status_str": "失败",
"user_id": "user-123",
"type": 1,
"number_data": {
"number": "13800138000",
"province": "广东",
"city": "深圳",
"operator": "移动"
},
"group": {
"id": 1,
"name": "测试组"
},
"task": {
"id": "task-123",
"name": "测试任务"
},
"user": {
"id": "user-123",
"name": "测试用户"
},
"customer_data": {
"name": "测试客户",
"email": "test@example.com",
"company": "测试公司",
"extra": "额外信息"
}
}
]
}
```
## Mock策略
测试中使用了以下Mock策略:
1. **数据库Mock**: 使用 `AsyncMock` 模拟数据库会话
2. **Redis Mock**: 模拟Redis分布式锁操作
3. **外部API Mock**: 模拟外部API调用和重试机制
4. **日志记录Mock**: 模拟数据库日志记录功能
## 测试环境配置
测试使用SQLite内存数据库,避免影响生产数据:
```python
TEST_DATABASE_URL = "sqlite+aiosqlite:///./test.db"
```
## 预期测试结果
成功运行时应该看到类似输出:
```
============================= test session starts =============================
collected 10 items
test_callback.py::TestAITalkCallback::test_root_endpoint PASSED [ 10%]
test_callback.py::TestAITalkCallback::test_health_check PASSED [ 20%]
test_callback.py::TestAITalkCallback::test_callback_count_below_threshold_success PASSED [ 30%]
test_callback.py::TestAITalkCallback::test_callback_count_above_threshold_direct_return PASSED [ 40%]
test_callback.py::TestAITalkCallback::test_callback_external_api_failure PASSED [ 50%]
test_callback.py::TestAITalkCallback::test_callback_invalid_request_data PASSED [ 60%]
test_callback.py::TestAITalkCallback::test_callback_invalid_count_negative PASSED [ 70%]
test_callback.py::TestAITalkCallback::test_callback_empty_data_list PASSED [ 80%]
test_callback.py::TestAITalkCallback::test_callback_database_error PASSED [ 90%]
test_callback.py::TestAITalkCallback::test_callback_redis_lock_error PASSED [100%]
============================== 10 passed in 2.34s ==============================
✅ 所有测试通过!
```
## 注意事项
1. 确保安装了所有测试依赖
2. 测试运行时会创建临时SQLite数据库文件
3. 测试使用了Mock,不会实际调用外部API
4. 建议在CI/CD环境中集成这些测试