主要更新:
- 新增 src/core/constants.py: 统一管理所有字符串常量(Period, IndicatorType, KlineColumns, IndicatorColumns)
- 统一命名规范: 所有指标类型和列名使用小写格式
- 实现 DataService.from_config() 工厂方法,自动初始化所有依赖组件
- 实现下载数据后自动重新计算指标功能
- 更新所有指标计算器使用 IndicatorColumns 常量定义列名
- 修复 table_schemas.py 表名格式bug (indicators_{period}_{indicator})
- 新增数据库清理脚本,删除旧格式的指标表
- 优化配置文件,调整指标参数
文档:
- docs/constants_guide.md: 常量使用指南
- docs/cache_storage_layout.md: 缓存和存储架构说明
136 lines
3.4 KiB
Python
136 lines
3.4 KiB
Python
"""
|
|
简单测试验证新架构
|
|
|
|
快速验证核心组件是否正常工作
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
import pandas as pd
|
|
import numpy as np
|
|
from datetime import datetime
|
|
|
|
# 添加项目根目录到路径
|
|
project_root = Path(__file__).parent.parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
# 测试导入
|
|
try:
|
|
from src.data_storage.storage_layer import StorageLayer
|
|
from src.cache.cache_manager import CacheManager
|
|
from src.data_service.data_service import DataService
|
|
|
|
print("✓ 所有模块导入成功")
|
|
except ImportError as e:
|
|
print(f"✗ 导入失败: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
# Mock Storage
|
|
class SimpleStorage(StorageLayer):
|
|
def __init__(self):
|
|
self.data = {}
|
|
|
|
def load_klines(self, symbol, period, start_date=None, end_date=None):
|
|
key = f"{symbol}_{period}"
|
|
if key in self.data:
|
|
return self.data[key].copy()
|
|
return None
|
|
|
|
def load_indicators(
|
|
self, symbol, period, indicator_name, start_date=None, end_date=None
|
|
):
|
|
return None
|
|
|
|
def save_klines(self, symbol, period, data):
|
|
self.data[f"{symbol}_{period}"] = data.copy()
|
|
return len(data)
|
|
|
|
def save_indicators(self, symbol, period, indicator_name, data):
|
|
return len(data)
|
|
|
|
def exists(self, symbol, period, start_date, end_date):
|
|
return f"{symbol}_{period}" in self.data
|
|
|
|
def get_date_range(self, symbol, period):
|
|
key = f"{symbol}_{period}"
|
|
if key in self.data:
|
|
df = self.data[key]
|
|
return (df.index.min(), df.index.max())
|
|
return None
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
|
|
def test_basic_flow():
|
|
"""测试基本流程"""
|
|
print("\n" + "=" * 50)
|
|
print("测试基本数据流")
|
|
print("=" * 50)
|
|
|
|
# 1. 创建组件
|
|
config = {
|
|
"cache": {
|
|
"enabled": True,
|
|
"mode": "static",
|
|
"auto_load": True,
|
|
}
|
|
}
|
|
|
|
storage = SimpleStorage()
|
|
|
|
# 准备测试数据
|
|
date_range = pd.date_range(start="2024-01-01", periods=10, freq="1D")
|
|
df = pd.DataFrame(
|
|
{
|
|
"timestamp": date_range,
|
|
"open": [100] * 10,
|
|
"high": [110] * 10,
|
|
"low": [90] * 10,
|
|
"close": [105] * 10,
|
|
"volume": [1000000] * 10,
|
|
},
|
|
index=date_range,
|
|
)
|
|
|
|
storage.save_klines("TEST001", "1d", df)
|
|
print("✓ 测试数据准备完成")
|
|
|
|
# 2. 创建CacheManager
|
|
cache = CacheManager(config)
|
|
print("✓ CacheManager创建成功")
|
|
|
|
# 3. 创建DataService
|
|
service = DataService(config, storage, cache)
|
|
print("✓ DataService创建成功")
|
|
|
|
# 4. 测试获取数据
|
|
klines = service.get_klines("TEST001", "1d")
|
|
assert klines is not None, "获取K线失败"
|
|
assert len(klines) == 10, f"K线数量错误: {len(klines)}"
|
|
print(f"✓ 获取K线成功: {len(klines)} 条记录")
|
|
|
|
# 5. 测试缓存命中
|
|
klines2 = service.get_klines("TEST001", "1d")
|
|
assert klines2 is not None
|
|
print("✓ 缓存命中测试通过")
|
|
|
|
# 6. 测试统计
|
|
stats = service.get_stats()
|
|
print(
|
|
f"✓ 统计信息: hit_rate={stats['cache']['hit_rate']:.1%}, "
|
|
f"auto_loads={stats['cache']['auto_loads']}"
|
|
)
|
|
|
|
# 7. 测试健康检查
|
|
health = service.health_check()
|
|
assert health["overall"], "健康检查失败"
|
|
print("✓ 健康检查通过")
|
|
|
|
print("\n✅ 所有测试通过!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_basic_flow()
|