test_datasource_api.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #!/bin/bash
  2. # ============================================
  3. # 数据源 API 测试脚本
  4. # ============================================
  5. # 测试数据源的 CRUD 和取值功能
  6. # 使用方法: ./test_datasource_api.sh [host] [port]
  7. # 示例: ./test_datasource_api.sh localhost 5232
  8. # ============================================
  9. # 配置参数
  10. HOST=${1:-localhost}
  11. PORT=${2:-5232}
  12. BASE_URL="http://${HOST}:${PORT}"
  13. DATASOURCE_URL="${BASE_URL}/api/v1/datasource"
  14. # 颜色定义
  15. RED='\033[0;31m'
  16. GREEN='\033[0;32m'
  17. YELLOW='\033[1;33m'
  18. BLUE='\033[0;34m'
  19. CYAN='\033[0;36m'
  20. NC='\033[0m' # No Color
  21. # 输出函数
  22. print_header() {
  23. echo -e "\n${BLUE}============================================${NC}"
  24. echo -e "${BLUE}$1${NC}"
  25. echo -e "${BLUE}============================================${NC}"
  26. }
  27. print_step() {
  28. echo -e "\n${CYAN}>>> $1${NC}"
  29. }
  30. print_success() {
  31. echo -e "${GREEN}✓ $1${NC}"
  32. }
  33. print_error() {
  34. echo -e "${RED}✗ $1${NC}"
  35. }
  36. print_info() {
  37. echo -e "${YELLOW}ℹ $1${NC}"
  38. }
  39. # 变量存储
  40. DATASOURCE_ID=""
  41. DATASOURCE_ID_2=""
  42. # ============================================
  43. # 测试函数
  44. # ============================================
  45. test_create_text_datasource() {
  46. print_step "创建文本类型数据源"
  47. RESPONSE=$(curl -s -X POST "${DATASOURCE_URL}" \
  48. -H "Content-Type: application/json" \
  49. -H "X-User-Id: test-user-001" \
  50. -d '{
  51. "name": "项目名称",
  52. "type": "text",
  53. "documentId": "test-doc-001",
  54. "sourceType": "manual",
  55. "valueType": "text",
  56. "aggregateType": "first",
  57. "nodeIds": {
  58. "refs": [
  59. {"type": "graph_node", "id": "node-001"}
  60. ]
  61. },
  62. "metadata": {"description": "测试数据源"}
  63. }')
  64. echo "Response: $RESPONSE"
  65. # 提取 ID
  66. DATASOURCE_ID=$(echo "$RESPONSE" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
  67. if [ -n "$DATASOURCE_ID" ]; then
  68. print_success "创建成功,ID: $DATASOURCE_ID"
  69. else
  70. print_error "创建失败"
  71. return 1
  72. fi
  73. }
  74. test_create_image_datasource() {
  75. print_step "创建图片类型数据源"
  76. RESPONSE=$(curl -s -X POST "${DATASOURCE_URL}" \
  77. -H "Content-Type: application/json" \
  78. -H "X-User-Id: test-user-001" \
  79. -d '{
  80. "name": "公司LOGO",
  81. "type": "image",
  82. "documentId": "test-doc-001",
  83. "sourceType": "file",
  84. "valueType": "image",
  85. "nodeIds": {
  86. "refs": [
  87. {"type": "document_element", "id": "element-001"}
  88. ]
  89. }
  90. }')
  91. echo "Response: $RESPONSE"
  92. DATASOURCE_ID_2=$(echo "$RESPONSE" | grep -o '"id":"[^"]*"' | head -1 | cut -d'"' -f4)
  93. if [ -n "$DATASOURCE_ID_2" ]; then
  94. print_success "创建成功,ID: $DATASOURCE_ID_2"
  95. else
  96. print_error "创建失败"
  97. return 1
  98. fi
  99. }
  100. test_get_datasource() {
  101. print_step "获取数据源详情"
  102. if [ -z "$DATASOURCE_ID" ]; then
  103. print_error "没有可用的数据源ID"
  104. return 1
  105. fi
  106. RESPONSE=$(curl -s -X GET "${DATASOURCE_URL}/${DATASOURCE_ID}")
  107. echo "Response: $RESPONSE"
  108. if echo "$RESPONSE" | grep -q '"code":200'; then
  109. print_success "获取成功"
  110. else
  111. print_error "获取失败"
  112. return 1
  113. fi
  114. }
  115. test_get_by_document() {
  116. print_step "按文档ID查询数据源"
  117. RESPONSE=$(curl -s -X GET "${DATASOURCE_URL}/document/test-doc-001")
  118. echo "Response: $RESPONSE"
  119. if echo "$RESPONSE" | grep -q '"code":200'; then
  120. print_success "查询成功"
  121. else
  122. print_error "查询失败"
  123. return 1
  124. fi
  125. }
  126. test_get_by_user() {
  127. print_step "按用户ID查询数据源"
  128. RESPONSE=$(curl -s -X GET "${DATASOURCE_URL}/user/test-user-001")
  129. echo "Response: $RESPONSE"
  130. if echo "$RESPONSE" | grep -q '"code":200'; then
  131. print_success "查询成功"
  132. else
  133. print_error "查询失败"
  134. return 1
  135. fi
  136. }
  137. test_update_datasource() {
  138. print_step "更新数据源"
  139. if [ -z "$DATASOURCE_ID" ]; then
  140. print_error "没有可用的数据源ID"
  141. return 1
  142. fi
  143. RESPONSE=$(curl -s -X PUT "${DATASOURCE_URL}/${DATASOURCE_ID}" \
  144. -H "Content-Type: application/json" \
  145. -d '{
  146. "name": "项目名称(已更新)",
  147. "aggregateType": "concat",
  148. "separator": ", "
  149. }')
  150. echo "Response: $RESPONSE"
  151. if echo "$RESPONSE" | grep -q '"code":200'; then
  152. print_success "更新成功"
  153. else
  154. print_error "更新失败"
  155. return 1
  156. fi
  157. }
  158. test_update_refs() {
  159. print_step "更新节点引用"
  160. if [ -z "$DATASOURCE_ID" ]; then
  161. print_error "没有可用的数据源ID"
  162. return 1
  163. fi
  164. # 追加模式
  165. RESPONSE=$(curl -s -X PUT "${DATASOURCE_URL}/${DATASOURCE_ID}/refs" \
  166. -H "Content-Type: application/json" \
  167. -d '{
  168. "mode": "append",
  169. "refs": [
  170. {"type": "graph_node", "id": "node-002"},
  171. {"type": "document_element", "id": "element-002"}
  172. ]
  173. }')
  174. echo "Response: $RESPONSE"
  175. if echo "$RESPONSE" | grep -q '"code":200'; then
  176. print_success "追加引用成功"
  177. else
  178. print_error "追加引用失败"
  179. return 1
  180. fi
  181. }
  182. test_get_value() {
  183. print_step "获取数据源值"
  184. if [ -z "$DATASOURCE_ID" ]; then
  185. print_error "没有可用的数据源ID"
  186. return 1
  187. fi
  188. RESPONSE=$(curl -s -X GET "${DATASOURCE_URL}/${DATASOURCE_ID}/value")
  189. echo "Response: $RESPONSE"
  190. if echo "$RESPONSE" | grep -q '"code":200'; then
  191. print_success "获取值成功"
  192. else
  193. print_error "获取值失败"
  194. return 1
  195. fi
  196. }
  197. test_batch_get_value() {
  198. print_step "批量获取数据源值"
  199. if [ -z "$DATASOURCE_ID" ] || [ -z "$DATASOURCE_ID_2" ]; then
  200. print_error "没有足够的数据源ID"
  201. return 1
  202. fi
  203. RESPONSE=$(curl -s -X POST "${DATASOURCE_URL}/batch-value" \
  204. -H "Content-Type: application/json" \
  205. -d "{
  206. \"dataSourceIds\": [\"${DATASOURCE_ID}\", \"${DATASOURCE_ID_2}\"]
  207. }")
  208. echo "Response: $RESPONSE"
  209. if echo "$RESPONSE" | grep -q '"code":200'; then
  210. print_success "批量获取值成功"
  211. else
  212. print_error "批量获取值失败"
  213. return 1
  214. fi
  215. }
  216. test_delete_datasource() {
  217. print_step "删除数据源"
  218. if [ -z "$DATASOURCE_ID" ]; then
  219. print_error "没有可用的数据源ID"
  220. return 1
  221. fi
  222. RESPONSE=$(curl -s -X DELETE "${DATASOURCE_URL}/${DATASOURCE_ID}")
  223. echo "Response: $RESPONSE"
  224. if echo "$RESPONSE" | grep -q '"code":200'; then
  225. print_success "删除成功"
  226. else
  227. print_error "删除失败"
  228. return 1
  229. fi
  230. # 删除第二个
  231. if [ -n "$DATASOURCE_ID_2" ]; then
  232. curl -s -X DELETE "${DATASOURCE_URL}/${DATASOURCE_ID_2}" > /dev/null
  233. print_success "删除第二个数据源"
  234. fi
  235. }
  236. # ============================================
  237. # 主流程
  238. # ============================================
  239. show_help() {
  240. echo "数据源 API 测试脚本"
  241. echo ""
  242. echo "用法: $0 [选项] [host] [port]"
  243. echo ""
  244. echo "选项:"
  245. echo " -h, --help 显示帮助信息"
  246. echo " -a, --all 运行所有测试(默认)"
  247. echo " -c, --create 仅测试创建"
  248. echo " -r, --read 仅测试查询"
  249. echo " -u, --update 仅测试更新"
  250. echo " -v, --value 仅测试取值"
  251. echo " -d, --delete 仅测试删除"
  252. echo ""
  253. echo "示例:"
  254. echo " $0 # 默认测试 localhost:5232"
  255. echo " $0 192.168.1.100 8080 # 指定 host 和 port"
  256. echo " $0 -c # 仅测试创建"
  257. }
  258. run_all_tests() {
  259. print_header "数据源 API 测试开始"
  260. print_info "目标地址: ${BASE_URL}"
  261. echo ""
  262. print_header "1. 创建测试"
  263. test_create_text_datasource
  264. test_create_image_datasource
  265. echo ""
  266. print_header "2. 查询测试"
  267. test_get_datasource
  268. test_get_by_document
  269. test_get_by_user
  270. echo ""
  271. print_header "3. 更新测试"
  272. test_update_datasource
  273. test_update_refs
  274. echo ""
  275. print_header "4. 取值测试"
  276. test_get_value
  277. test_batch_get_value
  278. echo ""
  279. print_header "5. 删除测试"
  280. test_delete_datasource
  281. echo ""
  282. print_header "测试完成"
  283. }
  284. # 解析命令行参数
  285. case "$1" in
  286. -h|--help)
  287. show_help
  288. exit 0
  289. ;;
  290. -a|--all)
  291. shift
  292. HOST=${1:-localhost}
  293. PORT=${2:-5232}
  294. BASE_URL="http://${HOST}:${PORT}"
  295. DATASOURCE_URL="${BASE_URL}/api/v1/datasource"
  296. run_all_tests
  297. ;;
  298. -c|--create)
  299. shift
  300. HOST=${1:-localhost}
  301. PORT=${2:-5232}
  302. BASE_URL="http://${HOST}:${PORT}"
  303. DATASOURCE_URL="${BASE_URL}/api/v1/datasource"
  304. print_header "创建测试"
  305. test_create_text_datasource
  306. test_create_image_datasource
  307. ;;
  308. -v|--value)
  309. shift
  310. HOST=${1:-localhost}
  311. PORT=${2:-5232}
  312. BASE_URL="http://${HOST}:${PORT}"
  313. DATASOURCE_URL="${BASE_URL}/api/v1/datasource"
  314. print_header "取值测试"
  315. test_create_text_datasource
  316. test_get_value
  317. ;;
  318. *)
  319. run_all_tests
  320. ;;
  321. esac