Spaces:
Runtime error
Runtime error
File size: 6,289 Bytes
ce06eb4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | """
测试脚本 - 验证 RequirementAgent 和 RecommendationAgent 的工作流
"""
import sys
import os
# 添加项目根目录到路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from agents.requirement_agent import RequirementAgent
from agents.recommendation_agent import RecommendationAgent
from models.indicator import Indicator
def test_requirement_agent_parse():
"""测试 RequirementAgent 的响应解析"""
print("=" * 60)
print("测试 1: RequirementAgent._parse_response()")
print("=" * 60)
agent = RequirementAgent(api_key="test_key")
# 测试1: 正常响应
test_response_1 = '''
```json
{
"business_domain": "电商",
"analysis_purpose": "分析用户购买行为,优化商品推荐策略"
}
```
'''
try:
result = agent._parse_response(test_response_1)
assert "business_domain" in result
assert "analysis_purpose" in result
assert result["business_domain"] == "电商"
assert "分析用户购买行为" in result["analysis_purpose"]
print("✅ 测试1.1 通过:正常解析 JSON 响应")
print(f" 结果: {result}")
except Exception as e:
print(f"❌ 测试1.1 失败: {e}")
return False
# 测试2: 无代码块的响应
test_response_2 = '''
{
"business_domain": "金融",
"analysis_purpose": "监控风险指标"
}
'''
try:
result = agent._parse_response(test_response_2)
assert result["business_domain"] == "金融"
print("✅ 测试1.2 通过:解析无代码块的响应")
except Exception as e:
print(f"❌ 测试1.2 失败: {e}")
return False
# 测试3: 缺少字段时使用默认值
test_response_3 = '''
{
"business_domain": ""
}
'''
try:
result = agent._parse_response(test_response_3)
assert result["business_domain"] == "通用"
assert "分析业务指标" in result["analysis_purpose"]
print("✅ 测试1.3 通过:缺少字段时使用默认值")
except Exception as e:
print(f"❌ 测试1.3 失败: {e}")
return False
print("\n✅ 所有 RequirementAgent 测试通过!\n")
return True
def test_recommendation_agent_run():
"""测试 RecommendationAgent.run() 的输入参数"""
print("=" * 60)
print("测试 2: RecommendationAgent.run()")
print("=" * 60)
agent = RecommendationAgent(api_key="test_key")
# 测试1: 正确输入
input_data = {
"business_domain": "电商",
"analysis_purpose": "分析用户购买行为",
"top_k": 8
}
try:
# 验证输入验证通过
is_valid = agent._validate_input(input_data)
assert is_valid == True
print("✅ 测试2.1 通过:输入验证通过")
except Exception as e:
print(f"❌ 测试2.1 失败: {e}")
return False
# 测试2: 缺少必需字段
input_data_invalid = {
"business_domain": "电商"
# 缺少 analysis_purpose
}
try:
is_valid = agent._validate_input(input_data_invalid)
assert is_valid == False
print("✅ 测试2.2 通过:缺少字段时验证失败")
except Exception as e:
print(f"❌ 测试2.2 失败: {e}")
return False
print("\n✅ 所有 RecommendationAgent 输入验证测试通过!\n")
return True
def test_app_step1_logic():
"""测试 app.py 中 Step 1 的逻辑流程"""
print("=" * 60)
print("测试 3: app.py Step 1 逻辑流程")
print("=" * 60)
# 模拟 Step 1 的输出
mock_req_result = {
"status": "success",
"data": {
"business_domain": "电商",
"analysis_purpose": "分析用户购买行为"
}
}
# 模拟 Step 1 的逻辑
try:
if mock_req_result and mock_req_result.get("status") == "success":
req_data = mock_req_result.get("data", {})
business_domain = req_data.get("business_domain", "通用")
analysis_purpose = req_data.get("analysis_purpose", "")
# 验证提取的字段
assert business_domain == "电商"
assert analysis_purpose == "分析用户购买行为"
print("✅ 测试3.1 通过:成功提取 business_domain 和 analysis_purpose")
# 模拟传递给 RecommendationAgent
rec_input = {
"business_domain": business_domain,
"analysis_purpose": analysis_purpose,
"top_k": 8
}
assert "business_domain" in rec_input
assert "analysis_purpose" in rec_input
print("✅ 测试3.2 通过:成功构建 RecommendationAgent 输入")
else:
print("❌ 测试3 失败:模拟的 RequirementAgent 结果不正确")
return False
except Exception as e:
print(f"❌ 测试3 失败: {e}")
return False
print("\n✅ 所有 app.py Step 1 逻辑测试通过!\n")
return True
def main():
"""运行所有测试"""
print("\n" + "=" * 60)
print("开始测试 Agent 工作流")
print("=" * 60 + "\n")
results = []
# 运行测试
results.append(("RequirementAgent 解析", test_requirement_agent_parse()))
results.append(("RecommendationAgent 输入验证", test_recommendation_agent_run()))
results.append(("app.py Step 1 逻辑", test_app_step1_logic()))
# 汇总结果
print("=" * 60)
print("测试汇总")
print("=" * 60)
all_passed = True
for name, passed in results:
status = "✅ 通过" if passed else "❌ 失败"
print(f"{status}: {name}")
if not passed:
all_passed = False
print("\n" + "=" * 60)
if all_passed:
print("🎉 所有测试通过!Agent 工作流修改正确。")
else:
print("⚠️ 部分测试失败,请检查代码。")
print("=" * 60 + "\n")
return 0 if all_passed else 1
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)
|