2379159818
- 新增 alpha_lab.py: AlphaLabSession 类(lazy import vnpy.alpha) - 新增 analyzer.py: run_factor_analysis + FactorReport(lazy import alphalens) - 测试: test_alpha_lab.py (2 passed) + test_analyzer.py (3 passed) - 策略: 本地无 alphalens/polars,函数内 lazy import 避免 ImportError - 骨架: run_factor_analysis 返回 FactorReport,alphalens 集成待完整实现
88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
"""Test analyzer module - Factor analysis with alphalens."""
|
|
import sys
|
|
import os
|
|
_VNPY_SRC = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "vnpy_v4.4.0"))
|
|
sys.path.insert(0, _VNPY_SRC)
|
|
|
|
from unittest.mock import Mock, patch, MagicMock
|
|
import tempfile
|
|
|
|
|
|
def test_run_factor_analysis_returns_report():
|
|
"""Test that run_factor_analysis returns FactorReport with correct structure."""
|
|
from pathlib import Path
|
|
from sanguo_factor.analyzer import run_factor_analysis, FactorReport
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
output_dir = str(Path(tmpdir) / "factor_analysis")
|
|
|
|
# Patch imports to avoid ImportError when alphalens missing
|
|
with patch("sanguo_factor.alpha_lab.AlphaLabSession") as MockSession, \
|
|
patch.dict("sys.modules", {"alphalens.utils": Mock(), "alphalens.tears": Mock()}):
|
|
# Mock the AlphaLabSession
|
|
mock_session_instance = Mock()
|
|
MockSession.return_value = mock_session_instance
|
|
|
|
# Mock get_factor to avoid registry call
|
|
with patch("sanguo_factor.registry.get_factor", return_value={"expression": "ts_mean(close, 5)"}):
|
|
# Run the analysis
|
|
result = run_factor_analysis(
|
|
symbols=["600000"],
|
|
factor_names=["ma5"],
|
|
start="2024-01-01",
|
|
end="2024-06-30",
|
|
cfg=Mock(),
|
|
output_dir=output_dir
|
|
)
|
|
|
|
# Verify the result is a FactorReport with expected structure
|
|
assert isinstance(result, FactorReport)
|
|
assert result.factor_names == ["ma5"]
|
|
assert result.output_dir == output_dir
|
|
assert isinstance(result.ic_summary, dict)
|
|
|
|
|
|
def test_run_factor_analysis_calls_load_symbols():
|
|
"""Test that run_factor_analysis handles missing alphalens gracefully."""
|
|
from pathlib import Path
|
|
from sanguo_factor.analyzer import run_factor_analysis
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
output_dir = str(Path(tmpdir) / "factor_analysis")
|
|
|
|
# alphalens missing - should return skeleton report with error
|
|
result = run_factor_analysis(
|
|
symbols=["600000", "000001"],
|
|
factor_names=["ma5"],
|
|
start="2024-01-01",
|
|
end="2024-06-30",
|
|
cfg=Mock(),
|
|
output_dir=output_dir
|
|
)
|
|
|
|
# Verify skeleton report returned
|
|
assert "error" in result.ic_summary
|
|
|
|
|
|
def test_run_factor_analysis_adds_features():
|
|
"""Test that run_factor_analysis returns correct structure when alphalens missing."""
|
|
from pathlib import Path
|
|
from sanguo_factor.analyzer import run_factor_analysis
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
output_dir = str(Path(tmpdir) / "factor_analysis")
|
|
|
|
# alphalens missing - verify structure
|
|
result = run_factor_analysis(
|
|
symbols=["600000"],
|
|
factor_names=["ma5"],
|
|
start="2024-01-01",
|
|
end="2024-06-30",
|
|
cfg=Mock(),
|
|
output_dir=output_dir
|
|
)
|
|
|
|
# Verify factor_names preserved even when alphalens missing
|
|
assert result.factor_names == ["ma5"]
|
|
assert result.output_dir == output_dir
|