Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Handle UserWarnings correctly in tests #417

Merged
merged 13 commits into from
Jul 7, 2023
Merged
24 changes: 22 additions & 2 deletions tests/safeds/data/tabular/transformation/test_imputer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import pytest
from safeds.data.tabular.containers import Table
from safeds.data.tabular.transformation import Imputer
Expand Down Expand Up @@ -120,7 +122,16 @@ def test_should_raise_if_column_not_found(self, strategy: ImputerStrategy) -> No
},
)

transformer = Imputer(strategy).fit(table_to_fit, None)
if isinstance(strategy, Imputer.Strategy.Mode):
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message=r"There are multiple most frequent values in a column given to the Imputer\..*",
category=UserWarning,
)
transformer = Imputer(strategy).fit(table_to_fit, None)
else:
transformer = Imputer(strategy).fit(table_to_fit, None)

table_to_transform = Table(
{
Expand Down Expand Up @@ -280,7 +291,16 @@ def test_should_return_transformed_table(
strategy: ImputerStrategy,
expected: Table,
) -> None:
assert Imputer(strategy).fit_and_transform(table, column_names) == expected
if isinstance(strategy, Imputer.Strategy.Mode):
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message=r"There are multiple most frequent values in a column given to the Imputer\..*",
category=UserWarning,
)
assert Imputer(strategy).fit_and_transform(table, column_names) == expected
else:
assert Imputer(strategy).fit_and_transform(table, column_names) == expected

@pytest.mark.parametrize("strategy", strategies(), ids=lambda x: x.__class__.__name__)
def test_should_not_change_original_table(self, strategy: ImputerStrategy) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import pytest
from safeds.data.tabular.containers import Table
from safeds.data.tabular.transformation import OneHotEncoder
Expand Down Expand Up @@ -138,7 +140,16 @@ def test_should_return_false_before_fitting(self) -> None:
)
def test_should_return_true_after_fitting(self, table: Table) -> None:
transformer = OneHotEncoder()
fitted_transformer = transformer.fit(table, None)
with warnings.catch_warnings():
warnings.filterwarnings(
action="ignore",
message=(
r"The columns .+ contain numerical data. The OneHotEncoder is designed to encode non-numerical "
r"values into numerical values"
),
category=UserWarning,
)
fitted_transformer = transformer.fit(table, None)
assert fitted_transformer.is_fitted()


Expand Down