Skip to content

Commit

Permalink
Added test case for zero divsion for column_scaler in test_column_sca…
Browse files Browse the repository at this point in the history
…ler.py
  • Loading branch information
sep-he committed Feb 3, 2025
1 parent 66a84be commit e879af2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tests/test_column_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ def one_column_df():
"""Return DataFrame with one column of numeric values. Used for testing."""
return pd.DataFrame({"price": [25, 50, 75]})

@pytest.fixture
def one_column_df_float():
"""Return DataFrame with one column of floating values. Used for testing."""
return pd.DataFrame({"price": [25.0, 50.0, 75.0]})

@pytest.fixture
def single_val_df():
"""Return DataFrame with one column with single repeated value. Used for testing."""
Expand All @@ -26,11 +31,17 @@ def non_numeric_df():

# Expected use case tests
def test_minmax_scaling_default(one_column_df):
"""Test min-max scaling with default new_min=0 and new_max=1."""
"""Test min-max scaling with default new_min=0 and new_max=1. Use float values."""
scaled_df = column_scaler(one_column_df, column="price", method="minmax")
expected = [0.0, 0.5, 1.0]
assert scaled_df["price"].tolist() == expected

def test_minmax_scaling_default_float(one_column_df_float):
"""Test min-max scaling with default new_min=0 and new_max=1."""
scaled_df = column_scaler(one_column_df_float, column="price", method="minmax")
expected = [0.0, 0.5, 1.0]
assert scaled_df["price"].tolist() == expected

def test_minmax_scaling_custom(one_column_df):
"""Test min-max scaling with custom new_min=10 and new_max=20."""
scaled_df = column_scaler(one_column_df, column="price", method="minmax", new_min=10, new_max=20)
Expand Down Expand Up @@ -58,6 +69,15 @@ def test_single_value_column_minmax(single_val_df):
expected = [15.0, 15.0, 15.0]
assert scaled_df["price"].tolist() == expected

def test_single_value_column_standard(single_val_df):
"""Test standard scaling with column with single repeated values to prevent division by zero."""
with pytest.warns(UserWarning,
match="Standard deviation is zero"):
scaled_df = column_scaler(single_val_df, column="price", method="standard")

expected = [0, 0, 0]
assert scaled_df["price"].tolist() == expected

def test_empty_dataframe(empty_df):
"""Test scaling on empty DataFrame."""
with pytest.warns(UserWarning, match="Empty DataFrame detected"):
Expand All @@ -73,6 +93,7 @@ def test_column_with_nan():
expected = [0.0, np.nan, 1.0]
assert np.allclose(scaled_df["price"], expected, equal_nan=True)


# Erroneous case tests

def test_non_numeric_column(non_numeric_df):
Expand Down

0 comments on commit e879af2

Please sign in to comment.