Files
aitsc/test_add_investment_record.py
2025-10-10 23:39:54 +08:00

157 lines
5.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
测试添加"如何投资"的优化记录
"""
import requests
import json
BASE_URL = "http://localhost:5002"
def test_add_investment_record():
"""测试添加投资相关的优化记录"""
print("🧪 测试添加投资优化记录...")
test_data = {
"original_text": "如何投资",
"optimized_text": """以下是针对不同投资需求的优化提示词,涵盖主要资产类别与策略方向:
【股票投资】
"生成A股沪深300成分股近三年的基本面分析报告需包含以下维度
1. 估值指标PE/PB/PS分位数
2. 财务健康度(资产负债率/现金流覆盖率)
3. 行业景气度(营收复合增长率/毛利率变动)
4. 技术面分析(相对强弱指数/布林带位置)
*重点标注当前估值处于历史30%分位以下的标的"
【基金配置】
"设计平衡型基金投资组合方案股债比例6:4要求
1. 列举3只近5年夏普比率1.2的主动型权益基金
2. 配置2只久期在3-5年的国债ETF
3. 加入15%的黄金ETF作为避险资产
4. 测算该组合在2018-2023年期间的最大回撤率
*需说明美联储加息周期中的调整策略"
【固收产品】
"对比分析当前中美债券市场投资机会:
1. 中国10年期国债收益率与CPI的利差空间
2. 美国高收益企业债违约率预期2024Q2
3. 人民币汇率波动对跨境债券收益的影响系数
4. 提供久期免疫策略的具体操作方案"
【另类投资】
"编制大宗商品CTA策略回测报告2020-2024
1. 能化/黑色/有色三大板块的趋势性指标
2. 跨品种套利年化收益率测算
3. 波动率聚集效应的风控方案
4. 建议仓位管理模型(凯利公式变体)"
【风险提示要素】
所有方案必须包含:
1. 历史波动率与VaR(95%)测算
2. 流动性风险预警指标
3. 跨市场传染效应分析
4. 符合《证券期货投资者适当性管理办法》的风险等级评定
请补充说明您的:
- 投资期限(短期/中期/长期)
- 风险承受等级R1-R5
- 特定行业偏好
- 资金规模区间
我将据此提供定制化方案。""",
"optimization_type": "投资策略",
"industry": "金融",
"profession": "投资顾问",
"tags": ["投资策略", "风险控制", "资产配置"]
}
try:
response = requests.post(
f"{BASE_URL}/api/optimization-history",
json=test_data,
headers={'Content-Type': 'application/json'}
)
if response.status_code == 200:
result = response.json()
if result.get('success'):
new_id = result.get('data', {}).get('id')
print(f" ✅ 投资优化记录添加成功ID: {new_id}")
return True
else:
print(f" ❌ 投资优化记录添加失败: {result.get('message')}")
return False
else:
print(f" ❌ 投资优化记录添加请求失败: {response.status_code}")
print(f" 响应内容: {response.text}")
return False
except Exception as e:
print(f" ❌ 投资优化记录添加异常: {str(e)}")
return False
def test_get_all_records():
"""测试获取所有记录"""
print("\n🧪 测试获取所有优化记录...")
try:
response = requests.get(f"{BASE_URL}/api/optimization-history")
if response.status_code == 200:
result = response.json()
if result.get('success'):
history = result.get('data', {}).get('history', [])
total = result.get('data', {}).get('pagination', {}).get('total', 0)
print(f" ✅ 成功获取 {total} 条记录")
# 查找投资相关的记录
investment_records = [r for r in history if '投资' in r.get('original_text', '') or '投资' in r.get('optimization_type', '')]
if investment_records:
print(f" ✅ 找到 {len(investment_records)} 条投资相关记录")
for record in investment_records:
print(f" - ID: {record['id']}, 类型: {record['optimization_type']}, 时间: {record['created_at']}")
else:
print(" ⚠️ 未找到投资相关记录")
return True
else:
print(f" ❌ 获取记录失败: {result.get('message')}")
return False
else:
print(f" ❌ 获取记录请求失败: {response.status_code}")
return False
except Exception as e:
print(f" ❌ 获取记录异常: {str(e)}")
return False
def main():
"""主测试函数"""
print("🚀 测试投资优化记录功能")
print("=" * 50)
# 测试添加投资记录
add_success = test_add_investment_record()
# 测试获取所有记录
get_success = test_get_all_records()
# 输出结果
print("\n" + "=" * 50)
print("📊 测试结果汇总")
print("=" * 50)
if add_success and get_success:
print("🎉 所有测试通过!投资优化记录功能正常!")
print("\n💡 现在您可以:")
print(" 1. 在优化历史页面看到投资相关的记录")
print(" 2. 搜索和筛选投资类型的记录")
print(" 3. 查看详细的投资策略内容")
else:
print("⚠️ 部分测试失败,请检查相关功能")
return add_success and get_success
if __name__ == '__main__':
success = main()
exit(0 if success else 1)