"""测试股票K线接口返回的字段""" import requests import json # API 配置 BASE_URL = "http://localhost:8080/v1" API_KEY = "demo-api-key-2024" # 测试获取股票K线 def test_stock_klines(): """测试股票K线接口返回的字段""" url = f"{BASE_URL}/stock/klines/000001.SZ" headers = {"X-API-Key": API_KEY} params = { "start": "20260301", "end": "20260310", "freq": "1d" } print(f"\n{'='*60}") print(f"测试接口: GET {url}") print(f"{'='*60}") try: response = requests.get(url, headers=headers, params=params) data = response.json() if data.get("code") == 0: kline_data = data.get("data", {}) items = kline_data.get("items", []) print(f"\n标的: {kline_data.get('symbol')}") print(f"周期: {kline_data.get('freq')}") print(f"数据条数: {len(items)}") print(f"\n{'='*60}") if items: # 显示第一条数据的完整字段 first_item = items[0] print("\n第一条数据详情:") print(f"{'-'*60}") # 基础字段 print(f"时间戳: {first_item.get('time')}") print(f"开盘价: {first_item.get('open')}") print(f"最高价: {first_item.get('high')}") print(f"最低价: {first_item.get('low')}") print(f"收盘价: {first_item.get('close')}") print(f"成交量: {first_item.get('volume')}") print(f"成交额: {first_item.get('amount')}") # 扩展字段 print(f"\n扩展字段:") print(f" 交易日: {first_item.get('trade_date')}") print(f" 是否涨停: {first_item.get('is_limit_up')}") print(f" 是否跌停: {first_item.get('is_limit_down')}") print(f" 总市值: {first_item.get('total_market_cap')}") print(f" 流通市值: {first_item.get('float_market_cap')}") print(f" 机构持仓占比: {first_item.get('inst_holding_ratio')}") print(f" 可交易日数: {first_item.get('trading_days')}") print(f" 创建时间: {first_item.get('created_at')}") # 验证所有字段是否存在 expected_fields = [ 'symbol', 'time', 'open', 'high', 'low', 'close', 'volume', 'amount', 'trade_date', 'is_limit_up', 'is_limit_down', 'total_market_cap', 'float_market_cap', 'inst_holding_ratio', 'trading_days', 'created_at' ] print(f"\n{'='*60}") print("字段完整性检查:") print(f"{'-'*60}") missing_fields = [] for field in expected_fields: if field in first_item: print(f" ✓ {field}") else: print(f" ✗ {field} (缺失)") missing_fields.append(field) if missing_fields: print(f"\n缺失字段: {', '.join(missing_fields)}") else: print(f"\n所有字段都存在!") return True else: print("没有获取到数据") return False else: print(f"请求失败: {data.get('message')}") return False except Exception as e: print(f"请求异常: {e}") return False if __name__ == "__main__": print("\n" + "="*60) print("股票K线接口字段测试") print("="*60) success = test_stock_klines() print(f"\n{'='*60}") if success: print("测试完成!") else: print("测试失败!") print("="*60 + "\n")