Parametrize tests (#8838)

Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
This commit is contained in:
Andrew Murray 2025-03-28 23:27:39 +11:00 committed by GitHub
parent 10ccbd7788
commit 1cb6c7c347
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 58 deletions

View File

@ -15,25 +15,19 @@ from .helper import (
)
def test_sanity(tmp_path: Path) -> None:
def roundtrip(im: Image.Image) -> None:
outfile = tmp_path / "temp.bmp"
@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
def test_sanity(mode: str, tmp_path: Path) -> None:
outfile = tmp_path / "temp.bmp"
im.save(outfile, "BMP")
im = hopper(mode)
im.save(outfile, "BMP")
with Image.open(outfile) as reloaded:
reloaded.load()
assert im.mode == reloaded.mode
assert im.size == reloaded.size
assert reloaded.format == "BMP"
assert reloaded.get_format_mimetype() == "image/bmp"
roundtrip(hopper())
roundtrip(hopper("1"))
roundtrip(hopper("L"))
roundtrip(hopper("P"))
roundtrip(hopper("RGB"))
with Image.open(outfile) as reloaded:
reloaded.load()
assert im.mode == reloaded.mode
assert im.size == reloaded.size
assert reloaded.format == "BMP"
assert reloaded.get_format_mimetype() == "image/bmp"
def test_invalid_file() -> None:

View File

@ -71,24 +71,26 @@ def test_invalid_file() -> None:
SgiImagePlugin.SgiImageFile(invalid_file)
def test_write(tmp_path: Path) -> None:
def roundtrip(img: Image.Image) -> None:
out = tmp_path / "temp.sgi"
img.save(out, format="sgi")
def roundtrip(img: Image.Image, tmp_path: Path) -> None:
out = tmp_path / "temp.sgi"
img.save(out, format="sgi")
assert_image_equal_tofile(img, out)
out = tmp_path / "fp.sgi"
with open(out, "wb") as fp:
img.save(fp)
assert_image_equal_tofile(img, out)
out = tmp_path / "fp.sgi"
with open(out, "wb") as fp:
img.save(fp)
assert_image_equal_tofile(img, out)
assert not fp.closed
assert not fp.closed
for mode in ("L", "RGB", "RGBA"):
roundtrip(hopper(mode))
@pytest.mark.parametrize("mode", ("L", "RGB", "RGBA"))
def test_write(mode: str, tmp_path: Path) -> None:
roundtrip(hopper(mode), tmp_path)
# Test 1 dimension for an L mode image
roundtrip(Image.new("L", (10, 1)))
def test_write_L_mode_1_dimension(tmp_path: Path) -> None:
roundtrip(Image.new("L", (10, 1)), tmp_path)
def test_write16(tmp_path: Path) -> None:

View File

@ -1,8 +1,6 @@
from __future__ import annotations
import os
from glob import glob
from itertools import product
from pathlib import Path
import pytest
@ -15,14 +13,27 @@ _TGA_DIR = os.path.join("Tests", "images", "tga")
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
_MODES = ("L", "LA", "P", "RGB", "RGBA")
_ORIGINS = ("tl", "bl")
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
@pytest.mark.parametrize("mode", _MODES)
def test_sanity(mode: str, tmp_path: Path) -> None:
@pytest.mark.parametrize(
"size_mode",
(
("1x1", "L"),
("200x32", "L"),
("200x32", "LA"),
("200x32", "P"),
("200x32", "RGB"),
("200x32", "RGBA"),
),
)
@pytest.mark.parametrize("origin", _ORIGINS)
@pytest.mark.parametrize("rle", (True, False))
def test_sanity(
size_mode: tuple[str, str], origin: str, rle: str, tmp_path: Path
) -> None:
def roundtrip(original_im: Image.Image) -> None:
out = tmp_path / "temp.tga"
@ -36,33 +47,26 @@ def test_sanity(mode: str, tmp_path: Path) -> None:
assert_image_equal(saved_im, original_im)
png_paths = glob(os.path.join(_TGA_DIR_COMMON, f"*x*_{mode.lower()}.png"))
size, mode = size_mode
png_path = os.path.join(_TGA_DIR_COMMON, size + "_" + mode.lower() + ".png")
with Image.open(png_path) as reference_im:
assert reference_im.mode == mode
for png_path in png_paths:
with Image.open(png_path) as reference_im:
assert reference_im.mode == mode
path_no_ext = os.path.splitext(png_path)[0]
tga_path = "{}_{}_{}.tga".format(path_no_ext, origin, "rle" if rle else "raw")
path_no_ext = os.path.splitext(png_path)[0]
for origin, rle in product(_ORIGINS, (True, False)):
tga_path = "{}_{}_{}.tga".format(
path_no_ext, origin, "rle" if rle else "raw"
)
with Image.open(tga_path) as original_im:
assert original_im.format == "TGA"
assert original_im.get_format_mimetype() == "image/x-tga"
if rle:
assert original_im.info["compression"] == "tga_rle"
assert original_im.info["orientation"] == _ORIGIN_TO_ORIENTATION[origin]
if mode == "P":
assert original_im.getpalette() == reference_im.getpalette()
with Image.open(tga_path) as original_im:
assert original_im.format == "TGA"
assert original_im.get_format_mimetype() == "image/x-tga"
if rle:
assert original_im.info["compression"] == "tga_rle"
assert (
original_im.info["orientation"]
== _ORIGIN_TO_ORIENTATION[origin]
)
if mode == "P":
assert original_im.getpalette() == reference_im.getpalette()
assert_image_equal(original_im, reference_im)
assert_image_equal(original_im, reference_im)
roundtrip(original_im)
roundtrip(original_im)
def test_palette_depth_8() -> None: