Skip to content

Commit

Permalink
Merge pull request #6526 from radarhere/parametrize
Browse files Browse the repository at this point in the history
Parametrized tests
  • Loading branch information
mergify[bot] committed Aug 24, 2022
2 parents 39f5b05 + 683aa90 commit cb6c941
Show file tree
Hide file tree
Showing 18 changed files with 862 additions and 879 deletions.
18 changes: 10 additions & 8 deletions Tests/test_file_apng.py
Expand Up @@ -325,21 +325,23 @@ def open():
pytest.warns(UserWarning, open)


def test_apng_sequence_errors():
test_files = [
@pytest.mark.parametrize(
"test_file",
(
"sequence_start.png",
"sequence_gap.png",
"sequence_repeat.png",
"sequence_repeat_chunk.png",
"sequence_reorder.png",
"sequence_reorder_chunk.png",
"sequence_fdat_fctl.png",
]
for f in test_files:
with pytest.raises(SyntaxError):
with Image.open(f"Tests/images/apng/{f}") as im:
im.seek(im.n_frames - 1)
im.load()
),
)
def test_apng_sequence_errors(test_file):
with pytest.raises(SyntaxError):
with Image.open(f"Tests/images/apng/{test_file}") as im:
im.seek(im.n_frames - 1)
im.load()


def test_apng_save(tmp_path):
Expand Down
132 changes: 67 additions & 65 deletions Tests/test_file_container.py
@@ -1,3 +1,5 @@
import pytest

from PIL import ContainerIO, Image

from .helper import hopper
Expand Down Expand Up @@ -59,89 +61,89 @@ def test_seek_mode_2():
assert container.tell() == 100


def test_read_n0():
@pytest.mark.parametrize("bytesmode", (True, False))
def test_read_n0(bytesmode):
# Arrange
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
container.seek(81)
data = container.read()
# Act
container.seek(81)
data = container.read()

# Assert
if bytesmode:
data = data.decode()
assert data == "7\nThis is line 8\n"
# Assert
if bytesmode:
data = data.decode()
assert data == "7\nThis is line 8\n"


def test_read_n():
@pytest.mark.parametrize("bytesmode", (True, False))
def test_read_n(bytesmode):
# Arrange
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
container.seek(81)
data = container.read(3)
# Act
container.seek(81)
data = container.read(3)

# Assert
if bytesmode:
data = data.decode()
assert data == "7\nT"
# Assert
if bytesmode:
data = data.decode()
assert data == "7\nT"


def test_read_eof():
@pytest.mark.parametrize("bytesmode", (True, False))
def test_read_eof(bytesmode):
# Arrange
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)

# Act
container.seek(100)
data = container.read()
# Act
container.seek(100)
data = container.read()

# Assert
if bytesmode:
data = data.decode()
assert data == ""
# Assert
if bytesmode:
data = data.decode()
assert data == ""


def test_readline():
@pytest.mark.parametrize("bytesmode", (True, False))
def test_readline(bytesmode):
# Arrange
for bytesmode in (True, False):
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)

# Act
data = container.readline()
# Act
data = container.readline()

# Assert
if bytesmode:
data = data.decode()
assert data == "This is line 1\n"
# Assert
if bytesmode:
data = data.decode()
assert data == "This is line 1\n"


def test_readlines():
@pytest.mark.parametrize("bytesmode", (True, False))
def test_readlines(bytesmode):
# Arrange
for bytesmode in (True, False):
expected = [
"This is line 1\n",
"This is line 2\n",
"This is line 3\n",
"This is line 4\n",
"This is line 5\n",
"This is line 6\n",
"This is line 7\n",
"This is line 8\n",
]
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)

# Act
data = container.readlines()

# Assert
if bytesmode:
data = [line.decode() for line in data]
assert data == expected
expected = [
"This is line 1\n",
"This is line 2\n",
"This is line 3\n",
"This is line 4\n",
"This is line 5\n",
"This is line 6\n",
"This is line 7\n",
"This is line 8\n",
]
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)

