Skip to content

Commit

Permalink
Merge pull request #4443 from radarhere/assert
Browse files Browse the repository at this point in the history
Converted most assert statements to pytest
  • Loading branch information
hugovk committed Feb 22, 2020
2 parents 20d6b62 + 8482919 commit dab94e6
Show file tree
Hide file tree
Showing 53 changed files with 1,717 additions and 1,670 deletions.
3 changes: 2 additions & 1 deletion Tests/check_libtiff_segfault.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

import pytest
from PIL import Image

from .helper import PillowTestCase
Expand All @@ -13,7 +14,7 @@ def test_segfault(self):
libtiff >= 4.0.0
"""

with self.assertRaises(IOError):
with pytest.raises(IOError):
with Image.open(TEST_FILE) as im:
im.load()

Expand Down
14 changes: 6 additions & 8 deletions Tests/check_png_dos.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ def test_ignore_dos_text(self):
ImageFile.LOAD_TRUNCATED_IMAGES = False

for s in im.text.values():
self.assertLess(len(s), 1024 * 1024, "Text chunk larger than 1M")
assert len(s) < 1024 * 1024, "Text chunk larger than 1M"

for s in im.info.values():
self.assertLess(len(s), 1024 * 1024, "Text chunk larger than 1M")
assert len(s) < 1024 * 1024, "Text chunk larger than 1M"

def test_dos_text(self):

try:
im = Image.open(TEST_FILE)
im.load()
except ValueError as msg:
self.assertTrue(msg, "Decompressed Data Too Large")
assert msg, "Decompressed Data Too Large"
return

for s in im.text.values():
self.assertLess(len(s), 1024 * 1024, "Text chunk larger than 1M")
assert len(s) < 1024 * 1024, "Text chunk larger than 1M"

def test_dos_total_memory(self):
im = Image.new("L", (1, 1))
Expand All @@ -54,15 +54,13 @@ def test_dos_total_memory(self):
try:
im2 = Image.open(b)
except ValueError as msg:
self.assertIn("Too much memory", msg)
assert "Too much memory" in msg
return

total_len = 0
for txt in im2.text.values():
total_len += len(txt)
self.assertLess(
total_len, 64 * 1024 * 1024, "Total text chunks greater than 64M"
)
assert total_len < 64 * 1024 * 1024, "Total text chunks greater than 64M"


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion Tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _test_leak(self, core):
core()
mem = self._get_mem_usage() - start_mem
msg = "memory usage limit exceeded in iteration %d" % cycle
self.assertLess(mem, self.mem_limit, msg)
assert mem < self.mem_limit, msg


# helpers
Expand Down
154 changes: 77 additions & 77 deletions Tests/test_color_lut.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from array import array

import pytest
from PIL import Image, ImageFilter

from .helper import PillowTestCase, assert_image_equal
Expand Down Expand Up @@ -74,10 +75,10 @@ def test_wrong_args(self):
with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"):
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 9)

with self.assertRaises(TypeError):
with pytest.raises(TypeError):
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, [0, 0, "0"] * 8)

with self.assertRaises(TypeError):
with pytest.raises(TypeError):
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, 16)

def test_correct_args(self):
Expand Down Expand Up @@ -240,14 +241,14 @@ def test_overflow(self):
-1, 2, 2, 2, 2, 2,
])).load()
# fmt: on
self.assertEqual(transformed[0, 0], (0, 0, 255))
self.assertEqual(transformed[50, 50], (0, 0, 255))
self.assertEqual(transformed[255, 0], (0, 255, 255))
self.assertEqual(transformed[205, 50], (0, 255, 255))
self.assertEqual(transformed[0, 255], (255, 0, 0))
self.assertEqual(transformed[50, 205], (255, 0, 0))
self.assertEqual(transformed[255, 255], (255, 255, 0))
self.assertEqual(transformed[205, 205], (255, 255, 0))
assert transformed[0, 0] == (0, 0, 255)
assert transformed[50, 50] == (0, 0, 255)
assert transformed[255, 0] == (0, 255, 255)
assert transformed[205, 50] == (0, 255, 255)
assert transformed[0, 255] == (255, 0, 0)
assert transformed[50, 205] == (255, 0, 0)
assert transformed[255, 255] == (255, 255, 0)
assert transformed[205, 205] == (255, 255, 0)

# fmt: off
transformed = im._new(im.im.color_lut_3d('RGB', Image.LINEAR,
Expand All @@ -260,14 +261,14 @@ def test_overflow(self):
-3, 5, 5, 5, 5, 5,
])).load()
# fmt: on
self.assertEqual(transformed[0, 0], (0, 0, 255))
self.assertEqual(transformed[50, 50], (0, 0, 255))
self.assertEqual(transformed[255, 0], (0, 255, 255))
self.assertEqual(transformed[205, 50], (0, 255, 255))
self.assertEqual(transformed[0, 255], (255, 0, 0))
self.assertEqual(transformed[50, 205], (255, 0, 0))
self.assertEqual(transformed[255, 255], (255, 255, 0))
self.assertEqual(transformed[205, 205], (255, 255, 0))
assert transformed[0, 0] == (0, 0, 255)
assert transformed[50, 50] == (0, 0, 255)
assert transformed[255, 0] == (0, 255, 255)
assert transformed[205, 50] == (0, 255, 255)
assert transformed[0, 255] == (255, 0, 0)
assert transformed[50, 205] == (255, 0, 0)
assert transformed[255, 255] == (255, 255, 0)
assert transformed[205, 205] == (255, 255, 0)


class TestColorLut3DFilter(PillowTestCase):
Expand Down Expand Up @@ -301,20 +302,20 @@ def test_wrong_args(self):

def test_convert_table(self):
lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)
self.assertEqual(tuple(lut.size), (2, 2, 2))
self.assertEqual(lut.name, "Color 3D LUT")
assert tuple(lut.size) == (2, 2, 2)
assert lut.name == "Color 3D LUT"

# fmt: off
lut = ImageFilter.Color3DLUT((2, 2, 2), [
(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11),
(12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23)])
# fmt: on
self.assertEqual(tuple(lut.size), (2, 2, 2))
self.assertEqual(lut.table, list(range(24)))
assert tuple(lut.size) == (2, 2, 2)
assert lut.table == list(range(24))

lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8, channels=4)
self.assertEqual(tuple(lut.size), (2, 2, 2))
self.assertEqual(lut.table, list(range(4)) * 8)
assert tuple(lut.size) == (2, 2, 2)
assert lut.table == list(range(4)) * 8

@unittest.skipIf(numpy is None, "Numpy is not installed")
def test_numpy_sources(self):
Expand All @@ -324,30 +325,30 @@ def test_numpy_sources(self):

table = numpy.ones((7, 6, 5, 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
self.assertIsInstance(lut.table, numpy.ndarray)
self.assertEqual(lut.table.dtype, table.dtype)
self.assertEqual(lut.table.shape, (table.size,))
assert isinstance(lut.table, numpy.ndarray)
assert lut.table.dtype == table.dtype
assert lut.table.shape == (table.size,)

table = numpy.ones((7 * 6 * 5, 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
self.assertEqual(lut.table.shape, (table.size,))
assert lut.table.shape == (table.size,)

table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table)
self.assertEqual(lut.table.shape, (table.size,))
assert lut.table.shape == (table.size,)

# Check application
Image.new("RGB", (10, 10), 0).filter(lut)

# Check copy
table[0] = 33
self.assertEqual(lut.table[0], 1)
assert lut.table[0] == 1

# Check not copy
table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16)
lut = ImageFilter.Color3DLUT((5, 6, 7), table, _copy_table=False)
table[0] = 33
self.assertEqual(lut.table[0], 33)
assert lut.table[0] == 33

@unittest.skipIf(numpy is None, "Numpy is not installed")
def test_numpy_formats(self):
Expand Down Expand Up @@ -386,7 +387,7 @@ def test_numpy_formats(self):

def test_repr(self):
lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)
self.assertEqual(repr(lut), "<Color3DLUT from list size=2x2x2 channels=3>")
assert repr(lut) == "<Color3DLUT from list size=2x2x2 channels=3>"

lut = ImageFilter.Color3DLUT(
(3, 4, 5),
Expand All @@ -395,8 +396,9 @@ def test_repr(self):
target_mode="YCbCr",
_copy_table=False,
)
self.assertEqual(
repr(lut), "<Color3DLUT from array size=3x4x5 channels=4 target_mode=YCbCr>"
assert (
repr(lut)
== "<Color3DLUT from array size=3x4x5 channels=4 target_mode=YCbCr>"
)


Expand All @@ -417,25 +419,25 @@ def test_wrong_channels_count(self):

def test_3_channels(self):
lut = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
self.assertEqual(tuple(lut.size), (5, 5, 5))
self.assertEqual(lut.name, "Color 3D LUT")
assert tuple(lut.size) == (5, 5, 5)
assert lut.name == "Color 3D LUT"
# fmt: off
self.assertEqual(lut.table[:24], [
assert lut.table[:24] == [
0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 0.5, 0.0, 0.0, 0.75, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.25, 0.25, 0.0, 0.5, 0.25, 0.0])
1.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.25, 0.25, 0.0, 0.5, 0.25, 0.0]
# fmt: on

def test_4_channels(self):
lut = ImageFilter.Color3DLUT.generate(
5, channels=4, callback=lambda r, g, b: (b, r, g, (r + g + b) / 2)
)
self.assertEqual(tuple(lut.size), (5, 5, 5))
self.assertEqual(lut.name, "Color 3D LUT")
assert tuple(lut.size) == (5, 5, 5)
assert lut.name == "Color 3D LUT"
# fmt: off
self.assertEqual(lut.table[:24], [
assert lut.table[:24] == [
0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.125, 0.0, 0.5, 0.0, 0.25,
0.0, 0.75, 0.0, 0.375, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 0.25, 0.125
])
]
# fmt: on

def test_apply(self):
Expand All @@ -445,7 +447,7 @@ def test_apply(self):
im = Image.merge(
"RGB", [g, g.transpose(Image.ROTATE_90), g.transpose(Image.ROTATE_180)]
)
self.assertEqual(im, im.filter(lut))
assert im == im.filter(lut)


class TestTransformColorLut3D(PillowTestCase):
Expand All @@ -461,7 +463,7 @@ def test_wrong_args(self):
with self.assertRaisesRegex(ValueError, "should have either channels"):
source.transform(lambda r, g, b: (r, g, b, 1))

with self.assertRaises(TypeError):
with pytest.raises(TypeError):
source.transform(lambda r, g, b, a: (r, g, b))

def test_target_mode(self):
Expand All @@ -470,31 +472,29 @@ def test_target_mode(self):
)

lut = source.transform(lambda r, g, b: (r, g, b))
self.assertEqual(lut.mode, "HSV")
assert lut.mode == "HSV"

lut = source.transform(lambda r, g, b: (r, g, b), target_mode="RGB")
self.assertEqual(lut.mode, "RGB")
assert lut.mode == "RGB"

def test_3_to_3_channels(self):
source = ImageFilter.Color3DLUT.generate((3, 4, 5), lambda r, g, b: (r, g, b))
lut = source.transform(lambda r, g, b: (r * r, g * g, b * b))
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
self.assertEqual(
lut.table[0:10], [0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
)
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) == len(source.table)
assert lut.table != source.table
assert lut.table[0:10] == [0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]

def test_3_to_4_channels(self):
source = ImageFilter.Color3DLUT.generate((6, 5, 4), lambda r, g, b: (r, g, b))
lut = source.transform(lambda r, g, b: (r * r, g * g, b * b, 1), channels=4)
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertNotEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) != len(source.table)
assert lut.table != source.table
# fmt: off
self.assertEqual(lut.table[0:16], [
assert lut.table[0:16] == [
0.0, 0.0, 0.0, 1, 0.2**2, 0.0, 0.0, 1,
0.4**2, 0.0, 0.0, 1, 0.6**2, 0.0, 0.0, 1])
0.4**2, 0.0, 0.0, 1, 0.6**2, 0.0, 0.0, 1]
# fmt: on

def test_4_to_3_channels(self):
Expand All @@ -504,27 +504,27 @@ def test_4_to_3_channels(self):
lut = source.transform(
lambda r, g, b, a: (a - r * r, a - g * g, a - b * b), channels=3
)
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertNotEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) != len(source.table)
assert lut.table != source.table
# fmt: off
self.assertEqual(lut.table[0:18], [
assert lut.table[0:18] == [
1.0, 1.0, 1.0, 0.75, 1.0, 1.0, 0.0, 1.0, 1.0,
1.0, 0.96, 1.0, 0.75, 0.96, 1.0, 0.0, 0.96, 1.0])
1.0, 0.96, 1.0, 0.75, 0.96, 1.0, 0.0, 0.96, 1.0]
# fmt: on

def test_4_to_4_channels(self):
source = ImageFilter.Color3DLUT.generate(
(6, 5, 4), lambda r, g, b: (r, g, b, 1), channels=4
)
lut = source.transform(lambda r, g, b, a: (r * r, g * g, b * b, a - 0.5))
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) == len(source.table)
assert lut.table != source.table
# fmt: off
self.assertEqual(lut.table[0:16], [
assert lut.table[0:16] == [
0.0, 0.0, 0.0, 0.5, 0.2**2, 0.0, 0.0, 0.5,
0.4**2, 0.0, 0.0, 0.5, 0.6**2, 0.0, 0.0, 0.5])
0.4**2, 0.0, 0.0, 0.5, 0.6**2, 0.0, 0.0, 0.5]
# fmt: on

def test_with_normals_3_channels(self):
Expand All @@ -534,13 +534,13 @@ def test_with_normals_3_channels(self):
lut = source.transform(
lambda nr, ng, nb, r, g, b: (nr - r, ng - g, nb - b), with_normals=True
)
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) == len(source.table)
assert lut.table != source.table
# fmt: off
self.assertEqual(lut.table[0:18], [
assert lut.table[0:18] == [
0.0, 0.0, 0.0, 0.16, 0.0, 0.0, 0.24, 0.0, 0.0,
0.24, 0.0, 0.0, 0.8 - (0.8**2), 0, 0, 0, 0, 0])
0.24, 0.0, 0.0, 0.8 - (0.8**2), 0, 0, 0, 0, 0]
# fmt: on

def test_with_normals_4_channels(self):
Expand All @@ -551,11 +551,11 @@ def test_with_normals_4_channels(self):
lambda nr, ng, nb, r, g, b, a: (nr - r, ng - g, nb - b, a - 0.5),
with_normals=True,
)
self.assertEqual(tuple(lut.size), tuple(source.size))
self.assertEqual(len(lut.table), len(source.table))
self.assertNotEqual(lut.table, source.table)
assert tuple(lut.size) == tuple(source.size)
assert len(lut.table) == len(source.table)
assert lut.table != source.table
# fmt: off
self.assertEqual(lut.table[0:16], [
assert lut.table[0:16] == [
0.0, 0.0, 0.0, 0.5, 0.25, 0.0, 0.0, 0.5,
0.0, 0.0, 0.0, 0.5, 0.0, 0.16, 0.0, 0.5])
0.0, 0.0, 0.0, 0.5, 0.0, 0.16, 0.0, 0.5]
# fmt: on

0 comments on commit dab94e6

Please sign in to comment.