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: snapshot tests for diagrams #239

Merged
merged 16 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion src/safeds/data/image/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""Work with image data."""
"""Work with image.png data."""
lars-reimann marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/safeds/data/image/containers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Classes that can store image data."""
"""Classes that can store image.png data."""

from ._image import Image

Expand Down
36 changes: 18 additions & 18 deletions src/safeds/data/image/containers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

class Image:
"""
A container for image data.
A container for image.png data.

Parameters
----------
data : BinaryIO
The image data as bytes.
The image.png data as bytes.
"""

@staticmethod
def from_jpeg_file(path: str) -> Image:
"""
Create an image from a JPEG file.
Create an image.png from a JPEG file.

Parameters
----------
Expand All @@ -32,8 +32,8 @@ def from_jpeg_file(path: str) -> Image:

Returns
-------
image : Image
The image.
image.png : Image
The image.png.
"""
return Image(
data=Path(path).open("rb"),
Expand All @@ -43,7 +43,7 @@ def from_jpeg_file(path: str) -> Image:
@staticmethod
def from_png_file(path: str) -> Image:
"""
Create an image from a PNG file.
Create an image.png from a PNG file.

Parameters
----------
Expand All @@ -52,8 +52,8 @@ def from_png_file(path: str) -> Image:

Returns
-------
image : Image
The image.
image.png : Image
The image.png.
"""
return Image(
data=Path(path).open("rb"),
Expand All @@ -73,12 +73,12 @@ def __init__(self, data: BinaryIO, format_: ImageFormat):
@property
def format(self) -> ImageFormat:
"""
Get the image format.
Get the image.png format.

Returns
-------
format : ImageFormat
The image format.
The image.png format.
"""
return self._format

Expand All @@ -88,7 +88,7 @@ def format(self) -> ImageFormat:

def to_jpeg_file(self, path: str) -> None:
"""
Save the image as a JPEG file.
Save the image.png as a JPEG file.

Parameters
----------
Expand All @@ -100,7 +100,7 @@ def to_jpeg_file(self, path: str) -> None:

def to_png_file(self, path: str) -> None:
"""
Save the image as a PNG file.
Save the image.png as a PNG file.

Parameters
----------
Expand All @@ -116,14 +116,14 @@ def to_png_file(self, path: str) -> None:

def _repr_jpeg_(self) -> bytes | None:
"""
Return a JPEG image as bytes.
Return a JPEG image.png as bytes.

If the image is not a JPEG, return None.
If the image.png is not a JPEG, return None.

Returns
-------
jpeg : bytes
The image as JPEG.
The image.png as JPEG.
"""
if self._format != ImageFormat.JPEG:
return None
Expand All @@ -135,14 +135,14 @@ def _repr_jpeg_(self) -> bytes | None:

def _repr_png_(self) -> bytes | None:
"""
Return a PNG image as bytes.
Return a PNG image.png as bytes.

If the image is not a PNG, return None.
If the image.png is not a PNG, return None.

Returns
-------
png : bytes
The image as PNG.
The image.png as PNG.
"""
if self._format != ImageFormat.PNG:
return None
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/data/image/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Types used to distinguish different image formats."""
"""Types used to distinguish different image.png formats."""

from ._image_format import ImageFormat

Expand Down
4 changes: 2 additions & 2 deletions src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def plot_boxplot(self) -> Image:
Returns
-------
plot: Image
The plot as an image.
The plot as an image.png.

Raises
------
Expand Down Expand Up @@ -515,7 +515,7 @@ def plot_histogram(self) -> Image:
Returns
-------
plot: Image
The plot as an image.
The plot as an image.png.
"""
fig = plt.figure()
ax = sns.histplot(data=self._data)
Expand Down
6 changes: 3 additions & 3 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ def plot_correlation_heatmap(self) -> Image:
Returns
-------
plot: Image
The plot as an image.
The plot as an image.png.
"""
only_numerical = self.remove_columns_with_non_numerical_values()

Expand Down Expand Up @@ -1040,7 +1040,7 @@ def plot_lineplot(self, x_column_name: str, y_column_name: str) -> Image:
Returns
-------
plot: Image
The plot as an image.
The plot as an image.png.

Raises
------
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def plot_scatterplot(self, x_column_name: str, y_column_name: str) -> Image:
Returns
-------
plot: Image
The plot as an image.
The plot as an image.png.

Raises
------
Expand Down
Binary file added tests/resources/image/legacy_boxplot.png
PhilipGutberlet marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/image/legacy_heatmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/image/legacy_histogram_str.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/image/legacy_lineplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/resources/image/legacy_scatterplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 17 additions & 17 deletions tests/safeds/data/image/containers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
class TestFromJpegFile:
@pytest.mark.parametrize(
"path",
["image/white_square.jpg"],
["image.png/white_square.jpg"],
)
def test_should_load_jpeg_file(self, path: str) -> None:
Image.from_jpeg_file(resolve_resource_path(path))

@pytest.mark.parametrize(
"path",
["image/missing_file.jpg"],
["image.png/missing_file.jpg"],
)
def test_should_raise_if_file_not_found(self, path: str) -> None:
with pytest.raises(FileNotFoundError):
Expand All @@ -28,14 +28,14 @@ def test_should_raise_if_file_not_found(self, path: str) -> None:
class TestFromPngFile:
@pytest.mark.parametrize(
"path",
["image/white_square.png"],
["image.png/white_square.png"],
)
def test_should_load_png_file(self, path: str) -> None:
Image.from_png_file(resolve_resource_path(path))

@pytest.mark.parametrize(
"path",
["image/missing_file.png"],
["image.png/missing_file.png"],
)
def test_should_raise_if_file_not_found(self, path: str) -> None:
with pytest.raises(FileNotFoundError):
Expand All @@ -44,10 +44,10 @@ def test_should_raise_if_file_not_found(self, path: str) -> None:

class TestFormat:
@pytest.mark.parametrize(
("image", "format_"),
("image.png", "format_"),
[
(Image.from_jpeg_file(resolve_resource_path("image/white_square.jpg")), ImageFormat.JPEG),
(Image.from_png_file(resolve_resource_path("image/white_square.png")), ImageFormat.PNG),
(Image.from_jpeg_file(resolve_resource_path("image.png/white_square.jpg")), ImageFormat.JPEG),
(Image.from_png_file(resolve_resource_path("image.png/white_square.png")), ImageFormat.PNG),
],
)
def test_should_return_correct_format(self, image: Image, format_: ImageFormat) -> None:
Expand All @@ -57,7 +57,7 @@ def test_should_return_correct_format(self, image: Image, format_: ImageFormat)
class TestToJpegFile:
@pytest.mark.parametrize(
"path",
["image/white_square.jpg"],
["image.png/white_square.jpg"],
)
def test_should_save_jpeg_file(self, path: str) -> None:
image = Image.from_jpeg_file(resolve_resource_path(path))
Expand All @@ -75,7 +75,7 @@ def test_should_save_jpeg_file(self, path: str) -> None:
class TestToPngFile:
@pytest.mark.parametrize(
"path",
["image/white_square.png"],
["image.png/white_square.png"],
)
def test_should_save_png_file(self, path: str) -> None:
image = Image.from_png_file(resolve_resource_path(path))
Expand All @@ -92,31 +92,31 @@ def test_should_save_png_file(self, path: str) -> None:

class TestReprJpeg:
@pytest.mark.parametrize(
"image",
[Image.from_jpeg_file(resolve_resource_path("image/white_square.jpg"))],
"image.png",
[Image.from_jpeg_file(resolve_resource_path("image.png/white_square.jpg"))],
)
def test_should_return_bytes_if_image_is_jpeg(self, image: Image) -> None:
assert isinstance(image._repr_jpeg_(), bytes)

@pytest.mark.parametrize(
"image",
[Image.from_png_file(resolve_resource_path("image/white_square.png"))],
"image.png",
[Image.from_png_file(resolve_resource_path("image.png/white_square.png"))],
)
def test_should_return_none_if_image_is_not_jpeg(self, image: Image) -> None:
assert image._repr_jpeg_() is None


class TestReprPng:
@pytest.mark.parametrize(
"image",
[Image.from_png_file(resolve_resource_path("image/white_square.png"))],
"image.png",
[Image.from_png_file(resolve_resource_path("image.png/white_square.png"))],
)
def test_should_return_bytes_if_image_is_png(self, image: Image) -> None:
assert isinstance(image._repr_png_(), bytes)

@pytest.mark.parametrize(
"image",
[Image.from_jpeg_file(resolve_resource_path("image/white_square.jpg"))],
"image.png",
[Image.from_jpeg_file(resolve_resource_path("image.png/white_square.jpg"))],
)
def test_should_return_none_if_image_is_not_png(self, image: Image) -> None:
assert image._repr_png_() is None
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import _pytest
import matplotlib.pyplot as plt
import pytest

from safeds.data.image.containers import Image
from safeds.data.tabular.containers import Table
from safeds.data.tabular.exceptions import NonNumericColumnError
from tests.helpers import resolve_resource_path


def test_plot_boxplot_complex() -> None:
lars-reimann marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -27,3 +30,12 @@ def test_plot_boxplot_int(monkeypatch: _pytest.monkeypatch) -> None:
monkeypatch.setattr(plt, "show", lambda: None)
table = Table.from_dict({"A": [1, 2, 3]})
table.get_column("A").plot_boxplot()


def test_plot_boxplot_legacy() -> None:
PhilipGutberlet marked this conversation as resolved.
Show resolved Hide resolved
table = Table.from_dict({"A": [1, 2, 3]})
# table.get_column("A").plot_boxplot().to_png_file(resolve_resource_path("./image/legacy_boxplot.png"))
table.get_column("A").plot_boxplot()
current = table.get_column("A").plot_boxplot()
legacy = Image.from_png_file(resolve_resource_path("./image/legacy_boxplot.png"))
assert legacy._image.tobytes() == current._image.tobytes()
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import _pytest
import matplotlib.pyplot as plt
from safeds.data.tabular.containers import Table
from safeds.data.image.containers import Image
from tests.helpers import resolve_resource_path


def test_plot_histogram(monkeypatch: _pytest.monkeypatch) -> None:
PhilipGutberlet marked this conversation as resolved.
Show resolved Hide resolved
monkeypatch.setattr(plt, "show", lambda: None)
table = Table.from_dict({"A": [1, 2, 3]})
table.get_column("A").plot_histogram()


def test_plot_histogram_legacy_check_str() -> None:
table = Table.from_dict({"A": ["A", "B", "Apple"]})
#table.get_column("A").plot_histogram().to_png_file(resolve_resource_path("./image/legacy_histogram_str.png"))
current = table.get_column("A").plot_histogram()
legacy = Image.from_png_file(resolve_resource_path("./image/legacy_histogram_str.png"))
assert legacy._image.tobytes() == current._image.tobytes()

def test_plot_histogram_legacy_check_numeric() -> None:
table = Table.from_dict({"A": [1, 2, 3]})
#table.get_column("A").plot_histogram().to_png_file(resolve_resource_path("./image/legacy_histogram_numeric.png"))
current = table.get_column("A").plot_histogram()
legacy = Image.from_png_file(resolve_resource_path("./image/legacy_histogram_numeric.png"))
assert legacy._image.tobytes() == current._image.tobytes()
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import _pytest
import matplotlib.pyplot as plt

from safeds.data.image.containers import Image
from safeds.data.tabular.containers import Table
from tests.helpers import resolve_resource_path


def test_plot_correlation_heatmap_non_numeric(monkeypatch: _pytest.monkeypatch) -> None:
Expand All @@ -13,3 +16,10 @@ def test_plot_correlation_heatmap(monkeypatch: _pytest.monkeypatch) -> None:
monkeypatch.setattr(plt, "show", lambda: None)
table = Table.from_dict({"A": [1, 2, 3.5], "B": [2, 4, 7]})
table.plot_correlation_heatmap()

def test_plot_heatmap_legacy_check() -> None:
table = Table.from_dict({"A": [1, 2, 3.5], "B": [0.2, 4, 77]})
#table.plot_correlation_heatmap().to_png_file(resolve_resource_path("./image/legacy_heatmap.png"))
current = table.plot_correlation_heatmap()
legacy = Image.from_png_file(resolve_resource_path("./image/legacy_heatmap.png"))
assert legacy._image.tobytes() == current._image.tobytes()
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import _pytest
import matplotlib.pyplot as plt
import pytest

from safeds.data.image.containers import Image
from safeds.data.tabular.containers import Table
from safeds.data.tabular.exceptions import UnknownColumnNameError
from tests.helpers import resolve_resource_path


def test_plot_lineplot(monkeypatch: _pytest.monkeypatch) -> None:
Expand All @@ -15,3 +18,10 @@ def test_plot_lineplot_wrong_column_name() -> None:
table = Table.from_dict({"A": [1, 2, 3], "B": [2, 4, 7]})
with pytest.raises(UnknownColumnNameError):
table.plot_lineplot("C", "A")

def test_plot_lineplot_legacy_check() -> None:
table = Table.from_dict({"A": [1, 2, 3], "B": [2, 4, 7]})
#table.plot_lineplot("A", "B").to_png_file(resolve_resource_path("./image/legacy_lineplot.png"))
current = table.plot_lineplot("A", "B")
legacy = Image.from_png_file(resolve_resource_path("./image/legacy_lineplot.png"))
assert legacy._image.tobytes() == current._image.tobytes()
Loading