Convert asserts

This commit is contained in:
Hugo 2020-02-22 18:07:04 +02:00
parent dab94e69d1
commit 699a9dadf1
11 changed files with 58 additions and 54 deletions

View File

@ -49,7 +49,7 @@ class BenchCffiAccess(PillowTestCase):
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)

View File

@ -26,7 +26,7 @@ class TestImagingLeaks(PillowTestCase):
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))

View File

@ -42,37 +42,37 @@ class TestColorLut3DCoreAPI(PillowTestCase):
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):
@ -105,25 +105,25 @@ class TestColorLut3DCoreAPI(PillowTestCase):
)
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))
@ -273,31 +273,31 @@ class TestColorLut3DCoreAPI(PillowTestCase):
class TestColorLut3DFilter(PillowTestCase):
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):
@ -320,7 +320,7 @@ class TestColorLut3DFilter(PillowTestCase):
@unittest.skipIf(numpy is None, "Numpy is 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)
@ -359,12 +359,12 @@ class TestColorLut3DFilter(PillowTestCase):
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))
@ -404,15 +404,15 @@ class TestColorLut3DFilter(PillowTestCase):
class TestGenerateColorLut3D(PillowTestCase):
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)
)
@ -454,13 +454,13 @@ class TestTransformColorLut3D(PillowTestCase):
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):

View File

@ -1,4 +1,5 @@
import os
import re
from io import BytesIO
import pytest
@ -42,7 +43,7 @@ class TestFileJpeg(PillowTestCase):
def test_sanity(self):
# internal version number
self.assertRegex(Image.core.jpeglib_version, r"\d+\.\d+$")
assert re.search(r"\d+\.\d+$", Image.core.jpeglib_version)
with Image.open(TEST_FILE) as im:
im.load()

View File

@ -1,3 +1,4 @@
import re
from io import BytesIO
import pytest
@ -34,7 +35,7 @@ class TestFileJpeg2k(PillowTestCase):
def test_sanity(self):
# Internal version number
self.assertRegex(Image.core.jp2klib_version, r"\d+\.\d+\.\d+$")
assert re.search(r"\d+\.\d+\.\d+$", Image.core.jp2klib_version)
with Image.open("Tests/images/test-card-lossless.jp2") as im:
px = im.load()

View File

@ -295,7 +295,9 @@ class TestFileLibTiff(LibTiffTestCase):
and libtiff
):
# libtiff does not support real RATIONALS
self.assertAlmostEqual(float(reloaded_value), float(value))
assert (
round(abs(float(reloaded_value) - float(value)), 7) == 0
)
continue
if libtiff and isinstance(value, bytes):

View File

@ -1,3 +1,4 @@
import re
import unittest
import zlib
from io import BytesIO
@ -75,7 +76,7 @@ class TestFilePng(PillowTestCase):
def test_sanity(self):
# internal version number
self.assertRegex(Image.core.zlib_version, r"\d+\.\d+\.\d+(\.\d+)?$")
assert re.search(r"\d+\.\d+\.\d+(\.\d+)?$", Image.core.zlib_version)
test_file = self.tempfile("temp.png")

View File

@ -65,9 +65,9 @@ class TestFileTiffMetadata(PillowTestCase):
assert loaded.tag_v2[ImageDescription] == reloaded_textdata
loaded_float = loaded.tag[tag_ids["RollAngle"]][0]
self.assertAlmostEqual(loaded_float, floatdata, places=5)
assert round(abs(loaded_float - floatdata), 5) == 0
loaded_double = loaded.tag[tag_ids["YawAngle"]][0]
self.assertAlmostEqual(loaded_double, doubledata)
assert round(abs(loaded_double - doubledata), 7) == 0
# check with 2 element ImageJMetaDataByteCounts, issue #2006

View File

@ -228,9 +228,8 @@ class TestCffi(AccessTest):
assert access[(x, y)] == caccess[(x, y)]
# Access an out-of-range pixel
self.assertRaises(
ValueError, lambda: access[(access.xsize + 1, access.ysize + 1)]
)
with pytest.raises(ValueError):
access[(access.xsize + 1, access.ysize + 1)]
def test_get_vs_c(self):
rgb = hopper("RGB")

View File

@ -418,24 +418,24 @@ class CoreResampleBoxTest(PillowTestCase):
im.resize((32, 32), resample, (20, 20, 20, 100))
im.resize((32, 32), resample, (20, 20, 100, 20))
with self.assertRaisesRegex(TypeError, "must be sequence of length 4"):
with pytest.raises(TypeError, match="must be sequence of length 4"):
im.resize((32, 32), resample, (im.width, im.height))
with self.assertRaisesRegex(ValueError, "can't be negative"):
with pytest.raises(ValueError, match="can't be negative"):
im.resize((32, 32), resample, (-20, 20, 100, 100))
with self.assertRaisesRegex(ValueError, "can't be negative"):
with pytest.raises(ValueError, match="can't be negative"):
im.resize((32, 32), resample, (20, -20, 100, 100))
with self.assertRaisesRegex(ValueError, "can't be empty"):
with pytest.raises(ValueError, match="can't be empty"):
im.resize((32, 32), resample, (20.1, 20, 20, 100))
with self.assertRaisesRegex(ValueError, "can't be empty"):
with pytest.raises(ValueError, match="can't be empty"):
im.resize((32, 32), resample, (20, 20.1, 100, 20))
with self.assertRaisesRegex(ValueError, "can't be empty"):
with pytest.raises(ValueError, match="can't be empty"):
im.resize((32, 32), resample, (20.1, 20.1, 20, 20))
with self.assertRaisesRegex(ValueError, "can't exceed"):
with pytest.raises(ValueError, match="can't exceed"):
im.resize((32, 32), resample, (0, 0, im.width + 1, im.height))
with self.assertRaisesRegex(ValueError, "can't exceed"):
with pytest.raises(ValueError, match="can't exceed"):
im.resize((32, 32), resample, (0, 0, im.width, im.height + 1))
def resize_tiled(self, im, dst_size, xtiles, ytiles):
@ -480,7 +480,7 @@ class CoreResampleBoxTest(PillowTestCase):
# error with box should be much smaller than without
assert_image_similar(reference, with_box, 6)
with self.assertRaisesRegex(AssertionError, r"difference 29\."):
with pytest.raises(AssertionError, match=r"difference 29\."):
assert_image_similar(reference, without_box, 5)
def test_formats(self):
@ -518,7 +518,7 @@ class CoreResampleBoxTest(PillowTestCase):
]:
res = im.resize(size, Image.LANCZOS, box)
assert res.size == size
with self.assertRaisesRegex(AssertionError, r"difference \d"):
with pytest.raises(AssertionError, match=r"difference \d"):
# check that the difference at least that much
assert_image_similar(
res, im.crop(box), 20, ">>> {} {}".format(size, box)

View File

@ -66,7 +66,7 @@ class TestImageFont(PillowTestCase):
)
def test_sanity(self):
self.assertRegex(ImageFont.core.freetype2_version, r"\d+\.\d+\.\d+$")
assert re.search(r"\d+\.\d+\.\d+$", ImageFont.core.freetype2_version)
def test_font_properties(self):
ttf = self.get_font()