|
| 1 | +"""Correctness/quality nits roundup (#541).""" |
| 2 | + |
| 3 | +import contextlib |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | + |
| 8 | +from sktime_mcp.runtime.executor import get_executor |
| 9 | +from sktime_mcp.runtime.handles import get_handle_manager |
| 10 | +from sktime_mcp.tools.fit_predict import fit_tool, predict_tool |
| 11 | +from sktime_mcp.tools.inspect_data import inspect_data_tool |
| 12 | +from sktime_mcp.tools.instantiate import instantiate_tool, release_handle_tool |
| 13 | +from sktime_mcp.tools.plotting import plot_series_tool |
| 14 | + |
| 15 | + |
| 16 | +def _release(h): |
| 17 | + with contextlib.suppress(KeyError): |
| 18 | + get_handle_manager().release_handle(h) |
| 19 | + |
| 20 | + |
| 21 | +def test_release_handle_failure_has_error_key(): |
| 22 | + # NB-02: failure must carry an "error" key like other tools |
| 23 | + res = release_handle_tool("est_never_existed") |
| 24 | + assert res["success"] is False |
| 25 | + assert "error" in res |
| 26 | + assert "not found" in res["error"].lower() |
| 27 | + |
| 28 | + |
| 29 | +def test_classifier_predict_omits_horizon(): |
| 30 | + # N-01: horizon is meaningless for classifiers |
| 31 | + h = instantiate_tool(spec="KNeighborsTimeSeriesClassifier()")["handle"] |
| 32 | + try: |
| 33 | + fit_tool(estimator_handle=h, X_dataset="arrow_head", y_dataset="arrow_head") |
| 34 | + res = predict_tool(estimator_handle=h, X_dataset="arrow_head") |
| 35 | + assert res["success"], res |
| 36 | + assert "horizon" not in res |
| 37 | + finally: |
| 38 | + _release(h) |
| 39 | + |
| 40 | + |
| 41 | +def test_forecaster_predict_keeps_horizon(): |
| 42 | + h = instantiate_tool(spec="NaiveForecaster(sp=12)")["handle"] |
| 43 | + try: |
| 44 | + fit_tool(estimator_handle=h, y_dataset="airline") |
| 45 | + res = predict_tool(estimator_handle=h, horizon=6) |
| 46 | + assert res["horizon"] == 6 |
| 47 | + finally: |
| 48 | + _release(h) |
| 49 | + |
| 50 | + |
| 51 | +def test_format_reports_sorting(): |
| 52 | + # NB-08: sorting is surfaced in changes |
| 53 | + ex = get_executor() |
| 54 | + ex._data_handles["nit_unsorted"] = { |
| 55 | + "y": pd.Series( |
| 56 | + [3.0, 1.0, 2.0], |
| 57 | + index=pd.to_datetime(["2024-03-01", "2024-01-01", "2024-02-01"]), |
| 58 | + ), |
| 59 | + "X": None, |
| 60 | + "metadata": {"frequency": None}, |
| 61 | + "validation": {}, |
| 62 | + "config": {}, |
| 63 | + } |
| 64 | + try: |
| 65 | + res = ex.format_data_handle("nit_unsorted", release_original=False) |
| 66 | + assert res["success"] |
| 67 | + assert res["changes_made"].get("sorted") is True |
| 68 | + finally: |
| 69 | + for h in list(ex._data_handles): |
| 70 | + if h == "nit_unsorted" or h == res.get("data_handle"): |
| 71 | + ex._data_handles.pop(h, None) |
| 72 | + |
| 73 | + |
| 74 | +def test_inspect_includes_exog_dtypes(): |
| 75 | + # N-14: dtypes must include exog columns that `columns` lists |
| 76 | + ex = get_executor() |
| 77 | + idx = pd.period_range("2020-01", periods=12, freq="M") |
| 78 | + ex._data_handles["nit_exog"] = { |
| 79 | + "y": pd.Series(range(12), index=idx, name="target", dtype=float), |
| 80 | + "X": pd.DataFrame({"temp": range(12)}, index=idx), |
| 81 | + "metadata": {}, |
| 82 | + "validation": {}, |
| 83 | + "config": {}, |
| 84 | + } |
| 85 | + try: |
| 86 | + res = inspect_data_tool(data_handle="nit_exog") |
| 87 | + assert res["success"], res |
| 88 | + assert "X:temp" in res["dtypes"] |
| 89 | + assert "X:temp" in res["columns"] |
| 90 | + finally: |
| 91 | + ex._data_handles.pop("nit_exog", None) |
| 92 | + |
| 93 | + |
| 94 | +def test_plot_rejects_nonpositive_dpi(): |
| 95 | + # N-20: dpi=0 must be rejected, not silently defaulted |
| 96 | + ex = get_executor() |
| 97 | + idx = pd.period_range("2020-01", periods=12, freq="M") |
| 98 | + ex._data_handles["nit_plot"] = {"y": pd.Series(range(12), index=idx)} |
| 99 | + try: |
| 100 | + res = plot_series_tool(data_handles=["nit_plot"], dpi=0) |
| 101 | + assert not res["success"] |
| 102 | + assert "dpi" in res["error"].lower() |
| 103 | + finally: |
| 104 | + ex._data_handles.pop("nit_plot", None) |
0 commit comments