Skip to content

Commit

Permalink
test: Added test for saving images
Browse files Browse the repository at this point in the history
feat: Added IllegalFormatError
  • Loading branch information
Marsmaennchen221 committed Dec 8, 2023
1 parent 4c6bcc1 commit 2506d5c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from torchvision.transforms.v2 import functional as func2
from torchvision.utils import save_image

from safeds.exceptions import ClosedBound, OutOfBoundsError
from safeds.exceptions import ClosedBound, OutOfBoundsError, IllegalFormatError


class Image:
Expand Down
2 changes: 2 additions & 0 deletions src/safeds/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ColumnLengthMismatchError,
ColumnSizeError,
DuplicateColumnNameError,
IllegalFormatError,
IllegalSchemaModificationError,
IndexOutOfBoundsError,
MissingValuesColumnError,
Expand Down Expand Up @@ -38,6 +39,7 @@
"ColumnLengthMismatchError",
"ColumnSizeError",
"DuplicateColumnNameError",
"IllegalFormatError",
"IllegalSchemaModificationError",
"IndexOutOfBoundsError",
"MissingValuesColumnError",
Expand Down
7 changes: 7 additions & 0 deletions src/safeds/exceptions/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,10 @@ class ColumnIsTargetError(IllegalSchemaModificationError):

def __init__(self, column_name: str) -> None:
super().__init__(f'Column "{column_name}" is the target column and cannot be removed.')


class IllegalFormatError(Exception):
"""Exception raised when a format is not legal."""

def __init__(self, formats: list[str] | str) -> None:
super().__init__(f"This format is illegal. Use one of the following formats: {formats}")
59 changes: 57 additions & 2 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from pathlib import Path
from tempfile import NamedTemporaryFile

import pytest
import torch
from safeds.data.image.containers import Image
from safeds.data.tabular.containers import Table
from safeds.exceptions import OutOfBoundsError
from safeds.exceptions import OutOfBoundsError, IllegalFormatError
from syrupy import SnapshotAssertion
from torch.types import Device

Expand Down Expand Up @@ -69,7 +70,7 @@ class TestFromBytes:
def test_should_write_and_load_bytes_jpeg(self, resource_path: str | Path, device: Device) -> None:
_skip_if_device_not_available(device)
image = Image.from_file(resolve_resource_path(resource_path), device)
image_copy = Image.from_bytes(image._repr_jpeg_(), device)
image_copy = Image.from_bytes(bytes(image._repr_jpeg_()), device)
assert image == image_copy

@pytest.mark.parametrize(
Expand Down Expand Up @@ -123,6 +124,60 @@ def test_should_return_bytes(self, resource_path: str | Path, device: Device) ->
assert isinstance(image._repr_png_(), bytes)


@pytest.mark.parametrize("device", _test_devices(), ids=_test_devices_ids())
class TestToJpegFile:
@pytest.mark.parametrize(
"resource_path",
["image/white_square.jpg", "image/white_square.png"],
ids=["white_square-jpg", "white_square-png"],
)
def test_should_save_file(self, resource_path: str | Path, device: Device) -> None:
_skip_if_device_not_available(device)
image = Image.from_file(resolve_resource_path(resource_path), device)
with NamedTemporaryFile(suffix=".jpg") as tmp_jpeg_file:
tmp_jpeg_file.close()
with Path(tmp_jpeg_file.name).open("w", encoding="utf-8") as tmp_file:
image.to_jpeg_file(tmp_file.name)
with Path(tmp_jpeg_file.name).open("r", encoding="utf-8") as tmp_file:
image_r = Image.from_file(tmp_file.name)
assert image == image_r

@pytest.mark.parametrize(
"resource_path",
[
"image/plane.png",
"image/rgba.png",
],
ids=["plane-png", "rgba-png"],
)
def test_should_raise_if_image_has_alpha_channel(self, resource_path: str | Path, device: Device) -> None:
_skip_if_device_not_available(device)
image = Image.from_file(resolve_resource_path(resource_path), device)
with NamedTemporaryFile(suffix=".jpg") as tmp_jpeg_file:
tmp_jpeg_file.close()
with Path(tmp_jpeg_file.name).open("w", encoding="utf-8") as tmp_file, pytest.raises(IllegalFormatError, match=r"This format is illegal. Use one of the following formats: png"):
image.to_jpeg_file(tmp_file.name)


@pytest.mark.parametrize("device", _test_devices(), ids=_test_devices_ids())
class TestToPngFile:
@pytest.mark.parametrize(
"resource_path",
["image/plane.jpg", "image/plane.png", "image/rgba.png", "image/white_square.jpg", "image/white_square.png"],
ids=["plane-jpg", "plane-png", "rgba-png", "white_square-jpg", "white_square-png"],
)
def test_should_save_file(self, resource_path: str | Path, device: Device) -> None:
_skip_if_device_not_available(device)
image = Image.from_file(resolve_resource_path(resource_path), device)
with NamedTemporaryFile(suffix=".png") as tmp_png_file:
tmp_png_file.close()
with Path(tmp_png_file.name).open("w", encoding="utf-8") as tmp_file:
image.to_png_file(tmp_file.name)
with Path(tmp_png_file.name).open("r", encoding="utf-8") as tmp_file:
image_r = Image.from_file(tmp_file.name)
assert image == image_r


@pytest.mark.parametrize("device", _test_devices(), ids=_test_devices_ids())
class TestProperties:
@pytest.mark.parametrize(
Expand Down

0 comments on commit 2506d5c

Please sign in to comment.