diff --git a/poetry.lock b/poetry.lock index 108e09a86..ade707ac6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -3525,6 +3525,20 @@ pure-eval = "*" [package.extras] tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"] +[[package]] +name = "syrupy" +version = "4.6.0" +description = "Pytest Snapshot Test Utility" +optional = false +python-versions = ">=3.8.1,<4" +files = [ + {file = "syrupy-4.6.0-py3-none-any.whl", hash = "sha256:747aae1bcf3cb3249e33b1e6d81097874d23615982d5686ebe637875b0775a1b"}, + {file = "syrupy-4.6.0.tar.gz", hash = "sha256:231b1f5d00f1f85048ba81676c79448076189c4aef4d33f21ae32f3b4c565a54"}, +] + +[package.dependencies] +pytest = ">=7.0.0,<8.0.0" + [[package]] name = "terminado" version = "0.18.0" @@ -3796,4 +3810,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.11,<3.13" -content-hash = "3abc3327cf2c4e7f96427b69bdbcc6e5baf0a8161d4e5c6c4c08746945ff369e" +content-hash = "410ca0fd5f08cab0690053c8ed863c5fa59144ab0eb3222317ecec0368a7c9f6" diff --git a/pyproject.toml b/pyproject.toml index 82ff7da2a..90b23982f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ seaborn = "^0.13.0" [tool.poetry.group.dev.dependencies] pytest = "^7.2.1" pytest-cov = "^4.0.0" +syrupy = "^4.6.0" [tool.poetry.group.docs.dependencies] jupyter = "^1.0.0" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..522a081d9 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,35 @@ +from typing import Any + +import matplotlib as mpl +import pytest +from safeds.data.image.containers import Image +from syrupy import SnapshotAssertion +from syrupy.extensions.single_file import SingleFileSnapshotExtension +from syrupy.types import SerializedData + +# Fix for failures when running pytest in a terminal (https://github.com/Safe-DS/Library/issues/482) +mpl.use("agg") + + +class JPEGImageExtension(SingleFileSnapshotExtension): + _file_extension = "jpg" + + def serialize(self, data: Image, **_kwargs: Any) -> SerializedData: + return data._repr_jpeg_() + + +@pytest.fixture() +def snapshot_jpeg(snapshot: SnapshotAssertion) -> SnapshotAssertion: + return snapshot.use_extension(JPEGImageExtension) + + +class PNGImageSnapshotExtension(SingleFileSnapshotExtension): + _file_extension = "png" + + def serialize(self, data: Image, **_kwargs: Any) -> SerializedData: + return data._repr_png_() + + +@pytest.fixture() +def snapshot_png(snapshot: SnapshotAssertion) -> SnapshotAssertion: + return snapshot.use_extension(PNGImageSnapshotExtension) diff --git a/tests/resources/image/adjusted_colors/by_0.5.png b/tests/resources/image/adjusted_colors/by_0.5.png deleted file mode 100644 index c85ff40cf..000000000 Binary files a/tests/resources/image/adjusted_colors/by_0.5.png and /dev/null differ diff --git a/tests/resources/image/adjusted_colors/by_0.png b/tests/resources/image/adjusted_colors/by_0.png deleted file mode 100644 index c147fec1d..000000000 Binary files a/tests/resources/image/adjusted_colors/by_0.png and /dev/null differ diff --git a/tests/resources/image/adjusted_colors/by_2.png b/tests/resources/image/adjusted_colors/by_2.png deleted file mode 100644 index 3184ada9c..000000000 Binary files a/tests/resources/image/adjusted_colors/by_2.png and /dev/null differ diff --git a/tests/resources/image/blurredBoy.png b/tests/resources/image/blurredBoy.png deleted file mode 100644 index 4b3d77676..000000000 Binary files a/tests/resources/image/blurredBoy.png and /dev/null differ diff --git a/tests/resources/image/boy.png b/tests/resources/image/boy.png deleted file mode 100644 index 20360eeff..000000000 Binary files a/tests/resources/image/boy.png and /dev/null differ diff --git a/tests/resources/image/brightness/brightened_by_0.5.png b/tests/resources/image/brightness/brightened_by_0.5.png deleted file mode 100644 index f2a45388c..000000000 Binary files a/tests/resources/image/brightness/brightened_by_0.5.png and /dev/null differ diff --git a/tests/resources/image/brightness/brightened_by_10.png b/tests/resources/image/brightness/brightened_by_10.png deleted file mode 100644 index bcf849d96..000000000 Binary files a/tests/resources/image/brightness/brightened_by_10.png and /dev/null differ diff --git a/tests/resources/image/brightness/to_brighten.png b/tests/resources/image/brightness/to_brighten.png deleted file mode 100644 index 2c9e5d18e..000000000 Binary files a/tests/resources/image/brightness/to_brighten.png and /dev/null differ diff --git a/tests/resources/image/contrast/contrast_adjusted_by_0.75.png b/tests/resources/image/contrast/contrast_adjusted_by_0.75.png deleted file mode 100644 index e0d1e1b41..000000000 Binary files a/tests/resources/image/contrast/contrast_adjusted_by_0.75.png and /dev/null differ diff --git a/tests/resources/image/contrast/contrast_adjusted_by_5.png b/tests/resources/image/contrast/contrast_adjusted_by_5.png deleted file mode 100644 index 67a6ecaaa..000000000 Binary files a/tests/resources/image/contrast/contrast_adjusted_by_5.png and /dev/null differ diff --git a/tests/resources/image/contrast/to_adjust_contrast.png b/tests/resources/image/contrast/to_adjust_contrast.png deleted file mode 100644 index 2c9e5d18e..000000000 Binary files a/tests/resources/image/contrast/to_adjust_contrast.png and /dev/null differ diff --git a/tests/resources/image/copy.png b/tests/resources/image/copy.png deleted file mode 100644 index 09e504aed..000000000 Binary files a/tests/resources/image/copy.png and /dev/null differ diff --git a/tests/resources/image/edgyBoy.png b/tests/resources/image/edgyBoy.png deleted file mode 100644 index e0aa5098f..000000000 Binary files a/tests/resources/image/edgyBoy.png and /dev/null differ diff --git a/tests/resources/image/flip_horizontally.png b/tests/resources/image/flip_horizontally.png deleted file mode 100644 index bad057148..000000000 Binary files a/tests/resources/image/flip_horizontally.png and /dev/null differ diff --git a/tests/resources/image/flip_vertically.png b/tests/resources/image/flip_vertically.png deleted file mode 100644 index 2031af9a7..000000000 Binary files a/tests/resources/image/flip_vertically.png and /dev/null differ diff --git a/tests/resources/image/inverted_colors_original.jpg b/tests/resources/image/inverted_colors_original.jpg deleted file mode 100644 index 7d23aea98..000000000 Binary files a/tests/resources/image/inverted_colors_original.jpg and /dev/null differ diff --git a/tests/resources/image/inverted_colors_original.png b/tests/resources/image/inverted_colors_original.png deleted file mode 100644 index 9f69bedb6..000000000 Binary files a/tests/resources/image/inverted_colors_original.png and /dev/null differ diff --git a/tests/resources/image/noise/noise_0.0.png b/tests/resources/image/noise/noise_0.0.png deleted file mode 100644 index 1956810e5..000000000 Binary files a/tests/resources/image/noise/noise_0.0.png and /dev/null differ diff --git a/tests/resources/image/noise/noise_0.7.png b/tests/resources/image/noise/noise_0.7.png deleted file mode 100644 index 21c78a42c..000000000 Binary files a/tests/resources/image/noise/noise_0.7.png and /dev/null differ diff --git a/tests/resources/image/noise/noise_2.5.png b/tests/resources/image/noise/noise_2.5.png deleted file mode 100644 index 0fcd5a56d..000000000 Binary files a/tests/resources/image/noise/noise_2.5.png and /dev/null differ diff --git a/tests/resources/image/original.png b/tests/resources/image/original.png deleted file mode 100644 index 09e504aed..000000000 Binary files a/tests/resources/image/original.png and /dev/null differ diff --git a/tests/resources/image/plane.jpg b/tests/resources/image/plane.jpg new file mode 100644 index 000000000..e4f70d30b Binary files /dev/null and b/tests/resources/image/plane.jpg differ diff --git a/tests/resources/image/plane.png b/tests/resources/image/plane.png new file mode 100644 index 000000000..49b7b1e72 Binary files /dev/null and b/tests/resources/image/plane.png differ diff --git a/tests/resources/image/sharpen/sharpened_by_-1.png b/tests/resources/image/sharpen/sharpened_by_-1.png deleted file mode 100644 index 927b96838..000000000 Binary files a/tests/resources/image/sharpen/sharpened_by_-1.png and /dev/null differ diff --git a/tests/resources/image/sharpen/sharpened_by_0.5.png b/tests/resources/image/sharpen/sharpened_by_0.5.png deleted file mode 100644 index afe187a9e..000000000 Binary files a/tests/resources/image/sharpen/sharpened_by_0.5.png and /dev/null differ diff --git a/tests/resources/image/sharpen/sharpened_by_10.png b/tests/resources/image/sharpen/sharpened_by_10.png deleted file mode 100644 index c7590fc53..000000000 Binary files a/tests/resources/image/sharpen/sharpened_by_10.png and /dev/null differ diff --git a/tests/resources/image/sharpen/to_sharpen.png b/tests/resources/image/sharpen/to_sharpen.png deleted file mode 100644 index 2c9e5d18e..000000000 Binary files a/tests/resources/image/sharpen/to_sharpen.png and /dev/null differ diff --git a/tests/resources/image/snapshot_boxplot_left_rotation.png b/tests/resources/image/snapshot_boxplot_left_rotation.png deleted file mode 100644 index 310667d96..000000000 Binary files a/tests/resources/image/snapshot_boxplot_left_rotation.png and /dev/null differ diff --git a/tests/resources/image/snapshot_boxplot_right_rotation.png b/tests/resources/image/snapshot_boxplot_right_rotation.png deleted file mode 100644 index 5eb177aca..000000000 Binary files a/tests/resources/image/snapshot_boxplot_right_rotation.png and /dev/null differ diff --git a/tests/resources/image/snapshot_empty_heatmap.png b/tests/resources/image/snapshot_empty_heatmap.png deleted file mode 100644 index decd3d60d..000000000 Binary files a/tests/resources/image/snapshot_empty_heatmap.png and /dev/null differ diff --git a/tests/resources/image/snapshot_heatmap_grayscale.png b/tests/resources/image/snapshot_heatmap_grayscale.png deleted file mode 100644 index ca7f451c0..000000000 Binary files a/tests/resources/image/snapshot_heatmap_grayscale.png and /dev/null differ diff --git a/tests/resources/image/snapshot_histograms/four_columns.png b/tests/resources/image/snapshot_histograms/four_columns.png deleted file mode 100644 index cdb102809..000000000 Binary files a/tests/resources/image/snapshot_histograms/four_columns.png and /dev/null differ diff --git a/tests/resources/image/snapshot_histograms/one_column.png b/tests/resources/image/snapshot_histograms/one_column.png deleted file mode 100644 index a36bcfa2d..000000000 Binary files a/tests/resources/image/snapshot_histograms/one_column.png and /dev/null differ diff --git a/tests/resources/image/white.png b/tests/resources/image/white.png deleted file mode 100644 index 4849863e8..000000000 Binary files a/tests/resources/image/white.png and /dev/null differ diff --git a/tests/resources/image/whiteCropped.png b/tests/resources/image/whiteCropped.png deleted file mode 100644 index fb18be863..000000000 Binary files a/tests/resources/image/whiteCropped.png and /dev/null differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[minimum noise].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[minimum noise].png new file mode 100644 index 000000000..57dee3a25 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[minimum noise].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[some noise].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[some noise].png new file mode 100644 index 000000000..d70b92972 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[some noise].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[very noisy].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[very noisy].png new file mode 100644 index 000000000..a45bce5c4 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAddGaussianNoise.test_should_add_noise[very noisy].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustBrightness.test_should_adjust_brightness[large factor].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustBrightness.test_should_adjust_brightness[large factor].png new file mode 100644 index 000000000..8d575b450 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustBrightness.test_should_adjust_brightness[large factor].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustBrightness.test_should_adjust_brightness[small factor].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustBrightness.test_should_adjust_brightness[small factor].png new file mode 100644 index 000000000..c1c5da058 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustBrightness.test_should_adjust_brightness[small factor].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustContrast.test_should_adjust_contrast[large factor].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustContrast.test_should_adjust_contrast[large factor].png new file mode 100644 index 000000000..d3576112b Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustContrast.test_should_adjust_contrast[large factor].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustContrast.test_should_adjust_contrast[small factor].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustContrast.test_should_adjust_contrast[small factor].png new file mode 100644 index 000000000..3326ddfd4 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestAdjustContrast.test_should_adjust_contrast[small factor].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestBlur.test_should_return_blurred_image[blur].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestBlur.test_should_return_blurred_image[blur].png new file mode 100644 index 000000000..a68d770f4 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestBlur.test_should_return_blurred_image[blur].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[add color].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[add color].png new file mode 100644 index 000000000..5bae77036 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[add color].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[remove all color].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[remove all color].png new file mode 100644 index 000000000..1c0e55cbb Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[remove all color].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[remove color].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[remove color].png new file mode 100644 index 000000000..a5815ce25 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestColorAdjust.test_should_adjust_colors[remove color].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestConvertToGrayscale.test_convert_to_grayscale[plane].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestConvertToGrayscale.test_convert_to_grayscale[plane].png new file mode 100644 index 000000000..06691ad71 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestConvertToGrayscale.test_convert_to_grayscale[plane].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestCrop.test_should_return_cropped_image[crop].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestCrop.test_should_return_cropped_image[crop].png new file mode 100644 index 000000000..690f5196b Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestCrop.test_should_return_cropped_image[crop].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestFindEdges.test_should_return_edges_of_image[find_edges].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestFindEdges.test_should_return_edges_of_image[find_edges].png new file mode 100644 index 000000000..b625bcdff Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestFindEdges.test_should_return_edges_of_image[find_edges].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestFlipHorizontally.test_should_flip_horizontally[plane].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestFlipHorizontally.test_should_flip_horizontally[plane].png new file mode 100644 index 000000000..35164d25e Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestFlipHorizontally.test_should_flip_horizontally[plane].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestFlipVertically.test_should_flip_vertically[plane].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestFlipVertically.test_should_flip_vertically[plane].png new file mode 100644 index 000000000..942b4cb7f Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestFlipVertically.test_should_flip_vertically[plane].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestInvertColors.test_should_invert_colors[invert-colors].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestInvertColors.test_should_invert_colors[invert-colors].png new file mode 100644 index 000000000..d1ca2753c Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestInvertColors.test_should_invert_colors[invert-colors].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestRotate.test_should_return_clockwise_rotated_image[rotate-clockwise].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestRotate.test_should_return_clockwise_rotated_image[rotate-clockwise].png new file mode 100644 index 000000000..ba19ff602 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestRotate.test_should_return_clockwise_rotated_image[rotate-clockwise].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestRotate.test_should_return_counter_clockwise_rotated_image[rotate-counter-clockwise].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestRotate.test_should_return_counter_clockwise_rotated_image[rotate-counter-clockwise].png new file mode 100644 index 000000000..603277fff Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestRotate.test_should_return_counter_clockwise_rotated_image[rotate-counter-clockwise].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[large factor].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[large factor].png new file mode 100644 index 000000000..265d06558 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[large factor].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[negative factor].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[negative factor].png new file mode 100644 index 000000000..1859ff818 Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[negative factor].png differ diff --git a/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[small factor].png b/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[small factor].png new file mode 100644 index 000000000..0ffa9db5b Binary files /dev/null and b/tests/safeds/data/image/containers/__snapshots__/test_image/TestSharpen.test_should_sharpen[small factor].png differ diff --git a/tests/safeds/data/image/containers/test_image.py b/tests/safeds/data/image/containers/test_image.py index e4b3af491..f1f907e1d 100644 --- a/tests/safeds/data/image/containers/test_image.py +++ b/tests/safeds/data/image/containers/test_image.py @@ -6,46 +6,59 @@ from safeds.data.image.typing import ImageFormat from safeds.data.tabular.containers import Table from safeds.exceptions import OutOfBoundsError +from syrupy import SnapshotAssertion from tests.helpers import resolve_resource_path class TestFromJpegFile: @pytest.mark.parametrize( - "path", - ["image/white_square.jpg", Path("image/white_square.jpg")], + "resource_path", + [ + "image/plane.jpg", + Path("image/plane.jpg"), + ], ids=["jpg", "jpg_Path"], ) - def test_should_load_jpeg_file(self, path: str | Path) -> None: - Image.from_jpeg_file(resolve_resource_path(path)) + def test_should_load_jpeg_file(self, resource_path: str | Path) -> None: + Image.from_jpeg_file(resolve_resource_path(resource_path)) @pytest.mark.parametrize( - "path", - ["image/missing_file.jpg", Path("image/missing_file.jpg")], + "resource_path", + [ + "image/missing_file.jpg", + Path("image/missing_file.jpg"), + ], ids=["missing_file_jpg", "missing_file_jpg_Path"], ) - def test_should_raise_if_file_not_found(self, path: str | Path) -> None: + def test_should_raise_if_file_not_found(self, resource_path: str | Path) -> None: with pytest.raises(FileNotFoundError): - Image.from_jpeg_file(resolve_resource_path(path)) + Image.from_jpeg_file(resolve_resource_path(resource_path)) class TestFromPngFile: @pytest.mark.parametrize( - "path", - ["image/white_square.png", Path("image/white_square.png")], + "resource_path", + [ + "image/plane.png", + Path("image/plane.png"), + ], ids=["png", "png_Path"], ) - def test_should_load_png_file(self, path: str | Path) -> None: - Image.from_png_file(resolve_resource_path(path)) + def test_should_load_png_file(self, resource_path: str | Path) -> None: + Image.from_png_file(resolve_resource_path(resource_path)) @pytest.mark.parametrize( - "path", - ["image/missing_file.png", Path("image/missing_file.png")], + "resource_path", + [ + "image/missing_file.png", + Path("image/missing_file.png"), + ], ids=["missing_file_png", "missing_file_png_Path"], ) - def test_should_raise_if_file_not_found(self, path: str | Path) -> None: + def test_should_raise_if_file_not_found(self, resource_path: str | Path) -> None: with pytest.raises(FileNotFoundError): - Image.from_png_file(resolve_resource_path(path)) + Image.from_png_file(resolve_resource_path(resource_path)) class TestFormat: @@ -71,12 +84,12 @@ class TestProperties: 1, ), ( - Image.from_png_file(resolve_resource_path("image/snapshot_boxplot.png")), - 640, - 480, + Image.from_png_file(resolve_resource_path("image/plane.png")), + 568, + 320, ), ], - ids=["[1,1].jpg", "[640,480].png"], + ids=["[1,1]", "[568,320]"], ) def test_should_return_image_properties(self, image: Image, width: int, height: int) -> None: assert image.width == width @@ -84,9 +97,13 @@ def test_should_return_image_properties(self, image: Image, width: int, height: class TestToJpegFile: - @pytest.mark.parametrize("path", ["image/white_square.jpg"], ids=["jpg_file"]) - def test_should_save_jpeg_file_by_str(self, path: str) -> None: - image = Image.from_jpeg_file(resolve_resource_path(path)) + @pytest.mark.parametrize( + "resource_path", + ["image/white_square.jpg"], + ids=["jpg"], + ) + def test_should_save_jpeg_file_by_str(self, resource_path: str) -> None: + image = Image.from_jpeg_file(resolve_resource_path(resource_path)) with NamedTemporaryFile() as tmp_file: tmp_file.close() @@ -97,9 +114,13 @@ def test_should_save_jpeg_file_by_str(self, path: str) -> None: assert image._image.tobytes() == image_read_back._image.tobytes() - @pytest.mark.parametrize("path", ["image/white_square.jpg"], ids=["jpg"]) - def test_should_save_jpeg_file_by_path(self, path: str) -> None: - image = Image.from_jpeg_file(resolve_resource_path(path)) + @pytest.mark.parametrize( + "resource_path", + ["image/white_square.jpg"], + ids=["jpg"], + ) + def test_should_save_jpeg_file_by_path(self, resource_path: str) -> None: + image = Image.from_jpeg_file(resolve_resource_path(resource_path)) with NamedTemporaryFile() as tmp_file: tmp_file.close() @@ -112,9 +133,13 @@ def test_should_save_jpeg_file_by_path(self, path: str) -> None: class TestToPngFile: - @pytest.mark.parametrize("path", ["image/white_square.png"], ids=["png"]) - def test_should_save_png_file_by_str(self, path: str) -> None: - image = Image.from_png_file(resolve_resource_path(path)) + @pytest.mark.parametrize( + "resource_path", + ["image/white_square.png"], + ids=["png"], + ) + def test_should_save_png_file_by_str(self, resource_path: str) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) with NamedTemporaryFile() as tmp_file: tmp_file.close() @@ -125,9 +150,13 @@ def test_should_save_png_file_by_str(self, path: str) -> None: assert image._image.tobytes() == image_read_back._image.tobytes() - @pytest.mark.parametrize("path", ["image/white_square.png"], ids=["png"]) - def test_should_save_png_file_by_path(self, path: str) -> None: - image = Image.from_png_file(resolve_resource_path(path)) + @pytest.mark.parametrize( + "resource_path", + ["image/white_square.png"], + ids=["png"], + ) + def test_should_save_png_file_by_path(self, resource_path: str) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) with NamedTemporaryFile() as tmp_file: tmp_file.close() @@ -141,37 +170,41 @@ def test_should_save_png_file_by_path(self, path: str) -> None: class TestReprJpeg: @pytest.mark.parametrize( - "image", - [Image.from_jpeg_file(resolve_resource_path("image/white_square.jpg"))], + "resource_path", + ["image/white_square.jpg"], ids=["jpg"], ) - def test_should_return_bytes_if_image_is_jpeg(self, image: Image) -> None: + def test_should_return_bytes_if_image_is_jpeg(self, resource_path: str) -> None: + image = Image.from_jpeg_file(resolve_resource_path(resource_path)) assert isinstance(image._repr_jpeg_(), bytes) @pytest.mark.parametrize( - "image", - [Image.from_png_file(resolve_resource_path("image/white_square.png"))], + "resource_path", + ["image/white_square.png"], ids=["png"], ) - def test_should_return_none_if_image_is_not_jpeg(self, image: Image) -> None: + def test_should_return_none_if_image_is_not_jpeg(self, resource_path: str) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) assert image._repr_jpeg_() is None class TestReprPng: @pytest.mark.parametrize( - "image", - [Image.from_png_file(resolve_resource_path("image/white_square.png"))], + "resource_path", + ["image/white_square.png"], ids=["png"], ) - def test_should_return_bytes_if_image_is_png(self, image: Image) -> None: + def test_should_return_bytes_if_image_is_png(self, resource_path: str) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) assert isinstance(image._repr_png_(), bytes) @pytest.mark.parametrize( - "image", - [Image.from_jpeg_file(resolve_resource_path("image/white_square.jpg"))], + "resource_path", + ["image/white_square.jpg"], ids=["jpg"], ) - def test_should_return_none_if_image_is_not_png(self, image: Image) -> None: + def test_should_return_none_if_image_is_not_png(self, resource_path: str) -> None: + image = Image.from_jpeg_file(resolve_resource_path(resource_path)) assert image._repr_png_() is None @@ -200,214 +233,201 @@ def test_should_return_resized_image( class TestConvertToGrayscale: @pytest.mark.parametrize( - ("image", "expected"), - [ - ( - Image.from_png_file(resolve_resource_path("image/snapshot_heatmap.png")), - Image.from_png_file(resolve_resource_path("image/snapshot_heatmap_grayscale.png")), - ), - ], - ids=["grayscale"], + "resource_path", + ["image/plane.png"], + ids=["plane"], ) - def test_convert_to_grayscale(self, image: Image, expected: Image) -> None: - grayscale_image = image.convert_to_grayscale() - assert grayscale_image == expected + def test_convert_to_grayscale(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.convert_to_grayscale() == snapshot_png -class TestEQ: +class TestEquals: def test_should_be_equal(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/original.png")) - image2 = Image.from_png_file(resolve_resource_path("image/copy.png")) + image = Image.from_png_file(resolve_resource_path("image/white_square.png")) + image2 = Image.from_png_file(resolve_resource_path("image/white_square.png")) assert image == image2 def test_should_not_be_equal(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/original.png")) - image2 = Image.from_png_file(resolve_resource_path("image/white_square.png")) + image = Image.from_png_file(resolve_resource_path("image/white_square.png")) + image2 = Image.from_png_file(resolve_resource_path("image/plane.png")) assert image != image2 def test_should_raise(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/original.png")) + image = Image.from_png_file(resolve_resource_path("image/white_square.png")) other = Table() assert (image.__eq__(other)) is NotImplemented class TestFlipVertically: - def test_should_flip_vertically(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/original.png")) - image2 = image.flip_vertically() - image3 = Image.from_png_file(resolve_resource_path("image/flip_vertically.png")) - assert image != image2 - assert image2 == image3 + @pytest.mark.parametrize( + "resource_path", + ["image/plane.png"], + ids=["plane"], + ) + def test_should_flip_vertically(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.flip_vertically() == snapshot_png - def test_should_be_original(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/original.png")) - image2 = image.flip_vertically().flip_vertically() - assert image == image2 + def test_should_be_original_if_flipped_twice(self) -> None: + original = Image.from_png_file(resolve_resource_path("image/plane.png")) + flipped_twice = original.flip_vertically().flip_vertically() + assert original == flipped_twice class TestFlipHorizontally: - def test_should_flip_horizontally(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/original.png")) - image2 = image.flip_horizontally() - image3 = Image.from_png_file(resolve_resource_path("image/flip_horizontally.png")) - assert image != image2 - assert image2 == image3 + @pytest.mark.parametrize( + "resource_path", + ["image/plane.png"], + ids=["plane"], + ) + def test_should_flip_horizontally(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.flip_horizontally() == snapshot_png - def test_should_be_original(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/original.png")) - image2 = image.flip_horizontally().flip_horizontally() - assert image == image2 + def test_should_be_original_if_flipped_twice(self) -> None: + original = Image.from_png_file(resolve_resource_path("image/plane.png")) + flipped_twice = original.flip_horizontally().flip_horizontally() + assert original == flipped_twice class TestAdjustContrast: - @pytest.mark.parametrize("factor", [0.75, 5], ids=["small factor", "large factor"]) - def test_should_adjust_contrast(self, factor: float) -> None: - image = Image.from_png_file(resolve_resource_path("image/contrast/to_adjust_contrast.png")) - image2 = image.adjust_contrast(factor) - image3 = Image.from_png_file( - resolve_resource_path("image/contrast/contrast_adjusted_by_" + str(factor) + ".png"), - ) - assert image != image2 - assert image2 == image3 + @pytest.mark.parametrize( + ("resource_path", "factor"), + [ + ("image/plane.png", 0.75), + ("image/plane.png", 5), + ], + ids=["small factor", "large factor"], + ) + def test_should_adjust_contrast(self, resource_path: str, factor: float, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.adjust_contrast(factor) == snapshot_png def test_should_not_adjust_contrast(self) -> None: + original = Image.from_png_file(resolve_resource_path("image/plane.png")) with pytest.warns( UserWarning, match="Contrast adjustment factor is 1.0, this will not make changes to the image.", ): - image = Image.from_png_file(resolve_resource_path("image/contrast/to_adjust_contrast.png")) - image2 = image.adjust_contrast(1) - assert image == image2 + adjusted = original.adjust_contrast(1) + assert original == adjusted def test_should_raise(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/brightness/to_brighten.png")) + image = Image.from_png_file(resolve_resource_path("image/plane.png")) with pytest.raises(OutOfBoundsError, match=r"factor \(=-1\) is not inside \[0, \u221e\)."): image.adjust_contrast(-1) -class TestBrightness: - @pytest.mark.parametrize("factor", [0.5, 10], ids=["small factor", "large factor"]) - def test_should_adjust_brightness(self, factor: float) -> None: - image = Image.from_png_file(resolve_resource_path("image/brightness/to_brighten.png")) - image2 = image.adjust_brightness(factor) - image3 = Image.from_png_file(resolve_resource_path("image/brightness/brightened_by_" + str(factor) + ".png")) - assert image != image2 - assert image2 == image3 +class TestAdjustBrightness: + @pytest.mark.parametrize( + ("resource_path", "factor"), + [ + ("image/plane.png", 0.5), + ("image/plane.png", 10), + ], + ids=["small factor", "large factor"], + ) + def test_should_adjust_brightness(self, resource_path: str, factor: float, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.adjust_brightness(factor) == snapshot_png def test_should_not_brighten(self) -> None: + image = Image.from_png_file(resolve_resource_path("image/plane.png")) with pytest.warns( UserWarning, match="Brightness adjustment factor is 1.0, this will not make changes to the image.", ): - image = Image.from_png_file(resolve_resource_path("image/brightness/to_brighten.png")) image2 = image.adjust_brightness(1) - assert image == image2 + assert image == image2 def test_should_raise(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/brightness/to_brighten.png")) + image = Image.from_png_file(resolve_resource_path("image/plane.png")) with pytest.raises(OutOfBoundsError, match=r"factor \(=-1\) is not inside \[0, \u221e\)."): image.adjust_brightness(-1) class TestInvertColors: @pytest.mark.parametrize( - ("image", "expected"), - [ - ( - Image.from_png_file(resolve_resource_path("image/original.png")), - Image.from_png_file(resolve_resource_path("image/inverted_colors_original.png")), - ), - ], + "resource_path", + ["image/plane.png"], ids=["invert-colors"], ) - def test_should_invert_colors(self, image: Image, expected: Image) -> None: - image = image.invert_colors() - assert image == expected + def test_should_invert_colors(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.invert_colors() == snapshot_png class TestColorAdjust: @pytest.mark.parametrize( - ("image", "factor", "expected"), + ("resource_path", "factor"), [ - ( - Image.from_png_file(resolve_resource_path("image/original.png")), - 2, - Image.from_png_file(resolve_resource_path("image/adjusted_colors/by_2.png")), - ), - ( - Image.from_png_file(resolve_resource_path("image/original.png")), - 0.5, - Image.from_png_file(resolve_resource_path("image/adjusted_colors/by_0.5.png")), - ), - ( - Image.from_png_file(resolve_resource_path("image/original.png")), - 0, - Image.from_png_file(resolve_resource_path("image/adjusted_colors/by_0.png")), - ), + ("image/plane.png", 2), + ("image/plane.png", 0.5), + ("image/plane.png", 0), ], ids=["add color", "remove color", "remove all color"], ) - def test_should_adjust_colors(self, image: Image, factor: float, expected: Image) -> None: - image = image.adjust_color_balance(factor) - assert image == expected + def test_should_adjust_colors(self, resource_path: str, factor: float, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.adjust_color_balance(factor) == snapshot_png @pytest.mark.parametrize( - ("image", "factor"), + ("resource_path", "factor"), [ - ( - Image.from_png_file(resolve_resource_path("image/original.png")), - -1, - ), + ("image/plane.png", -1), ], ids=["negative"], ) - def test_should_throw(self, image: Image, factor: float) -> None: + def test_should_throw(self, resource_path: str, factor: float) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) with pytest.raises(OutOfBoundsError, match=rf"factor \(={factor}\) is not inside \[0, \u221e\)."): image.adjust_color_balance(factor) @pytest.mark.parametrize( - ("image", "factor"), + ("resource_path", "factor"), [ - ( - Image.from_png_file(resolve_resource_path("image/original.png")), - 1, - ), + ("image/plane.png", 1), ], ids=["no change"], ) - def test_should_warn(self, image: Image, factor: float) -> None: + def test_should_warn(self, resource_path: str, factor: float) -> None: + original = Image.from_png_file(resolve_resource_path(resource_path)) with pytest.warns( UserWarning, match="Color adjustment factor is 1.0, this will not make changes to the image.", ): - adjust = image.adjust_color_balance(factor) - assert adjust == image + adjusted = original.adjust_color_balance(factor) + assert adjusted == original class TestAddGaussianNoise: @pytest.mark.parametrize( - ("image", "standard_deviation"), + ("resource_path", "standard_deviation"), [ - (Image.from_png_file(resolve_resource_path("image/boy.png")), 0.0), - (Image.from_png_file(resolve_resource_path("image/boy.png")), 0.7), - (Image.from_png_file(resolve_resource_path("image/boy.png")), 2.5), + ("image/plane.png", 0.0), + ("image/plane.png", 0.7), + ("image/plane.png", 2.5), ], ids=["minimum noise", "some noise", "very noisy"], ) - def test_should_add_noise(self, image: Image, standard_deviation: float) -> None: - expected = Image.from_png_file( - resolve_resource_path("image/noise/noise_" + str(standard_deviation) + ".png"), - ) - image = image.add_gaussian_noise(standard_deviation) - - assert image == expected + def test_should_add_noise( + self, + resource_path: str, + standard_deviation: float, + snapshot_png: SnapshotAssertion, + ) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.add_gaussian_noise(standard_deviation) == snapshot_png @pytest.mark.parametrize( - ("image", "standard_deviation"), - [(Image.from_png_file(resolve_resource_path("image/boy.png")), -1)], + ("resource_path", "standard_deviation"), + [("image/plane.png", -1)], ids=["sigma below zero"], ) - def test_should_raise_standard_deviation(self, image: Image, standard_deviation: float) -> None: + def test_should_raise_standard_deviation(self, resource_path: str, standard_deviation: float) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + with pytest.raises( OutOfBoundsError, match=rf"standard_deviation \(={standard_deviation}\) is not inside \[0, \u221e\)\.", @@ -417,94 +437,76 @@ def test_should_raise_standard_deviation(self, image: Image, standard_deviation: class TestBlur: @pytest.mark.parametrize( - ("image", "expected"), - [ - ( - Image.from_png_file(resolve_resource_path("image/boy.png")), - Image.from_png_file(resolve_resource_path("image/blurredBoy.png")), - ), - ], + "resource_path", + ["image/plane.png"], ids=["blur"], ) - def test_should_return_blurred_image(self, image: Image, expected: Image) -> None: - image = image.blur(2) - assert image == expected + def test_should_return_blurred_image(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.blur(2) == snapshot_png class TestCrop: @pytest.mark.parametrize( - ("image", "expected"), - [ - ( - Image.from_png_file(resolve_resource_path("image/white.png")), - Image.from_png_file(resolve_resource_path("image/whiteCropped.png")), - ), - ], + "resource_path", + ["image/plane.png"], ids=["crop"], ) - def test_should_return_cropped_image(self, image: Image, expected: Image) -> None: - image = image.crop(0, 0, 100, 100) - assert image == expected + def test_should_return_cropped_image(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.crop(0, 0, 100, 100) == snapshot_png class TestSharpen: - @pytest.mark.parametrize("factor", [-1, 0.5, 10], ids=["negative factor", "small factor", "large factor"]) - def test_should_sharpen(self, factor: float) -> None: - image = Image.from_png_file(resolve_resource_path("image/sharpen/to_sharpen.png")) - image2 = image.sharpen(factor) - image2.to_png_file(resolve_resource_path("image/sharpen/sharpened_by_" + str(factor) + ".png")) - assert image != image2 - assert image2 == Image.from_png_file( - resolve_resource_path("image/sharpen/sharpened_by_" + str(factor) + ".png"), - ) + @pytest.mark.parametrize( + ("resource_path", "factor"), + [ + ("image/plane.png", -1), + ("image/plane.png", 0.5), + ("image/plane.png", 10), + ], + ids=["negative factor", "small factor", "large factor"], + ) + def test_should_sharpen(self, resource_path: str, factor: float, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.sharpen(factor) == snapshot_png - def test_should_not_sharpen(self) -> None: - image = Image.from_png_file(resolve_resource_path("image/sharpen/to_sharpen.png")) + def test_should_not_sharpen_if_factor_is_1(self) -> None: + image = Image.from_png_file(resolve_resource_path("image/plane.png")) image2 = image.sharpen(1) assert image == image2 class TestRotate: @pytest.mark.parametrize( - ("image", "expected"), - [ - ( - Image.from_png_file(resolve_resource_path("image/snapshot_boxplot.png")), - Image.from_png_file(resolve_resource_path("image/snapshot_boxplot_right_rotation.png")), - ), - ], + "resource_path", + ["image/plane.png"], ids=["rotate-clockwise"], ) - def test_should_return_clockwise_rotated_image(self, image: Image, expected: Image) -> None: - image = image.rotate_right() - assert image == expected + def test_should_return_clockwise_rotated_image(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.rotate_right() == snapshot_png @pytest.mark.parametrize( - ("image", "expected"), - [ - ( - Image.from_png_file(resolve_resource_path("image/snapshot_boxplot.png")), - Image.from_png_file(resolve_resource_path("image/snapshot_boxplot_left_rotation.png")), - ), - ], + "resource_path", + ["image/plane.png"], ids=["rotate-counter-clockwise"], ) - def test_should_return_counter_clockwise_rotated_image(self, image: Image, expected: Image) -> None: - image = image.rotate_left() - assert image == expected + def test_should_return_counter_clockwise_rotated_image( + self, + resource_path: str, + snapshot_png: SnapshotAssertion, + ) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.rotate_left() == snapshot_png class TestFindEdges: @pytest.mark.parametrize( - ("image", "expected"), - [ - ( - Image.from_png_file(resolve_resource_path("image/boy.png")), - Image.from_png_file(resolve_resource_path("image/edgyBoy.png")), - ), - ], + "resource_path", + ["image/plane.png"], ids=["find_edges"], ) - def test_should_return_edges_of_image(self, image: Image, expected: Image) -> None: - image = image.find_edges() - assert image == expected + def test_should_return_edges_of_image(self, resource_path: str, snapshot_png: SnapshotAssertion) -> None: + image = Image.from_png_file(resolve_resource_path(resource_path)) + assert image.find_edges() == snapshot_png diff --git a/tests/resources/image/snapshot_boxplot.png b/tests/safeds/data/tabular/containers/_column/__snapshots__/test_plot_boxplot/test_should_match_snapshot.png similarity index 100% rename from tests/resources/image/snapshot_boxplot.png rename to tests/safeds/data/tabular/containers/_column/__snapshots__/test_plot_boxplot/test_should_match_snapshot.png diff --git a/tests/resources/image/snapshot_histogram_numeric.png b/tests/safeds/data/tabular/containers/_column/__snapshots__/test_plot_histogram/test_should_match_snapshot_numeric.png similarity index 100% rename from tests/resources/image/snapshot_histogram_numeric.png rename to tests/safeds/data/tabular/containers/_column/__snapshots__/test_plot_histogram/test_should_match_snapshot_numeric.png diff --git a/tests/resources/image/snapshot_histogram_str.png b/tests/safeds/data/tabular/containers/_column/__snapshots__/test_plot_histogram/test_should_match_snapshot_str.png similarity index 100% rename from tests/resources/image/snapshot_histogram_str.png rename to tests/safeds/data/tabular/containers/_column/__snapshots__/test_plot_histogram/test_should_match_snapshot_str.png diff --git a/tests/safeds/data/tabular/containers/_column/test_plot_boxplot.py b/tests/safeds/data/tabular/containers/_column/test_plot_boxplot.py index 54b57b74c..337e07762 100644 --- a/tests/safeds/data/tabular/containers/_column/test_plot_boxplot.py +++ b/tests/safeds/data/tabular/containers/_column/test_plot_boxplot.py @@ -1,19 +1,13 @@ import pytest -from safeds.data.image.containers import Image from safeds.data.tabular.containers import Table from safeds.exceptions import NonNumericColumnError +from syrupy import SnapshotAssertion -from tests.helpers import resolve_resource_path - -def test_should_match_snapshot() -> None: +def test_should_match_snapshot(snapshot_png: SnapshotAssertion) -> None: table = Table({"A": [1, 2, 3]}) - current = table.get_column("A").plot_boxplot() - snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_boxplot.png")) - - # Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm. - assertion = snapshot._image.tobytes() == current._image.tobytes() - assert assertion + boxplot = table.get_column("A").plot_boxplot() + assert boxplot == snapshot_png def test_should_raise_if_column_contains_non_numerical_values() -> None: diff --git a/tests/safeds/data/tabular/containers/_column/test_plot_histogram.py b/tests/safeds/data/tabular/containers/_column/test_plot_histogram.py index fe4157b6a..3ca198cda 100644 --- a/tests/safeds/data/tabular/containers/_column/test_plot_histogram.py +++ b/tests/safeds/data/tabular/containers/_column/test_plot_histogram.py @@ -1,21 +1,14 @@ -from safeds.data.image.containers import Image from safeds.data.tabular.containers import Table +from syrupy import SnapshotAssertion -from tests.helpers import resolve_resource_path - -def test_should_match_snapshot_numeric() -> None: +def test_should_match_snapshot_numeric(snapshot_png: SnapshotAssertion) -> None: table = Table({"A": [1, 2, 3]}) - current = table.get_column("A").plot_histogram() - snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_histogram_numeric.png")) - - # Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm. - assertion = snapshot._image.tobytes() == current._image.tobytes() - assert assertion + histogram = table.get_column("A").plot_histogram() + assert histogram == snapshot_png -def test_should_match_snapshot_str() -> None: +def test_should_match_snapshot_str(snapshot_png: SnapshotAssertion) -> None: table = Table({"A": ["A", "B", "Apple"]}) - current = table.get_column("A").plot_histogram() - snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_histogram_str.png")) - assert snapshot._image.tobytes() == current._image.tobytes() + histogram = table.get_column("A").plot_histogram() + assert histogram == snapshot_png diff --git a/tests/resources/image/snapshot_boxplots/four_columns_all_numeric.png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_boxplots/test_should_match_snapshot[four columns (all numeric)].png similarity index 100% rename from tests/resources/image/snapshot_boxplots/four_columns_all_numeric.png rename to tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_boxplots/test_should_match_snapshot[four columns (all numeric)].png diff --git a/tests/resources/image/snapshot_boxplots/four_columns_some_non_numeric.png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_boxplots/test_should_match_snapshot[four columns (some non-numeric)].png similarity index 100% rename from tests/resources/image/snapshot_boxplots/four_columns_some_non_numeric.png rename to tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_boxplots/test_should_match_snapshot[four columns (some non-numeric)].png diff --git a/tests/resources/image/snapshot_boxplots/one_column.png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_boxplots/test_should_match_snapshot[one column].png similarity index 100% rename from tests/resources/image/snapshot_boxplots/one_column.png rename to tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_boxplots/test_should_match_snapshot[one column].png diff --git a/tests/resources/image/snapshot_heatmap.png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_correlation_heatmap/test_should_match_snapshot[normal].png similarity index 100% rename from tests/resources/image/snapshot_heatmap.png rename to tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_correlation_heatmap/test_should_match_snapshot[normal].png diff --git a/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_histograms/test_should_match_snapshot[four columns].png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_histograms/test_should_match_snapshot[four columns].png new file mode 100644 index 000000000..2218cd6a0 Binary files /dev/null and b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_histograms/test_should_match_snapshot[four columns].png differ diff --git a/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_histograms/test_should_match_snapshot[one column].png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_histograms/test_should_match_snapshot[one column].png new file mode 100644 index 000000000..fb6937a2d Binary files /dev/null and b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_histograms/test_should_match_snapshot[one column].png differ diff --git a/tests/resources/image/snapshot_lineplot.png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_lineplot/test_should_match_snapshot.png similarity index 100% rename from tests/resources/image/snapshot_lineplot.png rename to tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_lineplot/test_should_match_snapshot.png diff --git a/tests/resources/image/snapshot_scatterplot.png b/tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_scatterplot/test_should_match_snapshot.png similarity index 100% rename from tests/resources/image/snapshot_scatterplot.png rename to tests/safeds/data/tabular/containers/_table/__snapshots__/test_plot_scatterplot/test_should_match_snapshot.png diff --git a/tests/safeds/data/tabular/containers/_table/test_plot_boxplots.py b/tests/safeds/data/tabular/containers/_table/test_plot_boxplots.py index d8f615ef7..7755e9451 100644 --- a/tests/safeds/data/tabular/containers/_table/test_plot_boxplots.py +++ b/tests/safeds/data/tabular/containers/_table/test_plot_boxplots.py @@ -1,33 +1,21 @@ import pytest -from safeds.data.image.containers import Image from safeds.data.tabular.containers import Table from safeds.exceptions import NonNumericColumnError - -from tests.helpers import resolve_resource_path +from syrupy import SnapshotAssertion @pytest.mark.parametrize( - ("table", "path"), + "table", [ - (Table({"A": [1, 2, 3]}), "./image/snapshot_boxplots/one_column.png"), - ( - Table({"A": [1, 2, 3], "B": ["A", "A", "Bla"], "C": [True, True, False], "D": [1.0, 2.1, 4.5]}), - "./image/snapshot_boxplots/four_columns_some_non_numeric.png", - ), - ( - Table({"A": [1, 2, 3], "B": [1.0, 2.1, 4.5], "C": [1, 2, 3], "D": [1.0, 2.1, 4.5]}), - "./image/snapshot_boxplots/four_columns_all_numeric.png", - ), + Table({"A": [1, 2, 3]}), + Table({"A": [1, 2, 3], "B": ["A", "A", "Bla"], "C": [True, True, False], "D": [1.0, 2.1, 4.5]}), + Table({"A": [1, 2, 3], "B": [1.0, 2.1, 4.5], "C": [1, 2, 3], "D": [1.0, 2.1, 4.5]}), ], ids=["one column", "four columns (some non-numeric)", "four columns (all numeric)"], ) -def test_should_match_snapshot(table: Table, path: str) -> None: - current = table.plot_boxplots() - snapshot = Image.from_png_file(resolve_resource_path(path)) - - # Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm. - assertion = snapshot._image.tobytes() == current._image.tobytes() - assert assertion +def test_should_match_snapshot(table: Table, snapshot_png: SnapshotAssertion) -> None: + boxplots = table.plot_boxplots() + assert boxplots == snapshot_png def test_should_raise_if_column_contains_non_numerical_values() -> None: diff --git a/tests/safeds/data/tabular/containers/_table/test_plot_correlation_heatmap.py b/tests/safeds/data/tabular/containers/_table/test_plot_correlation_heatmap.py index 902a184bd..cf730d870 100644 --- a/tests/safeds/data/tabular/containers/_table/test_plot_correlation_heatmap.py +++ b/tests/safeds/data/tabular/containers/_table/test_plot_correlation_heatmap.py @@ -1,29 +1,23 @@ import pytest -from safeds.data.image.containers import Image from safeds.data.tabular.containers import Table - -from tests.helpers import resolve_resource_path +from syrupy import SnapshotAssertion @pytest.mark.parametrize( - ("table", "path"), + "table", [ - (Table({"A": [1, 2, 3.5], "B": [0.2, 4, 77]}), "./image/snapshot_heatmap.png"), - (Table(), "./image/snapshot_empty_heatmap.png"), + Table({"A": [1, 2, 3.5], "B": [0.2, 4, 77]}), ], - ids=["normal", "empty"], + ids=["normal"], ) -def test_should_match_snapshot(table: Table, path: str) -> None: - if table.number_of_rows == 0: - with pytest.warns( - UserWarning, - match=r"An empty table has been used. A correlation heatmap on an empty table will show nothing.", - ): - current = table.plot_correlation_heatmap() - else: - current = table.plot_correlation_heatmap() - snapshot = Image.from_png_file(resolve_resource_path(path)) +def test_should_match_snapshot(table: Table, snapshot_png: SnapshotAssertion) -> None: + correlation_heatmap = table.plot_correlation_heatmap() + assert correlation_heatmap == snapshot_png + - # Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm. - assertion = snapshot == current - assert assertion +def test_should_warn_about_empty_table() -> None: + with pytest.warns( + UserWarning, + match=r"An empty table has been used. A correlation heatmap on an empty table will show nothing.", + ): + Table().plot_correlation_heatmap() diff --git a/tests/safeds/data/tabular/containers/_table/test_plot_histograms.py b/tests/safeds/data/tabular/containers/_table/test_plot_histograms.py index aa9f413bc..bde2b2dcd 100644 --- a/tests/safeds/data/tabular/containers/_table/test_plot_histograms.py +++ b/tests/safeds/data/tabular/containers/_table/test_plot_histograms.py @@ -1,34 +1,26 @@ import pytest -from safeds.data.image.containers import Image from safeds.data.tabular.containers import Table - -from tests.helpers import resolve_resource_path +from syrupy import SnapshotAssertion @pytest.mark.parametrize( - ("table", "path"), + "table", [ - (Table({"A": [1, 2, 3]}), "./image/snapshot_histograms/one_column.png"), - ( - Table( - { - "A": [1, 2, 3, 3, 2, 4, 2], - "B": ["a", "b", "b", "b", "b", "b", "a"], - "C": [True, True, False, True, False, None, True], - "D": [1.0, 2.1, 2.1, 2.1, 2.1, 3.0, 3.0], - }, - ), - "./image/snapshot_histograms/four_columns.png", + Table({"A": [1, 2, 3]}), + Table( + { + "A": [1, 2, 3, 3, 2, 4, 2], + "B": ["a", "b", "b", "b", "b", "b", "a"], + "C": [True, True, False, True, False, None, True], + "D": [1.0, 2.1, 2.1, 2.1, 2.1, 3.0, 3.0], + }, ), ], ids=["one column", "four columns"], ) -def test_should_match_snapshot(table: Table, path: str) -> None: - current = table.plot_histograms() - snapshot = Image.from_png_file(resolve_resource_path(path)) - # Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm. - assertion = snapshot == current - assert assertion +def test_should_match_snapshot(table: Table, snapshot_png: SnapshotAssertion) -> None: + histograms = table.plot_histograms() + assert histograms == snapshot_png def test_should_fail_on_empty_table() -> None: diff --git a/tests/safeds/data/tabular/containers/_table/test_plot_lineplot.py b/tests/safeds/data/tabular/containers/_table/test_plot_lineplot.py index 93365ec76..c41eee30b 100644 --- a/tests/safeds/data/tabular/containers/_table/test_plot_lineplot.py +++ b/tests/safeds/data/tabular/containers/_table/test_plot_lineplot.py @@ -1,19 +1,13 @@ import pytest -from safeds.data.image.containers import Image from safeds.data.tabular.containers import Table from safeds.exceptions import UnknownColumnNameError +from syrupy import SnapshotAssertion -from tests.helpers import resolve_resource_path - -def test_should_match_snapshot() -> None: +def test_should_match_snapshot(snapshot_png: SnapshotAssertion) -> None: table = Table({"A": [1, 2, 3], "B": [2, 4, 7]}) - current = table.plot_lineplot("A", "B") - snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_lineplot.png")) - - # Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm. - assertion = snapshot._image.tobytes() == current._image.tobytes() - assert assertion + lineplot = table.plot_lineplot("A", "B") + assert lineplot == snapshot_png @pytest.mark.parametrize( diff --git a/tests/safeds/data/tabular/containers/_table/test_plot_scatterplot.py b/tests/safeds/data/tabular/containers/_table/test_plot_scatterplot.py index f07763fb8..029d3e50d 100644 --- a/tests/safeds/data/tabular/containers/_table/test_plot_scatterplot.py +++ b/tests/safeds/data/tabular/containers/_table/test_plot_scatterplot.py @@ -1,19 +1,13 @@ import pytest -from safeds.data.image.containers import Image from safeds.data.tabular.containers import Table from safeds.exceptions import UnknownColumnNameError +from syrupy import SnapshotAssertion -from tests.helpers import resolve_resource_path - -def test_should_match_snapshot() -> None: +def test_should_match_snapshot(snapshot_png: SnapshotAssertion) -> None: table = Table({"A": [1, 2, 3], "B": [2, 4, 7]}) - current = table.plot_scatterplot("A", "B") - snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_scatterplot.png")) - - # Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm. - assertion = snapshot._image.tobytes() == current._image.tobytes() - assert assertion + scatterplot = table.plot_scatterplot("A", "B") + assert scatterplot == snapshot_png @pytest.mark.parametrize(