Files
rag/scripts/test_all_52_endpoints.sh
lacerate551 100d1a06eb init: RAG 知识库服务初始提交
- 后端 API(Flask + Gunicorn)
- RAG 引擎(混合检索 + 云端 Reranker + 引用溯源)
- 文档解析(MinerU + 多格式支持)
- Docker 生产部署配置
- 排除前端项目、敏感配置、模型文件
2026-06-04 17:35:27 +08:00

194 lines
9.7 KiB
Bash

#!/bin/bash
# RAG API 完整测试脚本 - 覆盖 curl测试手册.md 所有 52 个接口
set -e
BASE_URL="http://localhost:5001"
PASS=0
FAIL=0
RESULTS=""
log_result() {
local name="$1"
local status="$2"
if [ "$status" = "PASS" ]; then
PASS=$((PASS + 1))
echo "$name"
else
FAIL=$((FAIL + 1))
echo "$name"
fi
}
test_endpoint() {
local name="$1"
local method="$2"
local path="$3"
local data="$4"
local check="$5"
if [ "$method" = "GET" ]; then
result=$(curl -s "$BASE_URL$path" 2>&1)
else
result=$(curl -s -X $method "$BASE_URL$path" -H "Content-Type: application/json" -d "$data" 2>&1)
fi
if echo "$result" | grep -q "$check"; then
log_result "$name" "PASS"
else
log_result "$name" "FAIL"
fi
}
echo "=========================================="
echo "RAG API 完整测试 (52 接口)"
echo "=========================================="
# ===== 2. 健康检查 (2个) =====
echo -e "\n--- 2. 健康检查 ---"
test_endpoint "GET /health" "GET" "/health" "" '"status"'
curl -s "$BASE_URL/health" > /dev/null && log_result "GET /health (HTTP)" "PASS" || log_result "GET /health (HTTP)" "FAIL"
# ===== 3. 问答接口 (2个) =====
echo -e "\n--- 3. 问答接口 ---"
echo '{"message":"三峡工程","chat_history":[]}' > /tmp/rag.json
curl -s -X POST "$BASE_URL/rag" -H "Content-Type: application/json" -d @/tmp/rag.json 2>&1 | grep -q '"type":"finish"' && log_result "POST /rag" "PASS" || log_result "POST /rag" "FAIL"
echo '{"message":"你好","chat_history":[]}' > /tmp/chat.json
test_endpoint "POST /chat" "POST" "/chat" '{"message":"你好","chat_history":[]}' '"answer"'
# ===== 4. 检索接口 (1个) =====
echo -e "\n--- 4. 检索接口 ---"
test_endpoint "POST /search" "POST" "/search" '{"query":"三峡工程","top_k":3}' '"contexts"'
# ===== 5. 向量库管理 (8个) =====
echo -e "\n--- 5. 向量库管理 ---"
test_endpoint "GET /collections" "GET" "/collections" "" '"collections"'
test_endpoint "POST /collections" "POST" "/collections" '{"name":"test_api_kb","display_name":"测试库"}' '"success"'
test_endpoint "PUT /collections/test_api_kb" "PUT" "/collections/test_api_kb" '{"display_name":"测试库-修改"}' '"success"'
test_endpoint "GET /collections/public_kb/documents" "GET" "/collections/public_kb/documents" "" '"documents"'
test_endpoint "GET /collections/public_kb/chunks" "GET" "/collections/public_kb/chunks?limit=2" "" '"chunks"'
test_endpoint "GET /collections/public_kb/documents/versions" "GET" "/collections/public_kb/documents/%E4%B8%89%E5%B3%A1%E5%85%AC%E6%8A%A5_1-15%E9%A1%B5.pdf/versions" "" '"versions"'
test_endpoint "POST /collections/public_kb/update-image-descriptions" "POST" "/collections/public_kb/update-image-descriptions" "" '"success"'
curl -s -X DELETE "$BASE_URL/collections/test_api_kb" | grep -q '"success"' && log_result "DELETE /collections/test_api_kb" "PASS" || log_result "DELETE /collections/test_api_kb" "FAIL"
# ===== 6. 文档管理 (10个) =====
echo -e "\n--- 6. 文档管理 ---"
test_endpoint "GET /documents/list" "GET" "/documents/list?collection=public_kb&page=1&page_size=5" "" '"documents"'
test_endpoint "GET /documents/status" "GET" "/documents/public_kb%2Ftest.txt/status" "" '"status"'
# 创建测试文件上传
echo "测试文档内容" > /tmp/test_upload.txt
result=$(curl -s -X POST "$BASE_URL/documents/upload" -F "file=@/tmp/test_upload.txt" -F "collection=public_kb")
echo "$result" | grep -q '"success"' && log_result "POST /documents/upload" "PASS" || log_result "POST /documents/upload" "FAIL"
# 批量上传
echo "文件1" > /tmp/file1.txt
echo "文件2" > /tmp/file2.txt
result=$(curl -s -X POST "$BASE_URL/documents/batch-upload" -F "files=@/tmp/file1.txt" -F "files=@/tmp/file2.txt" -F "collection=public_kb")
echo "$result" | grep -q '"success_count"' && log_result "POST /documents/batch-upload" "PASS" || log_result "POST /documents/batch-upload" "FAIL"
# 更新文档
echo "更新内容" > /tmp/update.txt
result=$(curl -s -X PUT "$BASE_URL/documents/public_kb%2Ftest_upload.txt" -F "file=@/tmp/update.txt")
echo "$result" | grep -q '"success"' && log_result "PUT /documents/<path>" "PASS" || log_result "PUT /documents/<path>" "FAIL"
# 删除文档
curl -s -X DELETE "$BASE_URL/documents/public_kb%2Ftest_upload.txt" | grep -q '"success"' && log_result "DELETE /documents/<path>" "PASS" || log_result "DELETE /documents/<path>" "FAIL"
# 废弃/恢复
test_endpoint "POST /documents/deprecate" "POST" "/collections/public_kb/documents/test.txt/deprecate" '' '"success"'
test_endpoint "POST /documents/restore" "POST" "/collections/public_kb/documents/test.txt/restore" '' '"success"'
# ===== 7. 切片管理 (4个) =====
echo -e "\n--- 7. 切片管理 ---"
test_endpoint "GET /documents/chunks" "GET" "/documents/public_kb%2F%E4%B8%89%E5%B3%A1%E5%85%AC%E6%8A%A5_1-15%E9%A1%B5.pdf/chunks?page=1&page_size=2" "" '"chunks"'
test_endpoint "POST /chunks" "POST" "/chunks" '{"collection":"public_kb","content":"测试切片内容"}' '"success"'
# 获取一个真实切片ID
chunk_id=$(curl -s "$BASE_URL/collections/public_kb/chunks?limit=1" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -n "$chunk_id" ]; then
curl -s -X PUT "$BASE_URL/chunks/$chunk_id" -H "Content-Type: application/json" -d '{"collection":"public_kb","content":"更新内容"}' | grep -q '"success"' && log_result "PUT /chunks/<id>" "PASS" || log_result "PUT /chunks/<id>" "FAIL"
else
log_result "PUT /chunks/<id>" "FAIL (无切片)"
fi
# ===== 8. 同步服务 (6个) =====
echo -e "\n--- 8. 同步服务 ---"
test_endpoint "POST /sync" "POST" "/sync" '' '"success"'
test_endpoint "GET /sync/status" "GET" "/sync/status" "" '"enabled"'
test_endpoint "GET /sync/history" "GET" "/sync/history" "" '"history"'
test_endpoint "GET /sync/changes" "GET" "/sync/changes" "" '"changes"'
test_endpoint "POST /sync/start" "POST" "/sync/start" '' '"success"'
test_endpoint "POST /sync/stop" "POST" "/sync/stop" '' '"success"'
# ===== 9. 反馈系统 (5个) =====
echo -e "\n--- 9. 反馈系统 ---"
test_endpoint "POST /feedback" "POST" "/feedback" '{"session_id":"test","query":"测试问题","answer":"测试回答","rating":1}' '"success"'
test_endpoint "GET /feedback/list" "GET" "/feedback/list?page=1&page_size=5" "" '"feedbacks"'
test_endpoint "GET /feedback/stats" "GET" "/feedback/stats" "" '"stats"'
test_endpoint "GET /feedback/bad-cases" "GET" "/feedback/bad-cases" "" '"bad_cases"'
test_endpoint "GET /feedback/blacklist" "GET" "/feedback/blacklist" "" '"blacklist"'
# ===== 10. FAQ 管理 (7个) =====
echo -e "\n--- 10. FAQ 管理 ---"
test_endpoint "GET /faq" "GET" "/faq?page=1&page_size=5" "" '"faqs"'
result=$(curl -s -X POST "$BASE_URL/faq" -H "Content-Type: application/json" -d '{"question":"测试FAQ问题","answer":"测试FAQ答案"}')
echo "$result" | grep -q '"faq_id"' && log_result "POST /faq" "PASS" || log_result "POST /faq" "FAIL"
faq_id=$(echo "$result" | grep -o '"faq_id":[0-9]*' | cut -d':' -f2)
if [ -n "$faq_id" ]; then
curl -s -X PUT "$BASE_URL/faq/$faq_id" -H "Content-Type: application/json" -d '{"question":"更新问题","answer":"更新答案"}' | grep -q '"success"' && log_result "PUT /faq/<id>" "PASS" || log_result "PUT /faq/<id>" "FAIL"
fi
test_endpoint "GET /faq/suggestions" "GET" "/faq/suggestions?page=1&page_size=5" "" '"suggestions"'
test_endpoint "POST /faq/suggestions/approve" "POST" "/faq/suggestions/1/approve" '{}' '"success"'
test_endpoint "POST /faq/suggestions/reject" "POST" "/faq/suggestions/1/reject" '{}' '"success"'
if [ -n "$faq_id" ]; then
curl -s -X DELETE "$BASE_URL/faq/$faq_id" | grep -q '"success"' && log_result "DELETE /faq/<id>" "PASS" || log_result "DELETE /faq/<id>" "FAIL"
fi
# ===== 11. 出题系统 (3个) =====
echo -e "\n--- 11. 出题系统 ---"
test_endpoint "GET /exam/health" "GET" "/exam/health" "" '"status"'
test_endpoint "POST /exam/generate" "POST" "/exam/generate" '{"file_path":"public_kb/三峡公报_1-15页.pdf","collection":"public_kb","question_types":{"single_choice":1}}' '"questions"'
test_endpoint "POST /exam/grade" "POST" "/exam/grade" '{"answers":[{"question_id":"q1","question_type":"single_choice","question_content":{"answer":"A"},"student_answer":"A","max_score":2}]}' '"results"'
# ===== 12. 图片服务 (4个) =====
echo -e "\n--- 12. 图片服务 ---"
test_endpoint "GET /images/list" "GET" "/images/list?limit=5" "" '"images"'
# 获取图片ID测试
image_id=$(curl -s "$BASE_URL/images/list?limit=1" | grep -o '"image_id":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -n "$image_id" ]; then
http_code=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/images/$image_id.jpg")
[ "$http_code" = "200" ] && log_result "GET /images/<id>" "PASS" || log_result "GET /images/<id>" "FAIL"
curl -s "$BASE_URL/images/$image_id/info" | grep -q '"image_id"' && log_result "GET /images/<id>/info" "PASS" || log_result "GET /images/<id>/info" "FAIL"
else
log_result "GET /images/<id>" "FAIL (无图片)"
log_result "GET /images/<id>/info" "FAIL (无图片)"
fi
test_endpoint "GET /images/stats" "GET" "/images/stats" "" '"total_images"'
# ===== 13. 报告服务 (2个) =====
echo -e "\n--- 13. 报告服务 ---"
test_endpoint "GET /reports/weekly" "GET" "/reports/weekly" "" '"report"'
test_endpoint "GET /reports/monthly" "GET" "/reports/monthly" "" '"report"'
# ===== 14. 知识库路由 (1个) =====
echo -e "\n--- 14. 知识库路由 ---"
test_endpoint "POST /kb/route" "POST" "/kb/route" '{"query":"三峡工程"}' '"target_collections"'
# 输出结果
echo -e "\n=========================================="
echo "测试结果汇总"
echo "=========================================="
echo "总计: $PASS 通过, $FAIL 失败"
echo "=========================================="
# 清理
rm -f /tmp/rag.json /tmp/chat.json /tmp/test_upload.txt /tmp/file1.txt /tmp/file2.txt /tmp/update.txt
exit $FAIL