-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: raise if
remove_colums
is called with unknown column by defau…
…lt (#852) Closes #807 ### Summary of Changes - Added an optional, keyword-only parameter ignore_unknown_names: bool = False to the remove_columns method. - This parameter controls whether an error is raised when attempting to remove non-existent columns. - If ignore_unknown_names is set to False, the method checks for the existence of specified columns using the - ---- - If ignore_unknown_names is set to True, non-existent columns are ignored, and no error is raised. --------- Co-authored-by: Sardar <[email protected]> Co-authored-by: megalinter-bot <[email protected]> Co-authored-by: Tim Locke <[email protected]> Co-authored-by: Lars Reimann <[email protected]>
- Loading branch information
1 parent
9880fe0
commit 8f78163
Showing
7 changed files
with
65 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 50 additions & 9 deletions
59
tests/safeds/data/tabular/containers/_table/test_remove_columns.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,67 @@ | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.exceptions import ColumnNotFoundError | ||
|
||
|
||
# Test cases where no exception is expected | ||
@pytest.mark.parametrize( | ||
("table", "expected", "columns"), | ||
("table", "expected", "columns", "ignore_unknown_names"), | ||
[ | ||
(Table({"col1": [1, 2, 1], "col2": ["a", "b", "c"]}), Table({"col1": [1, 2, 1]}), ["col2"]), | ||
(Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), Table(), ["col1", "col2"]), | ||
(Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), []), | ||
(Table(), Table(), []), | ||
(Table(), Table(), ["col1"]), | ||
(Table({"col1": [1, 2, 1], "col2": ["a", "b", "c"]}), Table({"col1": [1, 2, 1]}), ["col2"], True), | ||
(Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), Table(), ["col1", "col2"], True), | ||
(Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), [], True), | ||
(Table(), Table(), [], True), | ||
(Table(), Table(), ["col1"], True), | ||
(Table({"col1": [1, 2, 1], "col2": ["a", "b", "c"]}), Table({"col1": [1, 2, 1]}), ["col2"], False), | ||
(Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), Table(), ["col1", "col2"], False), | ||
( | ||
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), | ||
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), | ||
[], | ||
False, | ||
), | ||
(Table(), Table(), [], False), | ||
], | ||
ids=[ | ||
"one column, ignore unknown names", | ||
"multiple columns, ignore unknown names", | ||
"no columns, ignore unknown names", | ||
"empty, ignore unknown names", | ||
"missing columns, ignore unknown names", | ||
"one column", | ||
"multiple columns", | ||
"no columns", | ||
"empty", | ||
"missing columns", | ||
], | ||
) | ||
def test_should_remove_table_columns(table: Table, expected: Table, columns: list[str]) -> None: | ||
table = table.remove_columns(columns) | ||
def test_should_remove_table_columns_no_exception( | ||
table: Table, | ||
expected: Table, | ||
columns: list[str], | ||
ignore_unknown_names: bool, | ||
) -> None: | ||
table = table.remove_columns(columns, ignore_unknown_names=ignore_unknown_names) | ||
assert table.schema == expected.schema | ||
assert table == expected | ||
assert table.row_count == expected.row_count | ||
|
||
|
||
# Test cases where an exception is expected | ||
@pytest.mark.parametrize( | ||
("table", "columns", "ignore_unknown_names"), | ||
[ | ||
(Table(), ["col1"], False), | ||
(Table(), ["col12"], False), | ||
], | ||
ids=[ | ||
"missing columns", | ||
"missing columns", | ||
], | ||
) | ||
def test_should_raise_error_for_unknown_columns( | ||
table: Table, | ||
columns: list[str], | ||
ignore_unknown_names: bool, | ||
) -> None: | ||
with pytest.raises(ColumnNotFoundError): | ||
table.remove_columns(columns, ignore_unknown_names=ignore_unknown_names) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters