Skip to content

Commit

Permalink
Merge pull request #6634 from radarhere/parametrized_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Oct 3, 2022
2 parents 285f9cb + 1735872 commit 243402e
Show file tree
Hide file tree
Showing 25 changed files with 350 additions and 534 deletions.
12 changes: 6 additions & 6 deletions Tests/test_features.py
Expand Up @@ -70,14 +70,14 @@ def test_libimagequant_version():
assert re.search(r"\d+\.\d+\.\d+$", features.version("libimagequant"))


def test_check_modules():
for feature in features.modules:
assert features.check_module(feature) in [True, False]
@pytest.mark.parametrize("feature", features.modules)
def test_check_modules(feature):
assert features.check_module(feature) in [True, False]


def test_check_codecs():
for feature in features.codecs:
assert features.check_codec(feature) in [True, False]
@pytest.mark.parametrize("feature", features.codecs)
def test_check_codecs(feature):
assert features.check_codec(feature) in [True, False]


def test_check_warns_on_nonexistent():
Expand Down
13 changes: 6 additions & 7 deletions Tests/test_file_apng.py
Expand Up @@ -39,13 +39,12 @@ def test_apng_basic():
assert im.getpixel((64, 32)) == (0, 255, 0, 255)


def test_apng_fdat():
with Image.open("Tests/images/apng/split_fdat.png") as im:
im.seek(im.n_frames - 1)
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
assert im.getpixel((64, 32)) == (0, 255, 0, 255)

with Image.open("Tests/images/apng/split_fdat_zero_chunk.png") as im:
@pytest.mark.parametrize(
"filename",
("Tests/images/apng/split_fdat.png", "Tests/images/apng/split_fdat_zero_chunk.png"),
)
def test_apng_fdat(filename):
with Image.open(filename) as im:
im.seek(im.n_frames - 1)
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
Expand Down
53 changes: 21 additions & 32 deletions Tests/test_file_eps.py
Expand Up @@ -124,14 +124,6 @@ def test_file_object(tmp_path):
image1.save(fh, "EPS")


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_iobase_object(tmp_path):
# issue 479
with Image.open(FILE1) as image1:
with open(str(tmp_path / "temp_iobase.eps"), "wb") as fh:
image1.save(fh, "EPS")


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_bytesio_object():
with open(FILE1, "rb") as f:
Expand Down Expand Up @@ -203,25 +195,23 @@ def test_render_scale2():


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_resize():
files = [FILE1, FILE2, "Tests/images/illu10_preview.eps"]
for fn in files:
with Image.open(fn) as im:
new_size = (100, 100)
im = im.resize(new_size)
assert im.size == new_size
@pytest.mark.parametrize("filename", (FILE1, FILE2, "Tests/images/illu10_preview.eps"))
def test_resize(filename):
with Image.open(filename) as im:
new_size = (100, 100)
im = im.resize(new_size)
assert im.size == new_size


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_thumbnail():
@pytest.mark.parametrize("filename", (FILE1, FILE2))
def test_thumbnail(filename):
# Issue #619
# Arrange
files = [FILE1, FILE2]
for fn in files:
with Image.open(FILE1) as im:
new_size = (100, 100)
im.thumbnail(new_size)
assert max(im.size) == max(new_size)
with Image.open(filename) as im:
new_size = (100, 100)
im.thumbnail(new_size)
assert max(im.size) == max(new_size)


def test_read_binary_preview():
Expand Down Expand Up @@ -266,20 +256,19 @@ def _test_readline_file_psfile(test_string, ending):
_test_readline_file_psfile(s, ending)


def test_open_eps():
# https://github.com/python-pillow/Pillow/issues/1104
# Arrange
FILES = [
@pytest.mark.parametrize(
"filename",
(
"Tests/images/illu10_no_preview.eps",
"Tests/images/illu10_preview.eps",
"Tests/images/illuCS6_no_preview.eps",
"Tests/images/illuCS6_preview.eps",
]

# Act / Assert
for filename in FILES:
with Image.open(filename) as img:
assert img.mode == "RGB"
),
)
def test_open_eps(filename):
# https://github.com/python-pillow/Pillow/issues/1104
with Image.open(filename) as img:
assert img.mode == "RGB"


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
Expand Down
34 changes: 17 additions & 17 deletions Tests/test_file_gif.py
Expand Up @@ -793,24 +793,24 @@ def test_identical_frames(tmp_path):
assert reread.info["duration"] == 4500


