test_curl_aquery_data.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #!/bin/bash
  2. # LightRAG aquery_data endpoint test script
  3. # Use curl command to test the new /query/data endpoint and validate the new data format
  4. echo "🚀 LightRAG aquery_data Endpoint Test (New Data Format Validation)"
  5. echo "=================================================="
  6. # Base URL (adjust according to actual deployment)
  7. BASE_URL="http://localhost:9621"
  8. # Color definitions
  9. RED='\033[0;31m'
  10. GREEN='\033[0;32m'
  11. YELLOW='\033[1;33m'
  12. BLUE='\033[0;34m'
  13. NC='\033[0m' # No Color
  14. # Test result statistics
  15. TOTAL_TESTS=0
  16. PASSED_TESTS=0
  17. FAILED_TESTS=0
  18. # Function to validate success response format
  19. validate_success_response() {
  20. local response="$1"
  21. local test_name="$2"
  22. local expected_mode="$3"
  23. echo -e "${BLUE}Validating $test_name response format...${NC}"
  24. # Check if valid JSON
  25. if ! echo "$response" | jq . >/dev/null 2>&1; then
  26. echo -e "${RED}❌ Response is not valid JSON format${NC}"
  27. return 1
  28. fi
  29. # Validate required fields
  30. local status=$(echo "$response" | jq -r '.status // "missing"')
  31. local message=$(echo "$response" | jq -r '.message // "missing"')
  32. local data_exists=$(echo "$response" | jq 'has("data")')
  33. local metadata_exists=$(echo "$response" | jq 'has("metadata")')
  34. echo " Status: $status"
  35. echo " Message: $message"
  36. # Validate data structure
  37. if [[ "$data_exists" == "true" ]]; then
  38. local entities_count=$(echo "$response" | jq '.data.entities | length // 0')
  39. local relationships_count=$(echo "$response" | jq '.data.relationships | length // 0')
  40. local chunks_count=$(echo "$response" | jq '.data.chunks | length // 0')
  41. local references_count=$(echo "$response" | jq '.data.references | length // 0')
  42. echo " Data.entities: $entities_count"
  43. echo " Data.relationships: $relationships_count"
  44. echo " Data.chunks: $chunks_count"
  45. echo " Data.references: $references_count"
  46. else
  47. echo -e "${RED} ❌ Missing 'data' field${NC}"
  48. return 1
  49. fi
  50. # Validate metadata
  51. if [[ "$metadata_exists" == "true" ]]; then
  52. local query_mode=$(echo "$response" | jq -r '.metadata.query_mode // "missing"')
  53. local keywords_exists=$(echo "$response" | jq 'has("metadata") and (.metadata | has("keywords"))')
  54. local processing_info_exists=$(echo "$response" | jq 'has("metadata") and (.metadata | has("processing_info"))')
  55. echo " Metadata.query_mode: $query_mode"
  56. echo " Metadata.keywords: $keywords_exists"
  57. echo " Metadata.processing_info: $processing_info_exists"
  58. # Validate if query mode matches
  59. if [[ "$expected_mode" != "" && "$query_mode" != "$expected_mode" ]]; then
  60. echo -e "${YELLOW} ⚠️ Query mode mismatch: expected '$expected_mode', actual '$query_mode'${NC}"
  61. fi
  62. else
  63. echo -e "${RED} ❌ Missing 'metadata' field${NC}"
  64. return 1
  65. fi
  66. # Validate status
  67. if [[ "$status" == "success" ]]; then
  68. echo -e "${GREEN} ✅ Response format validation passed${NC}"
  69. return 0
  70. else
  71. echo -e "${RED} ❌ Status is not 'success': $status${NC}"
  72. return 1
  73. fi
  74. }
  75. # Function to validate error response format
  76. validate_error_response() {
  77. local response="$1"
  78. local test_name="$2"
  79. echo -e "${BLUE}Validating $test_name response format...${NC}"
  80. # Check if valid JSON
  81. if ! echo "$response" | jq . >/dev/null 2>&1; then
  82. echo -e "${RED}❌ Response is not valid JSON format${NC}"
  83. return 1
  84. fi
  85. # Validate required fields
  86. local status=$(echo "$response" | jq -r '.status // "missing"')
  87. local message=$(echo "$response" | jq -r '.message // "missing"')
  88. local data_exists=$(echo "$response" | jq 'has("data")')
  89. local metadata_exists=$(echo "$response" | jq 'has("metadata")')
  90. echo " Status: $status"
  91. echo " Message: $message"
  92. # Validate basic structure exists
  93. if [[ "$data_exists" != "true" ]]; then
  94. echo -e "${RED} ❌ Missing 'data' field${NC}"
  95. return 1
  96. fi
  97. if [[ "$metadata_exists" != "true" ]]; then
  98. echo -e "${RED} ❌ Missing 'metadata' field${NC}"
  99. return 1
  100. fi
  101. echo " Data: {}"
  102. echo " Metadata: {}"
  103. # Validate status should be failure
  104. if [[ "$status" == "failure" ]]; then
  105. echo -e "${GREEN} ✅ Error response format validation passed${NC}"
  106. return 0
  107. else
  108. echo -e "${RED} ❌ Status is not 'failure': $status${NC}"
  109. return 1
  110. fi
  111. }
  112. # Function to run success test
  113. run_success_test() {
  114. local test_name="$1"
  115. local query_data="$2"
  116. local expected_mode="$3"
  117. local print_json="${4:-false}" # Optional parameter: whether to print JSON response (default: false)
  118. echo ""
  119. echo "=================================="
  120. echo -e "${BLUE}$test_name${NC}"
  121. echo "=================================="
  122. TOTAL_TESTS=$((TOTAL_TESTS + 1))
  123. # Send request
  124. echo "Sending request..."
  125. local response=$(curl -s -X POST "${BASE_URL}/query/data" \
  126. -H "Content-Type: application/json" \
  127. -H "X-API-Key: your-secure-api-key-here-123" \
  128. -d "$query_data")
  129. # Check if curl succeeded
  130. if [[ $? -ne 0 ]]; then
  131. echo -e "${RED}❌ Request failed - cannot connect to server${NC}"
  132. FAILED_TESTS=$((FAILED_TESTS + 1))
  133. return 1
  134. fi
  135. # Print JSON response if requested
  136. if [[ "$print_json" == "true" ]]; then
  137. echo ""
  138. echo "Response JSON:"
  139. echo "$response" | jq '.' 2>/dev/null || echo "$response"
  140. echo ""
  141. fi
  142. # Validate response
  143. if validate_success_response "$response" "$test_name" "$expected_mode"; then
  144. PASSED_TESTS=$((PASSED_TESTS + 1))
  145. echo -e "${GREEN}✅ $test_name test passed${NC}"
  146. else
  147. FAILED_TESTS=$((FAILED_TESTS + 1))
  148. echo -e "${RED}❌ $test_name test failed${NC}"
  149. echo "Raw response:"
  150. echo "$response" | jq '.' 2>/dev/null || echo "$response"
  151. fi
  152. }
  153. # Function to run error test
  154. run_error_test() {
  155. local test_name="$1"
  156. local query_data="$2"
  157. echo ""
  158. echo "=================================="
  159. echo -e "${BLUE}$test_name${NC}"
  160. echo "=================================="
  161. TOTAL_TESTS=$((TOTAL_TESTS + 1))
  162. # Send request
  163. echo "Sending request..."
  164. local response=$(curl -s -X POST "${BASE_URL}/query/data" \
  165. -H "Content-Type: application/json" \
  166. -H "X-API-Key: your-secure-api-key-here-123" \
  167. -d "$query_data")
  168. # Check if curl succeeded
  169. if [[ $? -ne 0 ]]; then
  170. echo -e "${RED}❌ Request failed - cannot connect to server${NC}"
  171. FAILED_TESTS=$((FAILED_TESTS + 1))
  172. return 1
  173. fi
  174. # Validate response
  175. if validate_error_response "$response" "$test_name"; then
  176. PASSED_TESTS=$((PASSED_TESTS + 1))
  177. echo -e "${GREEN}✅ $test_name test passed${NC}"
  178. else
  179. FAILED_TESTS=$((FAILED_TESTS + 1))
  180. echo -e "${RED}❌ $test_name test failed${NC}"
  181. echo "Raw response:"
  182. echo "$response" | jq '.' 2>/dev/null || echo "$response"
  183. fi
  184. }
  185. # Start tests
  186. echo "Starting tests for new /query/data endpoint data format..."
  187. echo ""
  188. # Test 1: Basic query test (mix mode)
  189. run_success_test "1. Basic Query Test (mix mode)" '{
  190. "query": "What is GraphRAG",
  191. "mode": "mix",
  192. "top_k": 5
  193. }' "mix" "true" # Output full JSON
  194. # Test 2: Detailed parameter query test (hybrid mode)
  195. run_success_test "2. Detailed Parameter Query Test (hybrid mode)" '{
  196. "query": "What is GraphRAG",
  197. "mode": "hybrid",
  198. "top_k": 5,
  199. "chunk_top_k": 8,
  200. "max_entity_tokens": 4000,
  201. "max_relation_tokens": 4000,
  202. "max_total_tokens": 16000,
  203. "enable_rerank": true,
  204. "response_type": "Multiple Paragraphs"
  205. }' "hybrid"
  206. # Output test result statistics
  207. echo ""
  208. echo "=================================================="
  209. echo -e "${BLUE}Test Result Statistics${NC}"
  210. echo "=================================================="
  211. echo -e "Total tests: ${BLUE}$TOTAL_TESTS${NC}"
  212. echo -e "Passed tests: ${GREEN}$PASSED_TESTS${NC}"
  213. echo -e "Failed tests: ${RED}$FAILED_TESTS${NC}"
  214. if [[ $FAILED_TESTS -eq 0 ]]; then
  215. echo -e "${GREEN}🎉 All tests passed! New data format adaptation successful!${NC}"
  216. exit 0
  217. else
  218. echo -e "${RED}⚠️ $FAILED_TESTS test(s) failed, please check the issues${NC}"
  219. exit 1
  220. fi
  221. echo ""
  222. echo "💡 Usage Instructions:"
  223. echo "1. Ensure LightRAG API service is running (python -m lightrag.api.lightrag_server)"
  224. echo "2. Adjust BASE_URL as needed"
  225. echo "3. If authentication is required, add -H \"Authorization: Bearer your-token\""
  226. echo "4. Install jq for better JSON formatting output: brew install jq (macOS) or apt install jq (Ubuntu)"
  227. echo "5. Script will automatically validate new data format structure: status, message, data, metadata"