Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

123 Zeilen
4.2 KiB

  1. #!/bin/bash
  2. set -e
  3. BASE_URL="${1:-http://localhost:3000}"
  4. ADMIN_KEY="${2}"
  5. if [ -z "$ADMIN_KEY" ]; then
  6. echo "Usage: $0 <base_url> <admin_key>"
  7. exit 1
  8. fi
  9. echo "=== 渠道定价增强 - 集成测试 ==="
  10. # ---------- 1. 创建渠道定价(含扩展字段) ----------
  11. echo "--- Test 1: Create with extended ratios ---"
  12. RESP=$(curl -s -X POST "$BASE_URL/api/channel-pricing/" \
  13. -H "Authorization: Bearer $ADMIN_KEY" \
  14. -H "Content-Type: application/json" \
  15. -d '{
  16. "model_name": "claude-3-5-sonnet",
  17. "channel_id": 1,
  18. "quota_type": 0,
  19. "model_ratio": 3.0,
  20. "completion_ratio": 15.0,
  21. "cache_ratio": 0.5,
  22. "cache_creation_ratio": 0.625,
  23. "image_ratio": 1.5,
  24. "audio_ratio": 2.0,
  25. "audio_completion_ratio": 1.8
  26. }')
  27. echo "$RESP" | python3 -m json.tool
  28. CACHE_RATIO=$(echo "$RESP" | python3 -c "import sys,json; print(json.load(sys.stdin)['data']['cache_ratio'])")
  29. if [ "$CACHE_RATIO" = "0.5" ]; then
  30. echo " [PASS] cache_ratio = 0.5"
  31. else
  32. echo " [FAIL] cache_ratio expected 0.5, got $CACHE_RATIO"
  33. fi
  34. # ---------- 2. 查询渠道定价(验证新字段返回) ----------
  35. echo "--- Test 2: Query and verify extended fields ---"
  36. RESP=$(curl -s "$BASE_URL/api/channel-pricing/model/claude-3-5-sonnet" \
  37. -H "Authorization: Bearer $ADMIN_KEY")
  38. echo "$RESP" | python3 -m json.tool
  39. # ---------- 3. 更新渠道定价(修改扩展字段) ----------
  40. echo "--- Test 3: Update extended ratios ---"
  41. RESP=$(curl -s -X POST "$BASE_URL/api/channel-pricing/" \
  42. -H "Authorization: Bearer $ADMIN_KEY" \
  43. -H "Content-Type: application/json" \
  44. -d '{
  45. "model_name": "claude-3-5-sonnet",
  46. "channel_id": 1,
  47. "quota_type": 0,
  48. "model_ratio": 3.0,
  49. "completion_ratio": 15.0,
  50. "cache_ratio": 0.8,
  51. "cache_creation_ratio": 0.0
  52. }')
  53. echo "$RESP" | python3 -m json.tool
  54. # ---------- 4. 验证回退:未设置的字段为 0 ----------
  55. echo "--- Test 4: Verify unset fields = 0 ---"
  56. CACHE_CREATION=$(echo "$RESP" | python3 -c "import sys,json; print(json.load(sys.stdin)['data']['cache_creation_ratio'])")
  57. if [ "$CACHE_CREATION" = "0.0" ]; then
  58. echo " [PASS] cache_creation_ratio = 0 (unset)"
  59. else
  60. echo " [FAIL] cache_creation_ratio expected 0.0, got $CACHE_CREATION"
  61. fi
  62. # ---------- 5. 负值校验 ----------
  63. echo "--- Test 5: Reject negative values ---"
  64. HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$BASE_URL/api/channel-pricing/" \
  65. -H "Authorization: Bearer $ADMIN_KEY" \
  66. -H "Content-Type: application/json" \
  67. -d '{
  68. "model_name": "claude-3-5-sonnet",
  69. "channel_id": 1,
  70. "quota_type": 0,
  71. "model_ratio": 3.0,
  72. "cache_ratio": -1.0
  73. }')
  74. if [ "$HTTP_CODE" = "400" ] || [ "$HTTP_CODE" = "422" ]; then
  75. echo " [PASS] negative value rejected with HTTP $HTTP_CODE"
  76. else
  77. echo " [FAIL] expected 400/422, got HTTP $HTTP_CODE"
  78. fi
  79. # ---------- 6. 用户端查询(带渠道信息 + CASE WHEN 回退) ----------
  80. echo "--- Test 6: User-facing query with global fallback ---"
  81. RESP=$(curl -s "$BASE_URL/api/channel-pricing/model/claude-3-5-sonnet" \
  82. -H "Authorization: Bearer $ADMIN_KEY")
  83. echo "$RESP" | python3 -c "
  84. import sys, json
  85. data = json.load(sys.stdin)
  86. for item in data.get('data', []):
  87. ch = item.get('channel_id', '?')
  88. cr = item.get('cache_ratio', 'N/A')
  89. ccr = item.get('cache_creation_ratio', 'N/A')
  90. ir = item.get('image_ratio', 'N/A')
  91. print(f' channel={ch} cache_ratio={cr} cache_creation_ratio={ccr} image_ratio={ir}')
  92. "
  93. # ---------- 7. 删除 + 验证缓存清除 ----------
  94. echo "--- Test 7: Delete and verify cache cleared ---"
  95. ID=$(curl -s "$BASE_URL/api/channel-pricing/model/claude-3-5-sonnet" \
  96. -H "Authorization: Bearer $ADMIN_KEY" | \
  97. python3 -c "import sys,json; items=json.load(sys.stdin).get('data',[]); print(items[0]['id'] if items else '')")
  98. if [ -n "$ID" ]; then
  99. curl -s -X DELETE "$BASE_URL/api/channel-pricing/$ID" \
  100. -H "Authorization: Bearer $ADMIN_KEY"
  101. echo " Deleted pricing id=$ID"
  102. RESP=$(curl -s "$BASE_URL/api/channel-pricing/model/claude-3-5-sonnet" \
  103. -H "Authorization: Bearer $ADMIN_KEY")
  104. COUNT=$(echo "$RESP" | python3 -c "import sys,json; print(len(json.load(sys.stdin).get('data',[])))")
  105. echo " Remaining pricings for claude-3-5-sonnet: $COUNT"
  106. else
  107. echo " [SKIP] No pricing to delete"
  108. fi
  109. echo "=== 集成测试完成 ==="