Skip to content

Commit

Permalink
Merge pull request #4482 from radarhere/pytest
Browse files Browse the repository at this point in the history
Removed use of PillowTestCase in various tests
  • Loading branch information
hugovk committed Mar 22, 2020
2 parents 7028de7 + 7ff2db1 commit 291f1eb
Show file tree
Hide file tree
Showing 14 changed files with 1,355 additions and 1,301 deletions.
455 changes: 234 additions & 221 deletions Tests/test_file_pdf.py

Large diffs are not rendered by default.

328 changes: 163 additions & 165 deletions Tests/test_file_tga.py
Expand Up @@ -5,204 +5,202 @@
import pytest
from PIL import Image

from .helper import PillowTestCase, assert_image_equal, hopper
from .helper import assert_image_equal, hopper

_TGA_DIR = os.path.join("Tests", "images", "tga")
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")


class TestFileTga(PillowTestCase):
_MODES = ("L", "LA", "P", "RGB", "RGBA")
_ORIGINS = ("tl", "bl")

_MODES = ("L", "LA", "P", "RGB", "RGBA")
_ORIGINS = ("tl", "bl")
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}

_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}

def test_sanity(self):
for mode in self._MODES:
png_paths = glob(
os.path.join(_TGA_DIR_COMMON, "*x*_{}.png".format(mode.lower()))
)
def test_sanity(tmp_path):
for mode in _MODES:

for png_path in png_paths:
with Image.open(png_path) as reference_im:
assert reference_im.mode == mode
def roundtrip(original_im):
out = str(tmp_path / "temp.tga")

path_no_ext = os.path.splitext(png_path)[0]
for origin, rle in product(self._ORIGINS, (True, False)):
tga_path = "{}_{}_{}.tga".format(
path_no_ext, origin, "rle" if rle else "raw"
)
original_im.save(out, rle=rle)
with Image.open(out) as saved_im:
if rle:
assert (
saved_im.info["compression"] == original_im.info["compression"]
)
assert saved_im.info["orientation"] == original_im.info["orientation"]
if mode == "P":
assert saved_im.getpalette() == original_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"]
== self._ORIGIN_TO_ORIENTATION[origin]
)
if mode == "P":
assert (
original_im.getpalette()
== reference_im.getpalette()
)

assert_image_equal(original_im, reference_im)

# Generate a new test name every time so the
# test will not fail with permission error
# on Windows.
out = self.tempfile("temp.tga")

original_im.save(out, rle=rle)
with Image.open(out) as saved_im:
if rle:
assert (
saved_im.info["compression"]
== original_im.info["compression"]
)
assert (
saved_im.info["orientation"]
== original_im.info["orientation"]
)
if mode == "P":
assert (
saved_im.getpalette()
== original_im.getpalette()
)

assert_image_equal(saved_im, original_im)

def test_id_field(self):
# tga file with id field
test_file = "Tests/images/tga_id_field.tga"

# Act
with Image.open(test_file) as im:

# Assert
assert im.size == (100, 100)

def test_id_field_rle(self):
# tga file with id field
test_file = "Tests/images/rgb32rle.tga"

# Act
with Image.open(test_file) as im:

# Assert
assert im.size == (199, 199)

def test_save(self):
test_file = "Tests/images/tga_id_field.tga"
with Image.open(test_file) as im:
out = self.tempfile("temp.tga")

# Save
im.save(out)
with Image.open(out) as test_im:
assert test_im.size == (100, 100)
assert test_im.info["id_section"] == im.info["id_section"]

# RGBA save
im.convert("RGBA").save(out)
with Image.open(out) as test_im:
assert test_im.size == (100, 100)
assert_image_equal(saved_im, original_im)

def test_save_wrong_mode(self):
im = hopper("PA")
out = self.tempfile("temp.tga")
png_paths = glob(
os.path.join(_TGA_DIR_COMMON, "*x*_{}.png".format(mode.lower()))
)

with pytest.raises(OSError):
im.save(out)
for png_path in png_paths:
with Image.open(png_path) as reference_im:
assert reference_im.mode == mode

def test_save_id_section(self):
test_file = "Tests/images/rgb32rle.tga"
with Image.open(test_file) as im:
out = self.tempfile("temp.tga")
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"
)

# Check there is no id section
im.save(out)
with Image.open(out) as test_im:
assert "id_section" not in test_im.info
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()

# Save with custom id section
im.save(out, id_section=b"Test content")
with Image.open(out) as test_im:
assert test_im.info["id_section"] == b"Test content"
assert_image_equal(original_im, reference_im)

# Save with custom id section greater than 255 characters
id_section = b"Test content" * 25
pytest.warns(UserWarning, lambda: im.save(out, id_section=id_section))
with Image.open(out) as test_im:
assert test_im.info["id_section"] == id_section[:255]
roundtrip(original_im)

test_file = "Tests/images/tga_id_field.tga"
with Image.open(test_file) as im:

# Save with no id section
im.save(out, id_section="")
with Image.open(out) as test_im:
assert "id_section" not in test_im.info
def test_id_field():
# tga file with id field
test_file = "Tests/images/tga_id_field.tga"

