diff --git a/src/safeds/data/image/containers/_image.py b/src/safeds/data/image/containers/_image.py index dbe440fe5..cd72d43a7 100644 --- a/src/safeds/data/image/containers/_image.py +++ b/src/safeds/data/image/containers/_image.py @@ -21,13 +21,13 @@ class Image: """ @staticmethod - def from_jpeg_file(path: str) -> Image: + def from_jpeg_file(path: str | Path) -> Image: """ Create an image from a JPEG file. Parameters ---------- - path : str + path : str | Path The path to the JPEG file. Returns @@ -41,13 +41,13 @@ def from_jpeg_file(path: str) -> Image: ) @staticmethod - def from_png_file(path: str) -> Image: + def from_png_file(path: str | Path) -> Image: """ Create an image from a PNG file. Parameters ---------- - path : str + path : str | Path The path to the PNG file. Returns @@ -86,25 +86,25 @@ def format(self) -> ImageFormat: # Conversion # ------------------------------------------------------------------------------------------------------------------ - def to_jpeg_file(self, path: str) -> None: + def to_jpeg_file(self, path: str | Path) -> None: """ Save the image as a JPEG file. Parameters ---------- - path : str + path : str | Path The path to the JPEG file. """ Path(path).parent.mkdir(parents=True, exist_ok=True) self._image.save(path, format="jpeg") - def to_png_file(self, path: str) -> None: + def to_png_file(self, path: str | Path) -> None: """ Save the image as a PNG file. Parameters ---------- - path : str + path : str | Path The path to the PNG file. """ Path(path).parent.mkdir(parents=True, exist_ok=True) diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index cf3a4693a..23767ac2f 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -60,13 +60,13 @@ class Table: # ------------------------------------------------------------------------------------------------------------------ @staticmethod - def from_csv_file(path: str) -> Table: + def from_csv_file(path: str | Path) -> Table: """ Read data from a CSV file into a table. Parameters ---------- - path : str + path : str | Path The path to the CSV file. Returns @@ -87,13 +87,13 @@ def from_csv_file(path: str) -> Table: raise FileNotFoundError(f'File "{path}" does not exist') from exception @staticmethod - def from_json_file(path: str) -> Table: + def from_json_file(path: str | Path) -> Table: """ Read data from a JSON file into a table. Parameters ---------- - path : str + path : str | Path The path to the JSON file. Returns @@ -1200,7 +1200,7 @@ def plot_scatterplot(self, x_column_name: str, y_column_name: str) -> Image: # Conversion # ------------------------------------------------------------------------------------------------------------------ - def to_csv_file(self, path: str) -> None: + def to_csv_file(self, path: str | Path) -> None: """ Write the data from the table into a CSV file. @@ -1209,7 +1209,7 @@ def to_csv_file(self, path: str) -> None: Parameters ---------- - path : str + path : str | Path The path to the output file. """ Path(path).parent.mkdir(parents=True, exist_ok=True) @@ -1217,7 +1217,7 @@ def to_csv_file(self, path: str) -> None: data_to_csv.columns = self._schema.column_names data_to_csv.to_csv(path, index=False) - def to_json_file(self, path: str) -> None: + def to_json_file(self, path: str | Path) -> None: """ Write the data from the table into a JSON file. @@ -1226,7 +1226,7 @@ def to_json_file(self, path: str) -> None: Parameters ---------- - path : str + path : str | Path The path to the output file. """ Path(path).parent.mkdir(parents=True, exist_ok=True) diff --git a/tests/helpers/_resources.py b/tests/helpers/_resources.py index 2eb04de6f..5394cdb8b 100644 --- a/tests/helpers/_resources.py +++ b/tests/helpers/_resources.py @@ -3,13 +3,13 @@ _resources_root = Path(__file__).parent / ".." / "resources" -def resolve_resource_path(resource_path: str) -> str: +def resolve_resource_path(resource_path: str | Path) -> str: """ Resolve a path relative to the `resources` directory to an absolute path. Parameters ---------- - resource_path : str + resource_path : str | Path The path to the resource relative to the `resources` directory. Returns diff --git a/tests/safeds/data/image/containers/test_image.py b/tests/safeds/data/image/containers/test_image.py index 2c0d5b622..ec8cf6ee0 100644 --- a/tests/safeds/data/image/containers/test_image.py +++ b/tests/safeds/data/image/containers/test_image.py @@ -11,16 +11,16 @@ class TestFromJpegFile: @pytest.mark.parametrize( "path", - ["image/white_square.jpg"], + ["image/white_square.jpg", Path("image/white_square.jpg")], ) - def test_should_load_jpeg_file(self, path: str) -> None: + def test_should_load_jpeg_file(self, path: str | Path) -> None: Image.from_jpeg_file(resolve_resource_path(path)) @pytest.mark.parametrize( "path", - ["image/missing_file.jpg"], + ["image/missing_file.jpg", Path("image/missing_file.jpg")], ) - def test_should_raise_if_file_not_found(self, path: str) -> None: + def test_should_raise_if_file_not_found(self, path: str | Path) -> None: with pytest.raises(FileNotFoundError): Image.from_jpeg_file(resolve_resource_path(path)) @@ -28,16 +28,16 @@ def test_should_raise_if_file_not_found(self, path: str) -> None: class TestFromPngFile: @pytest.mark.parametrize( "path", - ["image/white_square.png"], + ["image/white_square.png", Path("image/white_square.png")], ) - def test_should_load_png_file(self, path: str) -> None: + def test_should_load_png_file(self, path: str | Path) -> None: Image.from_png_file(resolve_resource_path(path)) @pytest.mark.parametrize( "path", - ["image/missing_file.png"], + ["image/missing_file.png", Path("image/missing_file.png")], ) - def test_should_raise_if_file_not_found(self, path: str) -> None: + def test_should_raise_if_file_not_found(self, path: str | Path) -> None: with pytest.raises(FileNotFoundError): Image.from_png_file(resolve_resource_path(path)) @@ -59,7 +59,7 @@ class TestToJpegFile: "path", ["image/white_square.jpg"], ) - def test_should_save_jpeg_file(self, path: str) -> None: + def test_should_save_jpeg_file_by_str(self, path: str) -> None: image = Image.from_jpeg_file(resolve_resource_path(path)) with NamedTemporaryFile() as tmp_file: @@ -71,13 +71,29 @@ def test_should_save_jpeg_file(self, path: str) -> None: assert image._image.tobytes() == image_read_back._image.tobytes() + @pytest.mark.parametrize( + "path", + ["image/white_square.jpg"], + ) + def test_should_save_jpeg_file_by_path(self, path: str) -> None: + image = Image.from_jpeg_file(resolve_resource_path(path)) + + with NamedTemporaryFile() as tmp_file: + tmp_file.close() + with Path(tmp_file.name).open("wb") as tmp_write_file: + image.to_jpeg_file(Path(tmp_write_file.name)) + with Path(tmp_file.name).open("rb") as tmp_read_file: + image_read_back = Image.from_jpeg_file(tmp_read_file.name) + + assert image._image.tobytes() == image_read_back._image.tobytes() + class TestToPngFile: @pytest.mark.parametrize( "path", ["image/white_square.png"], ) - def test_should_save_png_file(self, path: str) -> None: + def test_should_save_png_file_by_str(self, path: str) -> None: image = Image.from_png_file(resolve_resource_path(path)) with NamedTemporaryFile() as tmp_file: @@ -89,6 +105,22 @@ def test_should_save_png_file(self, path: str) -> None: assert image._image.tobytes() == image_read_back._image.tobytes() + @pytest.mark.parametrize( + "path", + ["image/white_square.png"], + ) + def test_should_save_png_file_by_path(self, path: str) -> None: + image = Image.from_png_file(resolve_resource_path(path)) + + with NamedTemporaryFile() as tmp_file: + tmp_file.close() + with Path(tmp_file.name).open("wb") as tmp_write_file: + image.to_png_file(Path(tmp_write_file.name)) + with Path(tmp_file.name).open("rb") as tmp_read_file: + image_read_back = Image.from_png_file(tmp_read_file.name) + + assert image._image.tobytes() == image_read_back._image.tobytes() + class TestReprJpeg: @pytest.mark.parametrize( diff --git a/tests/safeds/data/tabular/containers/_table/test_from_csv_file.py b/tests/safeds/data/tabular/containers/_table/test_from_csv_file.py index b1c942465..dee401fd3 100644 --- a/tests/safeds/data/tabular/containers/_table/test_from_csv_file.py +++ b/tests/safeds/data/tabular/containers/_table/test_from_csv_file.py @@ -1,15 +1,25 @@ +from pathlib import Path + import pytest from safeds.data.tabular.containers import Table from tests.helpers import resolve_resource_path -def test_from_csv_file_valid() -> None: - table = Table.from_csv_file(resolve_resource_path("table.csv")) +@pytest.mark.parametrize( + "path", + ["table.csv", Path("table.csv")], +) +def test_from_csv_file_valid(path: str | Path) -> None: + table = Table.from_csv_file(resolve_resource_path(path)) assert table.get_column("A").get_value(0) == 1 assert table.get_column("B").get_value(0) == 2 -def test_from_csv_file_invalid() -> None: +@pytest.mark.parametrize( + "path", + ["test_table_from_csv_file_invalid.csv", Path("test_table_from_csv_file_invalid.csv")], +) +def test_from_csv_file_invalid(path: str | Path) -> None: with pytest.raises(FileNotFoundError): - Table.from_csv_file(resolve_resource_path("test_table_from_csv_file_invalid.csv")) + Table.from_csv_file(resolve_resource_path(path)) diff --git a/tests/safeds/data/tabular/containers/_table/test_from_json_file.py b/tests/safeds/data/tabular/containers/_table/test_from_json_file.py index 6b7416af6..b841aa1e5 100644 --- a/tests/safeds/data/tabular/containers/_table/test_from_json_file.py +++ b/tests/safeds/data/tabular/containers/_table/test_from_json_file.py @@ -1,15 +1,25 @@ +from pathlib import Path + import pytest from safeds.data.tabular.containers import Table from tests.helpers import resolve_resource_path -def test_from_json_file_valid() -> None: - table = Table.from_json_file(resolve_resource_path("table.json")) +@pytest.mark.parametrize( + "path", + ["table.json", Path("table.json")], +) +def test_from_json_file_valid(path: str | Path) -> None: + table = Table.from_json_file(resolve_resource_path(path)) assert table.get_column("A").get_value(0) == 1 assert table.get_column("B").get_value(0) == 2 -def test_from_json_file_invalid() -> None: +@pytest.mark.parametrize( + "path", + ["test_table_from_json_file_invalid.json", Path("test_table_from_json_file_invalid.json")], +) +def test_from_json_file_invalid(path: str | Path) -> None: with pytest.raises(FileNotFoundError): - Table.from_json_file(resolve_resource_path("test_table_from_json_file_invalid.json")) + Table.from_json_file(resolve_resource_path(path)) diff --git a/tests/safeds/data/tabular/containers/_table/test_to_csv_file.py b/tests/safeds/data/tabular/containers/_table/test_to_csv_file.py index 6d7359a94..a6bf96ac3 100644 --- a/tests/safeds/data/tabular/containers/_table/test_to_csv_file.py +++ b/tests/safeds/data/tabular/containers/_table/test_to_csv_file.py @@ -4,7 +4,7 @@ from safeds.data.tabular.containers import Table -def test_to_csv_file() -> None: +def test_to_csv_file_by_str() -> None: table = Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]}) with NamedTemporaryFile() as tmp_table_file: tmp_table_file.close() @@ -13,3 +13,14 @@ def test_to_csv_file() -> None: with Path(tmp_table_file.name).open("r", encoding="utf-8") as tmp_file: table_r = Table.from_csv_file(tmp_file.name) assert table == table_r + + +def test_to_csv_file_by_path() -> None: + table = Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]}) + with NamedTemporaryFile() as tmp_table_file: + tmp_table_file.close() + with Path(tmp_table_file.name).open("w", encoding="utf-8") as tmp_file: + table.to_csv_file(Path(tmp_file.name)) + with Path(tmp_table_file.name).open("r", encoding="utf-8") as tmp_file: + table_r = Table.from_csv_file(Path(tmp_file.name)) + assert table == table_r diff --git a/tests/safeds/data/tabular/containers/_table/test_to_json_file.py b/tests/safeds/data/tabular/containers/_table/test_to_json_file.py index db7a75271..56a76875b 100644 --- a/tests/safeds/data/tabular/containers/_table/test_to_json_file.py +++ b/tests/safeds/data/tabular/containers/_table/test_to_json_file.py @@ -4,7 +4,7 @@ from safeds.data.tabular.containers import Table -def test_to_json_file() -> None: +def test_to_json_file_by_str() -> None: table = Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]}) with NamedTemporaryFile() as tmp_table_file: tmp_table_file.close() @@ -13,3 +13,14 @@ def test_to_json_file() -> None: with Path(tmp_table_file.name).open("r", encoding="utf-8") as tmp_file: table_r = Table.from_json_file(tmp_file.name) assert table == table_r + + +def test_to_json_file_by_path() -> None: + table = Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]}) + with NamedTemporaryFile() as tmp_table_file: + tmp_table_file.close() + with Path(tmp_table_file.name).open("w", encoding="utf-8") as tmp_file: + table.to_json_file(Path(tmp_file.name)) + with Path(tmp_table_file.name).open("r", encoding="utf-8") as tmp_file: + table_r = Table.from_json_file(Path(tmp_file.name)) + assert table == table_r