def test_identical_frames_to_single_frame(tmp_path):
for duration in ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500):
out = str(tmp_path / "temp.gif")
im_list = [
Image.new("L", (100, 100), "#000"),
Image.new("L", (100, 100), "#000"),
Image.new("L", (100, 100), "#000"),
]

im_list[0].save(
out, save_all=True, append_images=im_list[1:], duration=duration
)
with Image.open(out) as reread:
# Assert that all frames were combined
assert reread.n_frames == 1
@pytest.mark.parametrize(
"duration", ([1000, 1500, 2000, 4000], (1000, 1500, 2000, 4000), 8500)
)
def test_identical_frames_to_single_frame(duration, tmp_path):
out = str(tmp_path / "temp.gif")
im_list = [
Image.new("L", (100, 100), "#000"),
Image.new("L", (100, 100), "#000"),
Image.new("L", (100, 100), "#000"),
]

# Assert that the new duration is the total of the identical frames
assert reread.info["duration"] == 8500
im_list[0].save(out, save_all=True, append_images=im_list[1:], duration=duration)
with Image.open(out) as reread:
# Assert that all frames were combined
assert reread.n_frames == 1

# Assert that the new duration is the total of the identical frames
assert reread.info["duration"] == 8500


def test_number_of_loops(tmp_path):
Expand Down
61 changes: 32 additions & 29 deletions Tests/test_file_jpeg.py
Expand Up @@ -150,27 +150,30 @@ def test_icc(self, tmp_path):
assert not im1.info.get("icc_profile")
assert im2.info.get("icc_profile")

def test_icc_big(self):
@pytest.mark.parametrize(
"n",
(
0,
1,
3,
4,
5,
65533 - 14, # full JPEG marker block
65533 - 14 + 1, # full block plus one byte
ImageFile.MAXBLOCK, # full buffer block
ImageFile.MAXBLOCK + 1, # full buffer block plus one byte
ImageFile.MAXBLOCK * 4 + 3, # large block
),
)
def test_icc_big(self, n):
# Make sure that the "extra" support handles large blocks
def test(n):
# The ICC APP marker can store 65519 bytes per marker, so
# using a 4-byte test code should allow us to detect out of
# order issues.
icc_profile = (b"Test" * int(n / 4 + 1))[:n]
assert len(icc_profile) == n # sanity
im1 = self.roundtrip(hopper(), icc_profile=icc_profile)
assert im1.info.get("icc_profile") == (icc_profile or None)

test(0)
test(1)
test(3)
test(4)
test(5)
test(65533 - 14) # full JPEG marker block
test(65533 - 14 + 1) # full block plus one byte
test(ImageFile.MAXBLOCK) # full buffer block
test(ImageFile.MAXBLOCK + 1) # full buffer block plus one byte
test(ImageFile.MAXBLOCK * 4 + 3) # large block
# The ICC APP marker can store 65519 bytes per marker, so
# using a 4-byte test code should allow us to detect out of
# order issues.
icc_profile = (b"Test" * int(n / 4 + 1))[:n]
assert len(icc_profile) == n # sanity
im1 = self.roundtrip(hopper(), icc_profile=icc_profile)
assert im1.info.get("icc_profile") == (icc_profile or None)