# Act
data = container.readlines()

# Assert
if bytesmode:
data = [line.decode() for line in data]
assert data == expected
15 changes: 6 additions & 9 deletions Tests/test_file_im.py
Expand Up @@ -78,15 +78,12 @@ def test_eoferror():
im.seek(n_frames - 1)


def test_roundtrip(tmp_path):
def roundtrip(mode):
out = str(tmp_path / "temp.im")
im = hopper(mode)
im.save(out)
assert_image_equal_tofile(im, out)

for mode in ["RGB", "P", "PA"]:
roundtrip(mode)
@pytest.mark.parametrize("mode", ("RGB", "P", "PA"))
def test_roundtrip(mode, tmp_path):
out = str(tmp_path / "temp.im")
im = hopper(mode)
im.save(out)
assert_image_equal_tofile(im, out)


def test_save_unsupported_mode(tmp_path):
Expand Down
82 changes: 41 additions & 41 deletions Tests/test_file_libtiff.py
Expand Up @@ -135,50 +135,50 @@ def test_adobe_deflate_tiff(self):

assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")

def test_write_metadata(self, tmp_path):
@pytest.mark.parametrize("legacy_api", (False, True))
def test_write_metadata(self, legacy_api, tmp_path):
"""Test metadata writing through libtiff"""
for legacy_api in [False, True]:
f = str(tmp_path / "temp.tiff")
with Image.open("Tests/images/hopper_g4.tif") as img:
img.save(f, tiffinfo=img.tag)

if legacy_api:
original = img.tag.named()
else:
original = img.tag_v2.named()

# PhotometricInterpretation is set from SAVE_INFO,
# not the original image.
ignored = [
"StripByteCounts",
"RowsPerStrip",
"PageNumber",
"PhotometricInterpretation",
]

with Image.open(f) as loaded:
if legacy_api:
reloaded = loaded.tag.named()
else:
reloaded = loaded.tag_v2.named()

for tag, value in itertools.chain(reloaded.items(), original.items()):
if tag not in ignored:
val = original[tag]
if tag.endswith("Resolution"):
if legacy_api:
assert val[0][0] / val[0][1] == (
4294967295 / 113653537
), f"{tag} didn't roundtrip"
else:
assert val == 37.79000115940079, f"{tag} didn't roundtrip"
f = str(tmp_path / "temp.tiff")
with Image.open("Tests/images/hopper_g4.tif") as img:
img.save(f, tiffinfo=img.tag)

if legacy_api:
original = img.tag.named()
else:
original = img.tag_v2.named()

# PhotometricInterpretation is set from SAVE_INFO,
# not the original image.
ignored = [
"StripByteCounts",
"RowsPerStrip",
"PageNumber",
"PhotometricInterpretation",
]

with Image.open(f) as loaded:
if legacy_api:
reloaded = loaded.tag.named()
else:
reloaded = loaded.tag_v2.named()

for tag, value in itertools.chain(reloaded.items(), original.items()):
if tag not in ignored:
val = original[tag]
if tag.endswith("Resolution"):
if legacy_api:
assert val[0][0] / val[0][1] == (
4294967295 / 113653537
), f"{tag} didn't roundtrip"
else:
assert val == value, f"{tag} didn't roundtrip"
assert val == 37.79000115940079, f"{tag} didn't roundtrip"
else:
assert val == value, f"{tag} didn't roundtrip"

# https://github.com/python-pillow/Pillow/issues/1561
requested_fields = ["StripByteCounts", "RowsPerStrip", "StripOffsets"]
for field in requested_fields:
assert field in reloaded, f"{field} not in metadata"
# https://github.com/python-pillow/Pillow/issues/1561
requested_fields = ["StripByteCounts", "RowsPerStrip", "StripOffsets"]
for field in requested_fields:
assert field in reloaded, f"{field} not in metadata"

@pytest.mark.valgrind_known_error(reason="Known invalid metadata")
def test_additional_metadata(self, tmp_path):
Expand Down

0 comments on commit cb6c941

Please sign in to comment.