-
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.
- Loading branch information
1 parent
2bf303a
commit e23df8b
Showing
2 changed files
with
37 additions
and
25 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
57 changes: 37 additions & 20 deletions
57
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,29 +1,46 @@ | ||
import pytest | ||
|
||
from safeds.data.tabular.containers import Table, Column | ||
from safeds.data.tabular.exceptions import UnknownColumnNameError | ||
|
||
from tests.helpers import resolve_resource_path | ||
|
||
|
||
def test_keep_only_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_only_columns_order() -> None: | ||
table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) | ||
transformed_table = table.keep_only_columns(["B", "A"]) | ||
assert transformed_table == Table.from_columns( | ||
class TestKeepOnlyColumns: | ||
@pytest.mark.parametrize( | ||
("table", "column_names", "expected"), | ||
[ | ||
Column("B", [2]), | ||
Column("A", [1]) | ||
( | ||
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): | ||
transformed_table = table.keep_only_columns(column_names) | ||
assert transformed_table == expected | ||
|
||
|
||
def test_keep_only_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_raise_if_column_does_no_exist(self): | ||
table = Table.from_columns([Column("A", [1]), Column("B", [2])]) | ||
with pytest.raises(UnknownColumnNameError): | ||
table.keep_only_columns(["C"]) |