Skip to content

Commit

Permalink
test: Handle UserWarnings correctly in tests (#417)
Browse files Browse the repository at this point in the history
Closes #399.

### Summary of Changes

* Ignore the `UserWarning` about data being already numerical thrown by
`OneHotEncoder` in `nan` testcase.
* Ignore `UserWarning`s about multiple most common values existing
thrown by `Imputer` in testcases that are not meant to test for
warnings.

---------

Co-authored-by: megalinter-bot <[email protected]>
Co-authored-by: Alexander <[email protected]>
  • Loading branch information
3 people authored Jul 7, 2023
1 parent 1e903d3 commit 388ab2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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
13 changes: 12 additions & 1 deletion tests/safeds/data/tabular/transformation/test_one_hot_encoder.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 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

0 comments on commit 388ab2d

Please sign in to comment.