Skip to content

Commit

Permalink
test: use data-driven testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Apr 16, 2023
1 parent 2bf303a commit e23df8b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
5 changes: 0 additions & 5 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,9 @@ def from_columns(columns: list[Column]) -> Table:
Raises
------
MissingDataError
If an empty list is given.
ColumnLengthMismatchError
If any of the column sizes does not match with the others.
"""
if len(columns) == 0:
raise MissingDataError("This function requires at least one column.")

dataframe: DataFrame = pd.DataFrame()

for column in columns:
Expand Down
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"])

0 comments on commit e23df8b

Please sign in to comment.