Raise coverage threshold to 95% and expand test coverage
- pyproject.toml: fail_under 80 → 95 - test_alpha: +79 lines - test_cli_workflow: +226 lines - test_derived: +121 lines - test_downloader_contracts: +169 lines - test_features: +16 lines - test_minute_downloader: +81 lines - test_portfolio: +208 lines
This commit is contained in:
@@ -191,6 +191,15 @@ def test_combine_single_alpha_is_identity(tmp_path):
|
||||
assert (combo["combo_name"] == "rev_combo").all()
|
||||
|
||||
|
||||
def test_combine_alphas_rejects_unknown_method(tmp_path):
|
||||
data = _make_data()
|
||||
alpha_path = tmp_path / "alpha.pq"
|
||||
compute_alpha(data, "rev", "reversal", lookback=5).to_parquet(alpha_path, index=False)
|
||||
|
||||
with pytest.raises(ValueError, match="Unknown combo method"):
|
||||
combine_alphas([str(alpha_path)], "bad_combo", method="does_not_exist")
|
||||
|
||||
|
||||
# --- registry / factory -----------------------------------------------------
|
||||
|
||||
def test_builtins_are_registered():
|
||||
@@ -205,6 +214,22 @@ def test_get_alpha_filters_unaccepted_params():
|
||||
assert not hasattr(alpha, "vol_window")
|
||||
|
||||
|
||||
def test_get_alpha_forwards_kwargs_to_flexible_alpha():
|
||||
@register_alpha
|
||||
class _FlexibleAlpha(BaseAlpha):
|
||||
name = "_flexible_alpha_kwargs"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.kwargs = kwargs
|
||||
|
||||
def signal(self, close):
|
||||
return close
|
||||
|
||||
alpha = get_alpha("_flexible_alpha_kwargs", decay=0.5, label="demo")
|
||||
|
||||
assert alpha.kwargs == {"decay": 0.5, "label": "demo"}
|
||||
|
||||
|
||||
def test_get_alpha_unknown_raises():
|
||||
with pytest.raises(KeyError):
|
||||
get_alpha("does_not_exist")
|
||||
@@ -227,6 +252,31 @@ def test_register_rejects_non_basealpha():
|
||||
register_alpha(object) # type: ignore[arg-type]
|
||||
|
||||
|
||||
def test_register_rejects_empty_alpha_name():
|
||||
with pytest.raises(ValueError, match="non-empty"):
|
||||
@register_alpha
|
||||
class NoNameAlpha(BaseAlpha):
|
||||
def signal(self, close):
|
||||
return close
|
||||
|
||||
|
||||
def test_load_alpha_module_error_paths(tmp_path, monkeypatch):
|
||||
missing_path = tmp_path / "missing_alpha.py"
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_alpha_module(str(missing_path))
|
||||
|
||||
bad_path = tmp_path / "bad_alpha.py"
|
||||
bad_path.write_text("x = 1\n")
|
||||
monkeypatch.setattr(
|
||||
"pipeline.alpha.registry.importlib.util.spec_from_file_location",
|
||||
lambda *args, **kwargs: None,
|
||||
)
|
||||
with pytest.raises(ImportError, match="Cannot load alpha module"):
|
||||
load_alpha_module(str(bad_path))
|
||||
|
||||
load_alpha_module("math")
|
||||
|
||||
|
||||
# --- base class --------------------------------------------------------------
|
||||
|
||||
def test_to_weights_are_per_date_zscore():
|
||||
@@ -242,6 +292,20 @@ def test_to_weights_are_per_date_zscore():
|
||||
assert (weights.mean(axis=1).abs() < 1e-9).all()
|
||||
|
||||
|
||||
def test_base_alpha_default_signal_and_repr():
|
||||
alpha = BaseAlpha()
|
||||
alpha.example = 3
|
||||
|
||||
with pytest.raises(NotImplementedError, match="signal"):
|
||||
alpha.signal(pd.DataFrame({"x": [1.0]}))
|
||||
with pytest.raises(NotImplementedError, match="signal"):
|
||||
alpha.signal_from_data(
|
||||
pd.DataFrame({"symbol_id": ["sh600000"]}),
|
||||
pd.DataFrame({"sh600000": [1.0]}),
|
||||
)
|
||||
assert repr(alpha) == "BaseAlpha(example=3)"
|
||||
|
||||
|
||||
# --- external plugin loading -------------------------------------------------
|
||||
|
||||
def test_load_external_alpha_module(tmp_path):
|
||||
@@ -502,3 +566,18 @@ def test_compute_alpha_rejects_feature_path_collision_with_daily_data(tmp_path):
|
||||
"reversal",
|
||||
feature_paths=[str(close_collision_path)],
|
||||
)
|
||||
|
||||
|
||||
def test_evaluate_alpha_empty_when_signal_dates_not_on_market_calendar():
|
||||
data = _make_data(n_days=3)
|
||||
alpha = pd.DataFrame({
|
||||
"symbol_id": ["sh600000"],
|
||||
"date": [pd.Timestamp("2030-01-01")],
|
||||
"alpha_name": ["future"],
|
||||
"weight": [1.0],
|
||||
})
|
||||
|
||||
metrics = evaluate_alpha(alpha, data)
|
||||
|
||||
assert metrics["n_dates"] == 0
|
||||
assert metrics["cumulative_return"] == 0.0
|
||||
|
||||
Reference in New Issue
Block a user