@mark_if_feature_version(
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
Expand Down Expand Up @@ -649,19 +652,19 @@ def test_bad_mpo_header(self):
# Assert
assert im.format == "JPEG"

def test_save_correct_modes(self):
@pytest.mark.parametrize("mode", ("1", "L", "RGB", "RGBX", "CMYK", "YCbCr"))
def test_save_correct_modes(self, mode):
out = BytesIO()
for mode in ["1", "L", "RGB", "RGBX", "CMYK", "YCbCr"]:
img = Image.new(mode, (20, 20))
img.save(out, "JPEG")
img = Image.new(mode, (20, 20))
img.save(out, "JPEG")

def test_save_wrong_modes(self):
@pytest.mark.parametrize("mode", ("LA", "La", "RGBA", "RGBa", "P"))
def test_save_wrong_modes(self, mode):
# ref https://github.com/python-pillow/Pillow/issues/2005
out = BytesIO()
for mode in ["LA", "La", "RGBA", "RGBa", "P"]:
img = Image.new(mode, (20, 20))
with pytest.raises(OSError):
img.save(out, "JPEG")
img = Image.new(mode, (20, 20))
with pytest.raises(OSError):
img.save(out, "JPEG")

def test_save_tiff_with_dpi(self, tmp_path):
# Arrange
Expand Down
29 changes: 13 additions & 16 deletions Tests/test_file_jpeg2k.py
Expand Up @@ -126,14 +126,14 @@ def test_prog_res_rt():
assert_image_equal(im, test_card)


def test_default_num_resolutions():
for num_resolutions in range(2, 6):
d = 1 << (num_resolutions - 1)
im = test_card.resize((d - 1, d - 1))
with pytest.raises(OSError):
roundtrip(im, num_resolutions=num_resolutions)
reloaded = roundtrip(im)
assert_image_equal(im, reloaded)
@pytest.mark.parametrize("num_resolutions", range(2, 6))
def test_default_num_resolutions(num_resolutions):
d = 1 << (num_resolutions - 1)
im = test_card.resize((d - 1, d - 1))
with pytest.raises(OSError):
roundtrip(im, num_resolutions=num_resolutions)
reloaded = roundtrip(im)
assert_image_equal(im, reloaded)


def test_reduce():
Expand Down Expand Up @@ -266,14 +266,11 @@ def test_rgba():
assert jp2.mode == "RGBA"


def test_16bit_monochrome_has_correct_mode():
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
j2k.load()
assert j2k.mode == "I;16"

with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
jp2.load()
assert jp2.mode == "I;16"
@pytest.mark.parametrize("ext", (".j2k", ".jp2"))
def test_16bit_monochrome_has_correct_mode(ext):
with Image.open("Tests/images/16bit.cropped" + ext) as im:
im.load()
assert im.mode == "I;16"


def test_16bit_monochrome_jp2_like_tiff():
Expand Down
13 changes: 3 additions & 10 deletions Tests/test_file_libtiff.py
Expand Up @@ -509,20 +509,13 @@ def test_palette_save(self, im, tmp_path):
# colormap/palette tag
assert len(reloaded.tag_v2[320]) == 768

def xtest_bw_compression_w_rgb(self, tmp_path):
"""This test passes, but when running all tests causes a failure due
to output on stderr from the error thrown by libtiff. We need to
capture that but not now"""

@pytest.mark.parametrize("compression", ("tiff_ccitt", "group3", "group4"))
def test_bw_compression_w_rgb(self, compression, tmp_path):
im = hopper("RGB")
out = str(tmp_path / "temp.tif")

with pytest.raises(OSError):
im.save(out, compression="tiff_ccitt")
with pytest.raises(OSError):
im.save(out, compression="group3")
with pytest.raises(OSError):
im.save(out, compression="group4")
im.save(out, compression=compression)

def test_fp_leak(self):
im = Image.open("Tests/images/hopper_g4_500.tif")
Expand Down
16 changes: 2 additions & 14 deletions Tests/test_file_palm.py
Expand Up @@ -63,19 +63,7 @@ def test_p_mode(tmp_path):
roundtrip(tmp_path, mode)


def test_l_oserror(tmp_path):
# Arrange
mode = "L"

# Act / Assert
with pytest.raises(OSError):
helper_save_as_palm(tmp_path, mode)


def test_rgb_oserror(tmp_path):
# Arrange
mode = "RGB"

# Act / Assert
@pytest.mark.parametrize("mode", ("L", "RGB"))
def test_oserror(tmp_path, mode):
with pytest.raises(OSError):
helper_save_as_palm(tmp_path, mode)
10 changes: 5 additions & 5 deletions Tests/test_file_pcx.py
Expand Up @@ -39,14 +39,14 @@ def test_invalid_file():
PcxImagePlugin.PcxImageFile(invalid_file)


def test_odd(tmp_path):
@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
def test_odd(tmp_path, mode):
# See issue #523, odd sized images should have a stride that's even.
# Not that ImageMagick or GIMP write PCX that way.
# We were not handling properly.
for mode in ("1", "L", "P", "RGB"):
# larger, odd sized images are better here to ensure that
# we handle interrupted scan lines properly.
_roundtrip(tmp_path, hopper(mode).resize((511, 511)))
# larger, odd sized images are better here to ensure that
# we handle interrupted scan lines properly.
_roundtrip(tmp_path, hopper(mode).resize((511, 511)))


def test_odd_read():
Expand Down

0 comments on commit 243402e

Please sign in to comment.