def test_save_orientation(self):
test_file = "Tests/images/rgb32rle.tga"
out = self.tempfile("temp.tga")
with Image.open(test_file) as im:
assert im.info["orientation"] == -1
# Act
with Image.open(test_file) as im:

im.save(out, orientation=1)
with Image.open(out) as test_im:
assert test_im.info["orientation"] == 1
# Assert
assert im.size == (100, 100)

def test_save_rle(self):
test_file = "Tests/images/rgb32rle.tga"
with Image.open(test_file) as im:
assert im.info["compression"] == "tga_rle"

out = self.tempfile("temp.tga")
def test_id_field_rle():
# tga file with id field
test_file = "Tests/images/rgb32rle.tga"

# Save
im.save(out)
with Image.open(out) as test_im:
assert test_im.size == (199, 199)
assert test_im.info["compression"] == "tga_rle"
# Act
with Image.open(test_file) as im:

# Save without compression
im.save(out, compression=None)
# Assert
assert im.size == (199, 199)


def test_save(tmp_path):
test_file = "Tests/images/tga_id_field.tga"
with Image.open(test_file) as im:
out = str(tmp_path / "temp.tga")

# Save
im.save(out)
with Image.open(out) as test_im:
assert "compression" not in test_im.info
assert test_im.size == (100, 100)
assert test_im.info["id_section"] == im.info["id_section"]

# RGBA save
im.convert("RGBA").save(out)
with Image.open(out) as test_im:
assert test_im.size == (199, 199)
with Image.open(out) as test_im:
assert test_im.size == (100, 100)

test_file = "Tests/images/tga_id_field.tga"
with Image.open(test_file) as im:
assert "compression" not in im.info

# Save with compression
im.save(out, compression="tga_rle")
with Image.open(out) as test_im:
assert test_im.info["compression"] == "tga_rle"
def test_save_wrong_mode(tmp_path):
im = hopper("PA")
out = str(tmp_path / "temp.tga")

def test_save_l_transparency(self):
# There are 559 transparent pixels in la.tga.
num_transparent = 559
with pytest.raises(OSError):
im.save(out)

in_file = "Tests/images/la.tga"
with Image.open(in_file) as im:
assert im.mode == "LA"
assert im.getchannel("A").getcolors()[0][0] == num_transparent

out = self.tempfile("temp.tga")
im.save(out)
def test_save_id_section(tmp_path):
test_file = "Tests/images/rgb32rle.tga"
with Image.open(test_file) as im:
out = str(tmp_path / "temp.tga")

with Image.open(out) as test_im:
assert test_im.mode == "LA"
assert test_im.getchannel("A").getcolors()[0][0] == num_transparent
# Check there is no id section
im.save(out)
with Image.open(out) as test_im:
assert "id_section" not in test_im.info

# Save with custom id section
im.save(out, id_section=b"Test content")
with Image.open(out) as test_im:
assert test_im.info["id_section"] == b"Test content"

# Save with custom id section greater than 255 characters
id_section = b"Test content" * 25
pytest.warns(UserWarning, lambda: im.save(out, id_section=id_section))
with Image.open(out) as test_im:
assert test_im.info["id_section"] == id_section[:255]

test_file = "Tests/images/tga_id_field.tga"
with Image.open(test_file) as im:

# Save with no id section
im.save(out, id_section="")
with Image.open(out) as test_im:
assert "id_section" not in test_im.info


def test_save_orientation(tmp_path):
test_file = "Tests/images/rgb32rle.tga"
out = str(tmp_path / "temp.tga")
with Image.open(test_file) as im:
assert im.info["orientation"] == -1

im.save(out, orientation=1)
with Image.open(out) as test_im:
assert test_im.info["orientation"] == 1


def test_save_rle(tmp_path):
test_file = "Tests/images/rgb32rle.tga"
with Image.open(test_file) as im:
assert im.info["compression"] == "tga_rle"

out = str(tmp_path / "temp.tga")

# Save
im.save(out)
with Image.open(out) as test_im:
assert test_im.size == (199, 199)
assert test_im.info["compression"] == "tga_rle"

# Save without compression
im.save(out, compression=None)
with Image.open(out) as test_im:
assert "compression" not in test_im.info

# RGBA save
im.convert("RGBA").save(out)
with Image.open(out) as test_im:
assert test_im.size == (199, 199)

test_file = "Tests/images/tga_id_field.tga"
with Image.open(test_file) as im:
assert "compression" not in im.info

# Save with compression
im.save(out, compression="tga_rle")
with Image.open(out) as test_im:
assert test_im.info["compression"] == "tga_rle"


def test_save_l_transparency(tmp_path):
# There are 559 transparent pixels in la.tga.
num_transparent = 559

in_file = "Tests/images/la.tga"
with Image.open(in_file) as im:
assert im.mode == "LA"
assert im.getchannel("A").getcolors()[0][0] == num_transparent

out = str(tmp_path / "temp.tga")
im.save(out)

with Image.open(out) as test_im:
assert test_im.mode == "LA"
assert test_im.getchannel("A").getcolors()[0][0] == num_transparent

assert_image_equal(im, test_im)
assert_image_equal(im, test_im)

0 comments on commit 291f1eb

Please sign in to comment.