import pandas as pd import pytest @pytest.fixture() def example_data() -> pd.DataFrame: x = list(range(1, 6)) y = [4.1, 6.9, 10.1, 12.9, 15.9] return pd.DataFrame({"A": x, "B": y}) def test_linear_regression(example_data): from conda_helpers import linear_regression from conda_helpers.linear_regression import Regression result = linear_regression(example_data, x="A", y="B") assert isinstance(result, Regression) assert pytest.approx(2.96) == result.coefficient assert pytest.approx(2.96) == result.coeff assert pytest.approx(1.1) == result.intercept assert pytest.approx(0.9996349) == result.score assert pytest.approx(0.9996349) == result.r2 def test_regression_predict(example_data): from conda_helpers import linear_regression regression = linear_regression(example_data, x="A", y="B") prediction = regression.predict(x=10) assert pytest.approx(30.7) == prediction assert pytest.approx(10) == regression.predict(y=prediction) def test_regression_predict_exceptions(example_data): from conda_helpers import linear_regression regression = linear_regression(example_data, x="A", y="B") with pytest.raises(TypeError, match="expects a keyword"): regression.predict() with pytest.raises(TypeError, match="expects one keyword"): regression.predict(x=1, y=2) with pytest.raises(TypeError, match="takes 1 positional argument but"): regression.predict(1) def test_regression_to_dict(example_data): from conda_helpers import linear_regression regression = linear_regression(example_data, x="A", y="B") result = regression.to_dict() assert sorted(result.keys()) == ["coefficient", "intercept", "score"] assert pytest.approx(2.96) == result["coefficient"] assert pytest.approx(1.1) == result["intercept"] assert pytest.approx(0.9996349) == result["score"]