f93621e3fc
- 实现因子表达式注册表(registry.py) - 实现 Alpha158 子集内置因子库(library.py) - 使用 vnpy.alpha ts_ 算子语法(ts_mean, ts_delta, ts_max, ts_min) - 修正列名:close/open/high/low/volume(非 _price 后缀) - TDD 实现:8 测试全部通过 - 支持因子分类过滤(custom/builtin) - 防止重复注册 Co-Authored-By: Claude <noreply@anthropic.com>
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""因子表达式注册表测试."""
|
|
import pytest
|
|
from sanguo_factor.registry import register_factor, get_factor, list_factors, _REGISTRY
|
|
|
|
|
|
def test_register_and_get_factor():
|
|
"""测试注册和获取因子."""
|
|
_REGISTRY.clear()
|
|
register_factor("ma5", "ts_mean(close, 5)", category="trend")
|
|
f = get_factor("ma5")
|
|
assert f["name"] == "ma5"
|
|
assert f["category"] == "trend"
|
|
assert f["expression"] == "ts_mean(close, 5)"
|
|
|
|
|
|
def test_list_factors_by_category():
|
|
"""测试按分类列出因子."""
|
|
_REGISTRY.clear()
|
|
register_factor("ma5", "ts_mean(close, 5)", category="trend")
|
|
register_factor("rsi", "ts_rank(close, 14)", category="momentum")
|
|
assert len(list_factors(category="trend")) == 1
|
|
assert len(list_factors()) == 2
|
|
|
|
|
|
def test_register_duplicate_raises():
|
|
"""测试重复注册抛出异常."""
|
|
_REGISTRY.clear()
|
|
register_factor("ma5", "ts_mean(close, 5)", category="trend")
|
|
with pytest.raises(ValueError, match="因子已存在"):
|
|
register_factor("ma5", "ts_mean(close, 10)", category="trend")
|
|
|
|
|
|
def test_get_nonexistent_factor_returns_none():
|
|
"""测试获取不存在的因子返回 None."""
|
|
_REGISTRY.clear()
|
|
assert get_factor("nonexistent") is None
|
|
|
|
|
|
def test_list_factors_empty_registry():
|
|
"""测试空注册表返回空列表."""
|
|
_REGISTRY.clear()
|
|
assert list_factors() == []
|
|
assert list_factors(category="trend") == []
|