Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve tests for row #906

Merged
merged 18 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ._assertions import (
assert_cell_operation_works,
assert_row_operation_works,
assert_tables_equal,
assert_that_tabular_datasets_are_equal,
)
Expand Down Expand Up @@ -38,6 +39,7 @@

__all__ = [
"assert_cell_operation_works",
"assert_row_operation_works",
"assert_tables_equal",
"assert_that_tabular_datasets_are_equal",
"configure_test_with_device",
Expand Down
22 changes: 22 additions & 0 deletions tests/helpers/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,25 @@ def assert_cell_operation_works(
column = Column("A", [input_value])
transformed_column = column.transform(transformer)
assert transformed_column == Column("A", [expected_value]), f"Expected: {expected_value}\nGot: {transformed_column}"


def assert_row_operation_works(
input_value: Any,
transformer: Callable[[Table], Table],
expected_value: Any,
) -> None:
"""
Assert that a row operation works as expected.

Parameters
----------
input_value:
The value in the input row.
transformer:
The transformer to apply to the rows.
expected_value:
The expected value of the transformed row.
"""
table = Table(input_value)
transformed_table = transformer(table)
assert transformed_table == Table(expected_value), f"Expected: {expected_value}\nGot: {transformed_table}"
Empty file.
19 changes: 19 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_column_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table", "expected"),
[
(Table(), 0),
(Table({"A": [1, 2, 3]}), 1),
],
ids=[
"empty",
"non-empty",
],
)
def test_should_return_the_number_of_columns(table: Table, expected: int) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
assert row.column_count == expected
24 changes: 24 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_column_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table", "expected"),
[
(Table({}), []),
(Table({"A": [1, 2, 3]}), ["A"]),
(
Table({"A": [1, 2, 3], "B": ["A", "A", "Bla"], "C": [True, True, False], "D": [1.0, 2.1, 4.5]}),
["A", "B", "C", "D"],
),
],
ids=[
"empty",
"one-column",
"four-column",
],
)
def test_should_return_the_column_names(table: Table, expected: list[str]) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
assert row.column_names == expected
23 changes: 23 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_contains.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table", "column_name", "expected"),
[
(Table({}), "A", False),
(Table({"A": [1, 2, 3]}), "A", True),
(Table({"A": [1, 2, 3], "B": ["A", "A", "Bla"]}), "C", False),
(Table({"col1": [1, 2, 3], "B": ["A", "A", "Bla"]}), 1, False),
],
ids=[
"empty row",
"column exists",
"column does not exist",
"not a string",
],
)
def test_should_return_whether_the_row_has_the_column(table: Table, column_name: str, expected: bool) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
assert (column_name in row) == expected
58 changes: 58 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_eq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table1", "table2", "expected"),
[
(Table({"col1": []}), Table({"col1": []}), True),
(Table({"col1": [1, 2]}), Table({"col1": [1, 2]}), True),
(Table({"col1": [1, 2]}), Table({"col1": [2, 3]}), False),
(Table({"col1": [1, 2]}), Table({"col2": [1, 2]}), False),
(Table({"col1": ["1", "2"]}), Table({"col1": [1, 2]}), False),
],
ids=[
"empty rows",
"equal rows",
"different values",
"different columns",
"different types",
],
)
def test_should_return_whether_two_rows_are_equal(table1: Table, table2: Table, expected: bool) -> None:
row1: Row[any] = _LazyVectorizedRow(table=table1)
row2: Row[any] = _LazyVectorizedRow(table=table2)
assert (row1.__eq__(row2)) == expected


@pytest.mark.parametrize(
("table", "expected"),
[
(Table({"col1": []}), True),
(Table({"col1": [1, 2]}), True),
],
ids=[
"empty table",
"filled table",
],
)
def test_should_return_true_if_rows_are_strict_equal(table: Table, expected: bool) -> None:
row1: Row[any] = _LazyVectorizedRow(table=table)
assert (row1.__eq__(row1)) == expected


@pytest.mark.parametrize(
("table1", "table2"),
[
(Table({"col1": []}), Table({"col1": []})),
(Table({"col1": [1, 2]}), Table({"col1": [1, 2]})),
],
ids=[
"empty tables",
"filled tables",
],
)
def test_should_return_false_if_object_is_other_type(table1: Table, table2: Table) -> None:
row1: Row[any] = _LazyVectorizedRow(table=table1)
assert (row1.__eq__(table2)) == NotImplemented
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow
from safeds.data.tabular.typing import DataType


@pytest.mark.parametrize(
("table", "column_name", "expected"),
[
(Table({"col1": ["A"]}), "col1", "String"),
(Table({"col1": ["a"], "col2": [1]}), "col2", "Int64"),
],
ids=[
"one column",
"two columns",
],
)
def test_should_return_the_type_of_the_column(table: Table, column_name: str, expected: DataType) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
assert str(row.get_column_type(column_name)) == expected
46 changes: 46 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_get_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import re

import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow
from safeds.exceptions import ColumnNotFoundError

from tests.helpers import assert_row_operation_works


