Skip to content

Commit

Permalink
Merge pull request #4446 from hugovk/convert-asserts
Browse files Browse the repository at this point in the history
Convert some tests to pytest style
  • Loading branch information
radarhere committed Feb 23, 2020
2 parents dab94e6 + 12f66f4 commit de179eb
Show file tree
Hide file tree
Showing 19 changed files with 1,386 additions and 1,313 deletions.
2 changes: 1 addition & 1 deletion Tests/bench_cffi_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_direct(self):
caccess = im.im.pixel_access(False)
access = PyAccess.new(im, False)

self.assertEqual(caccess[(0, 0)], access[(0, 0)])
assert caccess[(0, 0)] == access[(0, 0)]

print("Size: %sx%s" % im.size)
timer(iterate_get, "PyAccess - get", im.size, access)
Expand Down
2 changes: 1 addition & 1 deletion Tests/check_imaging_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _test_leak(self, min_iterations, max_iterations, fn, *args, **kwargs):
mem_limit = mem + 1
continue
msg = "memory usage limit exceeded after %d iterations" % (i + 1)
self.assertLessEqual(mem, mem_limit, msg)
assert mem <= mem_limit, msg

def test_leak_putdata(self):
im = Image.new("RGB", (25, 25))
Expand Down
79 changes: 39 additions & 40 deletions Tests/test_color_lut.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import unittest
from array import array

import pytest
from PIL import Image, ImageFilter

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

try:
import numpy
except ImportError:
numpy = None


class TestColorLut3DCoreAPI(PillowTestCase):
class TestColorLut3DCoreAPI:
def generate_identity_table(self, channels, size):
if isinstance(size, tuple):
size1D, size2D, size3D = size
Expand Down Expand Up @@ -42,37 +41,37 @@ def generate_identity_table(self, channels, size):
def test_wrong_args(self):
im = Image.new("RGB", (10, 10), 0)

with self.assertRaisesRegex(ValueError, "filter"):
with pytest.raises(ValueError, match="filter"):
im.im.color_lut_3d("RGB", Image.CUBIC, *self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "image mode"):
with pytest.raises(ValueError, match="image mode"):
im.im.color_lut_3d(
"wrong", Image.LINEAR, *self.generate_identity_table(3, 3)
)

with self.assertRaisesRegex(ValueError, "table_channels"):
with pytest.raises(ValueError, match="table_channels"):
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(5, 3))

with self.assertRaisesRegex(ValueError, "table_channels"):
with pytest.raises(ValueError, match="table_channels"):
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(1, 3))

with self.assertRaisesRegex(ValueError, "table_channels"):
with pytest.raises(ValueError, match="table_channels"):
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(2, 3))

with self.assertRaisesRegex(ValueError, "Table size"):
with pytest.raises(ValueError, match="Table size"):
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (1, 3, 3))
)

with self.assertRaisesRegex(ValueError, "Table size"):
with pytest.raises(ValueError, match="Table size"):
im.im.color_lut_3d(
"RGB", Image.LINEAR, *self.generate_identity_table(3, (66, 3, 3))
)

with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"):
with pytest.raises(ValueError, match=r"size1D \* size2D \* size3D"):
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 7)

with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"):
with pytest.raises(ValueError, match=r"size1D \* size2D \* size3D"):
im.im.color_lut_3d("RGB", Image.LINEAR, 3, 2, 2, 2, [0, 0, 0] * 9)

with pytest.raises(TypeError):
Expand Down Expand Up @@ -105,25 +104,25 @@ def test_correct_args(self):
)

def test_wrong_mode(self):
with self.assertRaisesRegex(ValueError, "wrong mode"):
with pytest.raises(ValueError, match="wrong mode"):
im = Image.new("L", (10, 10), 0)
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "wrong mode"):
with pytest.raises(ValueError, match="wrong mode"):
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d("L", Image.LINEAR, *self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "wrong mode"):
with pytest.raises(ValueError, match="wrong mode"):
im = Image.new("L", (10, 10), 0)
im.im.color_lut_3d("L", Image.LINEAR, *self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "wrong mode"):
with pytest.raises(ValueError, match="wrong mode"):
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d(
"RGBA", Image.LINEAR, *self.generate_identity_table(3, 3)
)

with self.assertRaisesRegex(ValueError, "wrong mode"):
with pytest.raises(ValueError, match="wrong mode"):
im = Image.new("RGB", (10, 10), 0)
im.im.color_lut_3d("RGB", Image.LINEAR, *self.generate_identity_table(4, 3))

Expand Down Expand Up @@ -271,33 +270,33 @@ def test_overflow(self):
assert transformed[205, 205] == (255, 255, 0)


class TestColorLut3DFilter(PillowTestCase):
class TestColorLut3DFilter:
def test_wrong_args(self):
with self.assertRaisesRegex(ValueError, "should be either an integer"):
with pytest.raises(ValueError, match="should be either an integer"):
ImageFilter.Color3DLUT("small", [1])

