parametrize tests

This commit is contained in:
Yay295 2022-08-24 07:42:51 -05:00 committed by GitHub
parent cb6c94123e
commit f0be6845f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,90 +5,110 @@ from PIL import Image, ImageFilter
from .helper import assert_image_equal, hopper from .helper import assert_image_equal, hopper
def test_sanity(): @pytest.mark.parametrize(
def apply_filter(filter_to_apply): "filter_to_apply",
for mode in ["L", "RGB", "CMYK"]: (
im = hopper(mode) ImageFilter.BLUR,
out = im.filter(filter_to_apply) ImageFilter.CONTOUR,
assert out.mode == im.mode ImageFilter.DETAIL,
assert out.size == im.size ImageFilter.EDGE_ENHANCE,
ImageFilter.EDGE_ENHANCE_MORE,
ImageFilter.EMBOSS,
ImageFilter.FIND_EDGES,
ImageFilter.SMOOTH,
ImageFilter.SMOOTH_MORE,
ImageFilter.SHARPEN,
ImageFilter.MaxFilter,
ImageFilter.MedianFilter,
ImageFilter.MinFilter,
ImageFilter.ModeFilter,
ImageFilter.GaussianBlur,
ImageFilter.GaussianBlur(5),
ImageFilter.BoxBlur(5),
ImageFilter.UnsharpMask,
ImageFilter.UnsharpMask(10),
),
)
@pytest.mark.parametrize("mode", ("L", "RGB", "CMYK"))
def test_sanity(filter_to_apply, mode):
im = hopper(mode)
out = im.filter(filter_to_apply)
assert out.mode == im.mode
assert out.size == im.size
apply_filter(ImageFilter.BLUR)
apply_filter(ImageFilter.CONTOUR)
apply_filter(ImageFilter.DETAIL)
apply_filter(ImageFilter.EDGE_ENHANCE)
apply_filter(ImageFilter.EDGE_ENHANCE_MORE)
apply_filter(ImageFilter.EMBOSS)
apply_filter(ImageFilter.FIND_EDGES)
apply_filter(ImageFilter.SMOOTH)
apply_filter(ImageFilter.SMOOTH_MORE)
apply_filter(ImageFilter.SHARPEN)
apply_filter(ImageFilter.MaxFilter)
apply_filter(ImageFilter.MedianFilter)
apply_filter(ImageFilter.MinFilter)
apply_filter(ImageFilter.ModeFilter)
apply_filter(ImageFilter.GaussianBlur)
apply_filter(ImageFilter.GaussianBlur(5))
apply_filter(ImageFilter.BoxBlur(5))
apply_filter(ImageFilter.UnsharpMask)
apply_filter(ImageFilter.UnsharpMask(10))
@pytest.mark.parametrize("mode", ("L", "RGB", "CMYK"))
def test_sanity_error(mode):
with pytest.raises(TypeError): with pytest.raises(TypeError):
apply_filter("hello") im = hopper(mode)
out = im.filter("hello")
assert out.mode == im.mode
assert out.size == im.size
def test_crash(): # crashes on small images
@pytest.mark.parametrize("size", ((1, 1), (2, 2), (3, 3)))
# crashes on small images def test_crash(size):
im = Image.new("RGB", (1, 1)) im = Image.new("RGB", size)
im.filter(ImageFilter.SMOOTH)
im = Image.new("RGB", (2, 2))
im.filter(ImageFilter.SMOOTH)
im = Image.new("RGB", (3, 3))
im.filter(ImageFilter.SMOOTH) im.filter(ImageFilter.SMOOTH)
def test_modefilter(): @pytest.mark.parametrize(
def modefilter(mode): "mode,expected",
im = Image.new(mode, (3, 3), None) (
im.putdata(list(range(9))) ("1", (4, 0)),
# image is: ("L", (4, 0)),
# 0 1 2 ("P", (4, 0)),
# 3 4 5 ("RGB", ((4, 0, 0), (0, 0, 0))),
# 6 7 8 ),
mod = im.filter(ImageFilter.ModeFilter).getpixel((1, 1)) )
im.putdata([0, 0, 1, 2, 5, 1, 5, 2, 0]) # mode=0 def test_modefilter(mode, expected):
mod2 = im.filter(ImageFilter.ModeFilter).getpixel((1, 1)) im = Image.new(mode, (3, 3), None)
return mod, mod2 im.putdata(list(range(9)))
# image is:
assert modefilter("1") == (4, 0) # 0 1 2
assert modefilter("L") == (4, 0) # 3 4 5
assert modefilter("P") == (4, 0) # 6 7 8
assert modefilter("RGB") == ((4, 0, 0), (0, 0, 0)) mod = im.filter(ImageFilter.ModeFilter).getpixel((1, 1))
im.putdata([0, 0, 1, 2, 5, 1, 5, 2, 0]) # mode=0
mod2 = im.filter(ImageFilter.ModeFilter).getpixel((1, 1))
assert (mod, mod2) == expected
def test_rankfilter(): @pytest.mark.parametrize(
def rankfilter(mode): "mode,expected",
im = Image.new(mode, (3, 3), None) (
im.putdata(list(range(9))) ("1", (0, 4, 8)),
# image is: ("L", (0, 4, 8)),
# 0 1 2 ("RGB", ((0, 0, 0), (4, 0, 0), (8, 0, 0))),
# 3 4 5 ("I", (0, 4, 8)),
# 6 7 8 ("F", (0.0, 4.0, 8.0)),
minimum = im.filter(ImageFilter.MinFilter).getpixel((1, 1)) ),
med = im.filter(ImageFilter.MedianFilter).getpixel((1, 1)) )
maximum = im.filter(ImageFilter.MaxFilter).getpixel((1, 1)) def test_rankfilter(mode, expected):
return minimum, med, maximum im = Image.new(mode, (3, 3), None)
im.putdata(list(range(9)))
# image is:
# 0 1 2
# 3 4 5
# 6 7 8
minimum = im.filter(ImageFilter.MinFilter).getpixel((1, 1))
med = im.filter(ImageFilter.MedianFilter).getpixel((1, 1))
maximum = im.filter(ImageFilter.MaxFilter).getpixel((1, 1))
assert (minimum, med, maximum) == expected
assert rankfilter("1") == (0, 4, 8)
assert rankfilter("L") == (0, 4, 8) def test_rankfilter_error():
with pytest.raises(ValueError): with pytest.raises(ValueError):
rankfilter("P") im = Image.new("P", (3, 3), None)
assert rankfilter("RGB") == ((0, 0, 0), (4, 0, 0), (8, 0, 0)) im.putdata(list(range(9)))
assert rankfilter("I") == (0, 4, 8) # image is:
assert rankfilter("F") == (0.0, 4.0, 8.0) # 0 1 2
# 3 4 5
# 6 7 8
im.filter(ImageFilter.MinFilter).getpixel((1, 1))
im.filter(ImageFilter.MedianFilter).getpixel((1, 1))
im.filter(ImageFilter.MaxFilter).getpixel((1, 1))
def test_rankfilter_properties(): def test_rankfilter_properties():
@ -110,7 +130,8 @@ def test_kernel_not_enough_coefficients():
ImageFilter.Kernel((3, 3), (0, 0)) ImageFilter.Kernel((3, 3), (0, 0))
def test_consistency_3x3(): @pytest.mark.parametrize("mode", ("L", "LA", "RGB", "CMYK"))
def test_consistency_3x3(mode):
with Image.open("Tests/images/hopper.bmp") as source: with Image.open("Tests/images/hopper.bmp") as source:
with Image.open("Tests/images/hopper_emboss.bmp") as reference: with Image.open("Tests/images/hopper_emboss.bmp") as reference:
kernel = ImageFilter.Kernel( kernel = ImageFilter.Kernel(
@ -125,14 +146,14 @@ def test_consistency_3x3():
source = source.split() * 2 source = source.split() * 2
reference = reference.split() * 2 reference = reference.split() * 2
for mode in ["L", "LA", "RGB", "CMYK"]: assert_image_equal(
assert_image_equal( Image.merge(mode, source[: len(mode)]).filter(kernel),
Image.merge(mode, source[: len(mode)]).filter(kernel), Image.merge(mode, reference[: len(mode)]),
Image.merge(mode, reference[: len(mode)]), )
)
def test_consistency_5x5(): @pytest.mark.parametrize("mode", ("L", "LA", "RGB", "CMYK"))
def test_consistency_5x5(mode):
with Image.open("Tests/images/hopper.bmp") as source: with Image.open("Tests/images/hopper.bmp") as source:
with Image.open("Tests/images/hopper_emboss_more.bmp") as reference: with Image.open("Tests/images/hopper_emboss_more.bmp") as reference:
kernel = ImageFilter.Kernel( kernel = ImageFilter.Kernel(
@ -149,8 +170,7 @@ def test_consistency_5x5():
source = source.split() * 2 source = source.split() * 2
reference = reference.split() * 2 reference = reference.split() * 2
for mode in ["L", "LA", "RGB", "CMYK"]: assert_image_equal(
assert_image_equal( Image.merge(mode, source[: len(mode)]).filter(kernel),
Image.merge(mode, source[: len(mode)]).filter(kernel), Image.merge(mode, reference[: len(mode)]),
Image.merge(mode, reference[: len(mode)]), )
)