@pytest.mark.parametrize(
("table_data", "column_name", "target", "expected"),
[
({"A": [1, 2]}, "A", 1, {"A": [2]}),
({"A": [1, 2, 3], "B": [4, 5, 2]}, "B", 2, {"A": [1, 2], "B": [4, 5]}),
],
ids=[
"one column",
"two columns",
],
)
def test_should_get_correct_item(table_data: dict, column_name: str, target: int, expected: dict) -> None:
assert_row_operation_works(
table_data,
lambda table: table.remove_rows(lambda row: row.get_value(column_name).eq(target)),
expected,
)


@pytest.mark.parametrize(
("table", "column_name"),
[
(Table(), "A"),
(Table({"A": ["a", "aa", "aaa"]}), "B"),
(Table({"A": ["b", "aa", "aaa"], "C": ["b", "aa", "aaa"]}), "B"),
],
ids=[
"empty table",
"table with one column",
"table with two columns",
],
)
def test_should_raise_column_not_found_error(table: Table, column_name: str) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
with pytest.raises(ColumnNotFoundError, match=re.escape(f"Could not find column(s):\n - '{column_name}'")):
row.get_value(column_name)
46 changes: 46 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_getitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import re

import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow
from safeds.exceptions import ColumnNotFoundError

from tests.helpers import assert_row_operation_works


@pytest.mark.parametrize(
("table_data", "column_name", "target", "expected"),
[
({"A": [1, 2]}, "A", 1, {"A": [2]}),
({"A": [1, 2, 3], "B": [4, 5, 2]}, "B", 2, {"A": [1, 2], "B": [4, 5]}),
],
ids=[
"table one column",
"table two columns",
],
)
def test_should_get_correct_item(table_data: dict, column_name: str, target: int, expected: dict) -> None:
assert_row_operation_works(
table_data,
lambda table: table.remove_rows(lambda row: row[column_name].eq(target)),
expected,
)


@pytest.mark.parametrize(
("table", "column_name"),
[
(Table(), "A"),
(Table({"A": ["a", "aa", "aaa"]}), "B"),
(Table({"A": ["b", "aa", "aaa"], "C": ["b", "aa", "aaa"]}), "B"),
],
ids=[
"empty table",
"table with one column",
"table with two columns",
],
)
def test_should_raise_column_not_found_error(table: Table, column_name: str) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
with pytest.raises(ColumnNotFoundError, match=re.escape(f"Could not find column(s):\n - '{column_name}'")):
row[column_name]
21 changes: 21 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_has_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table", "column_name", "expected"),
[
(Table(), "A", False),
(Table({"A": ["a", "aa", "aaa"]}), "A", True),
(Table({"A": ["a", "aa", "aaa"]}), "B", False),
],
ids=[
"empty table",
"table with existing column_name",
"table with non existing column_name",
],
)
def test_should_have_column_name(table: Table, column_name: str, expected: bool) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
assert row.has_column(column_name) == expected
24 changes: 24 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table1", "table2", "expected"),
[
(Table(), Table({"A": ["a", "aa", "aaa"]}), False),
(Table(), Table(), True),
(Table({"A": ["a", "aa", "aaa"]}), Table({"A": ["a", "aa", "aaa"]}), True),
(Table({"A": ["a", "aa", "aaa"]}), Table({"B": ["a", "aa", "aaa"]}), False),
],
ids=[
"empty and different table",
"same empty tables",
"same tables",
"different tables",
],
)
def test_should_return_consistent_hashes(table1: Table, table2: Table, expected: bool) -> None:
row1: Row[any] = _LazyVectorizedRow(table=table1)
row2: Row[any] = _LazyVectorizedRow(table=table2)
assert (hash(row1) == hash(row2)) == expected
22 changes: 22 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_iter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table", "expected"),
[
(Table(), []),
(Table({"A": ["a", "aa", "aaa"]}), ["A"]),
(Table({"A": ["a", "aa", "aaa"], "B": ["b", "bb", "bbb"], "C": ["c", "cc", "ccc"]}), ["A", "B", "C"]),
],
ids=[
"empty",
"one column",
"three columns",
],
)
def test_should_return_same_list_of_column_name_with_iter(table: Table, expected: list) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
iterable = iter(row)
assert list(iterable) == expected
21 changes: 21 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_len.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table", "expected"),
[
(Table(), 0),
(Table({"A": ["a", "aa", "aaa"]}), 1),
(Table({"A": ["a", "aa", "aaa"], "B": ["b", "bb", "bbb"]}), 2),
],
ids=[
"empty",
"one column",
"two columns",
],
)
def test_should_have_same_length_as_number_of_columns(table: Table, expected: int) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
assert len(row) == expected
21 changes: 21 additions & 0 deletions tests/safeds/data/tabular/containers/_row/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from safeds.data.tabular.containers import Row, Table
from safeds.data.tabular.containers._lazy_vectorized_row import _LazyVectorizedRow


@pytest.mark.parametrize(
("table"),
[
(Table()),
(Table({"A": ["a", "aa", "aaa"]})),
(Table({"A": ["a", "aa", "aaa"], "B": ["b", "bb", "bbb"]})),
],
ids=[
"empty",
"one column",
"two columns",
],
)
def test_should_return_same_schema(table: Table) -> None:
row: Row[any] = _LazyVectorizedRow(table=table)
assert table.schema == row.schema
Loading