with self.assertRaisesRegex(ValueError, "should be either an integer"):
with pytest.raises(ValueError, match="should be either an integer"):
ImageFilter.Color3DLUT((11, 11), [1])

with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"):
with pytest.raises(ValueError, match=r"in \[2, 65\] range"):
ImageFilter.Color3DLUT((11, 11, 1), [1])

with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"):
with pytest.raises(ValueError, match=r"in \[2, 65\] range"):
ImageFilter.Color3DLUT((11, 11, 66), [1])

with self.assertRaisesRegex(ValueError, "table should have .+ items"):
with pytest.raises(ValueError, match="table should have .+ items"):
ImageFilter.Color3DLUT((3, 3, 3), [1, 1, 1])

with self.assertRaisesRegex(ValueError, "table should have .+ items"):
with pytest.raises(ValueError, match="table should have .+ items"):
ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 2)

with self.assertRaisesRegex(ValueError, "should have a length of 4"):
with pytest.raises(ValueError, match="should have a length of 4"):
ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 27, channels=4)

with self.assertRaisesRegex(ValueError, "should have a length of 3"):
with pytest.raises(ValueError, match="should have a length of 3"):
ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8)

with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"):
with pytest.raises(ValueError, match="Only 3 or 4 output"):
ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8, channels=2)

def test_convert_table(self):
Expand All @@ -317,10 +316,10 @@ def test_convert_table(self):
assert tuple(lut.size) == (2, 2, 2)
assert lut.table == list(range(4)) * 8

@unittest.skipIf(numpy is None, "Numpy is not installed")
@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
def test_numpy_sources(self):
table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16)
with self.assertRaisesRegex(ValueError, "should have either channels"):
with pytest.raises(ValueError, match="should have either channels"):
lut = ImageFilter.Color3DLUT((5, 6, 7), table)

table = numpy.ones((7, 6, 5, 3), dtype=numpy.float16)
Expand Down Expand Up @@ -350,7 +349,7 @@ def test_numpy_sources(self):
table[0] = 33
assert lut.table[0] == 33

@unittest.skipIf(numpy is None, "Numpy is not installed")
@pytest.mark.skipif(numpy is None, reason="NumPy not installed")
def test_numpy_formats(self):
g = Image.linear_gradient("L")
im = Image.merge(
Expand All @@ -359,12 +358,12 @@ def test_numpy_formats(self):

lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
lut.table = numpy.array(lut.table, dtype=numpy.float32)[:-1]
with self.assertRaisesRegex(ValueError, "should have table_channels"):
with pytest.raises(ValueError, match="should have table_channels"):
im.filter(lut)

lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
lut.table = numpy.array(lut.table, dtype=numpy.float32).reshape((7 * 9 * 11), 3)
with self.assertRaisesRegex(ValueError, "should have table_channels"):
with pytest.raises(ValueError, match="should have table_channels"):
im.filter(lut)

lut = ImageFilter.Color3DLUT.generate((7, 9, 11), lambda r, g, b: (r, g, b))
Expand Down Expand Up @@ -402,17 +401,17 @@ def test_repr(self):
)


class TestGenerateColorLut3D(PillowTestCase):
class TestGenerateColorLut3D:
def test_wrong_channels_count(self):
with self.assertRaisesRegex(ValueError, "3 or 4 output channels"):
with pytest.raises(ValueError, match="3 or 4 output channels"):
ImageFilter.Color3DLUT.generate(
5, channels=2, callback=lambda r, g, b: (r, g, b)
)

with self.assertRaisesRegex(ValueError, "should have either channels"):
with pytest.raises(ValueError, match="should have either channels"):
ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b, r))

with self.assertRaisesRegex(ValueError, "should have either channels"):
with pytest.raises(ValueError, match="should have either channels"):
ImageFilter.Color3DLUT.generate(
5, channels=4, callback=lambda r, g, b: (r, g, b)
)
Expand Down Expand Up @@ -450,17 +449,17 @@ def test_apply(self):
assert im == im.filter(lut)


class TestTransformColorLut3D(PillowTestCase):
class TestTransformColorLut3D:
def test_wrong_args(self):
source = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))

with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"):
with pytest.raises(ValueError, match="Only 3 or 4 output"):
source.transform(lambda r, g, b: (r, g, b), channels=8)

with self.assertRaisesRegex(ValueError, "should have either channels"):
with pytest.raises(ValueError, match="should have either channels"):
source.transform(lambda r, g, b: (r, g, b), channels=4)

with self.assertRaisesRegex(ValueError, "should have either channels"):
with pytest.raises(ValueError, match="should have either channels"):
source.transform(lambda r, g, b: (r, g, b, 1))

with pytest.raises(TypeError):
Expand Down

0 comments on commit de179eb

Please sign in to comment.