-
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.
fix:
table.keep_only_columns
now maps column names to correct data (#…
…194) Closes #115 . ### Summary of Changes Fixed `table.keep_only_columns` to rearrange the column names correctly Fixed grammar in two docs --------- Co-authored-by: Lars Reimann <[email protected]> Co-authored-by: megalinter-bot <[email protected]>
- Loading branch information
1 parent
da928ea
commit 459ab75
Showing
3 changed files
with
43 additions
and
28 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
53 changes: 40 additions & 13 deletions
53
tests/safeds/data/tabular/containers/_table/test_keep_only_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,18 +1,45 @@ | ||
import pytest | ||
from safeds.data.tabular.containers import Table | ||
from safeds.data.tabular.containers import Column, Table | ||
from safeds.data.tabular.exceptions import UnknownColumnNameError | ||
|
||
from tests.helpers import resolve_resource_path | ||
|
||
class TestKeepOnlyColumns: | ||
@pytest.mark.parametrize( | ||
("table", "column_names", "expected"), | ||
[ | ||
( | ||
Table.from_columns([Column("A", [1]), Column("B", [2])]), | ||
[], | ||
Table.from_columns([]), | ||
), | ||
( | ||
Table.from_columns([Column("A", [1]), Column("B", [2])]), | ||
["A"], | ||
Table.from_columns([Column("A", [1])]), | ||
), | ||
( | ||
Table.from_columns([Column("A", [1]), Column("B", [2])]), | ||
["B"], | ||
Table.from_columns([Column("B", [2])]), | ||
), | ||
( | ||
Table.from_columns([Column("A", [1]), Column("B", [2])]), | ||
["A", "B"], | ||
Table.from_columns([Column("A", [1]), Column("B", [2])]), | ||
), | ||
# Related to https://github.com/Safe-DS/Stdlib/issues/115 | ||
( | ||
Table.from_columns([Column("A", [1]), Column("B", [2]), Column("C", [3])]), | ||
["C", "A"], | ||
Table.from_columns([Column("C", [3]), Column("A", [1])]), | ||
), | ||
], | ||
) | ||
def test_should_keep_only_listed_columns(self, table: Table, column_names: list[str], expected: Table) -> None: | ||
transformed_table = table.keep_only_columns(column_names) | ||
assert transformed_table == expected | ||
|
||
def test_keep_columns() -> None: | ||
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) | ||
transformed_table = table.keep_only_columns(["A"]) | ||
assert transformed_table.schema.has_column("A") | ||
assert not transformed_table.schema.has_column("B") | ||
|
||
|
||
def test_keep_columns_warning() -> None: | ||
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) | ||
with pytest.raises(UnknownColumnNameError): | ||
table.keep_only_columns(["C"]) | ||
def test_should_raise_if_column_does_no_exist(self) -> None: | ||
table = Table.from_columns([Column("A", [1]), Column("B", [2])]) | ||
with pytest.raises(UnknownColumnNameError): | ||
table.keep_only_columns(["C"]) |