Added various tests

This commit is contained in:
Andrew Murray 2015-07-03 15:03:25 +10:00
parent 1a5f9cfac6
commit a06b59bd52
38 changed files with 264 additions and 28 deletions

View File

@ -3,7 +3,7 @@
from __future__ import division
from helper import unittest, PillowTestCase
import sys
from PIL import Image, ImageFilter
from PIL import Image
min_iterations = 100
max_iterations = 10000

View File

@ -61,6 +61,10 @@ class TestCffi(PillowTestCase):
for y in range(0, h, 10):
self.assertEqual(access[(x, y)], caccess[(x, y)])
# Access an out-of-range pixel
self.assertRaises(ValueError,
lambda: access[(access.xsize+1, access.ysize+1)])
def test_get_vs_c(self):
rgb = hopper('RGB')
rgb.load()
@ -103,6 +107,14 @@ class TestCffi(PillowTestCase):
access[(x, y)] = color
self.assertEqual(color, caccess[(x, y)])
# Attempt to set the value on a read-only image
access = PyAccess.new(im, True)
try:
access[(0, 0)] = color
except ValueError:
return
self.fail("Putpixel did not fail on a read-only image")
def test_set_vs_c(self):
rgb = hopper('RGB')
rgb.load()

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import Image, BmpImagePlugin
import io
@ -25,6 +25,10 @@ class TestFileBmp(PillowTestCase):
self.roundtrip(hopper("P"))
self.roundtrip(hopper("RGB"))
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: BmpImagePlugin.BmpImageFile(fp))
def test_save_to_bytes(self):
output = io.BytesIO()
im = hopper()

View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import BufrStubImagePlugin
class TestFileBufrStub(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: BufrStubImagePlugin.BufrStubImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -20,6 +20,9 @@ class TestFileCur(PillowTestCase):
self.assertEqual(im.getpixel((11, 1)), (253, 254, 254, 1))
self.assertEqual(im.getpixel((16, 16)), (84, 87, 86, 255))
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: CurImagePlugin.CurImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()

View File

@ -20,6 +20,10 @@ class TestFileDcx(PillowTestCase):
orig = hopper()
self.assert_image_equal(im, orig)
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: DcxImagePlugin.DcxImageFile(fp))
def test_tell(self):
# Arrange
im = Image.open(TEST_FILE)

View File

@ -51,6 +51,9 @@ class TestFileEps(PillowTestCase):
self.assertEqual(image2_scale2.size, (720, 504))
self.assertEqual(image2_scale2.format, "EPS")
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: EpsImagePlugin.EpsImageFile("Tests/images/flower.jpg"))
def test_file_object(self):
# issue 479
image1 = Image.open(file1)

View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import FitsStubImagePlugin
class TestFileFitsStub(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: FitsStubImagePlugin.FITSStubImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import Image, FliImagePlugin
# sample ppm stream
# created as an export of a palette image from Gimp2.6
@ -18,6 +18,9 @@ class TestFileFli(PillowTestCase):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "FLI")
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: FliImagePlugin.FliImageFile("Tests/images/flower.jpg"))
def test_n_frames(self):
im = Image.open(test_file)
self.assertEqual(im.n_frames, 1)

15
Tests/test_file_fpx.py Normal file
View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import FpxImagePlugin
class TestFileFpx(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: FpxImagePlugin.FpxImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

15
Tests/test_file_gbr.py Normal file
View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import GbrImagePlugin
class TestFileGbr(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: GbrImagePlugin.GbrImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -25,6 +25,9 @@ class TestFileGif(PillowTestCase):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "GIF")
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: GifImagePlugin.GifImageFile("Tests/images/flower.jpg"))
def test_optimize(self):
from io import BytesIO

View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import GribStubImagePlugin
class TestFileGribStub(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: GribStubImagePlugin.GribStubImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import Hdf5StubImagePlugin
class TestFileHdf5Stub(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: Hdf5StubImagePlugin.HDF5StubImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -1,7 +1,7 @@
from helper import unittest, PillowTestCase, hopper
import io
from PIL import Image
from PIL import Image, IcoImagePlugin
# sample ppm stream
TEST_ICO_FILE = "Tests/images/hopper.ico"
@ -17,6 +17,10 @@ class TestFileIco(PillowTestCase):
self.assertEqual(im.size, (16, 16))
self.assertEqual(im.format, "ICO")
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: IcoImagePlugin.IcoImageFile(fp))
def test_save_to_bytes(self):
output = io.BytesIO()
im = hopper()

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import Image, ImImagePlugin
# sample im
TEST_IM = "Tests/images/hopper.im"
@ -40,6 +40,10 @@ class TestFileIm(PillowTestCase):
self.assert_image_equal(reread, im)
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: ImImagePlugin.ImImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import Image, Jpeg2KImagePlugin
from io import BytesIO
codecs = dir(Image.core)
@ -39,6 +39,9 @@ class TestFileJpeg2k(PillowTestCase):
self.assertEqual(im.size, (640, 480))
self.assertEqual(im.format, 'JPEG2000')
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: Jpeg2KImagePlugin.Jpeg2KImageFile("Tests/images/flower.jpg"))
def test_bytesio(self):
with open('Tests/images/test-card-lossless.jp2', 'rb') as f:
data = BytesIO(f.read())

15
Tests/test_file_mcidas.py Normal file
View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import McIdasImagePlugin
class TestFileMcIdas(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: McIdasImagePlugin.McIdasImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

15
Tests/test_file_mic.py Normal file
View File

@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase
from PIL import MicImagePlugin
class TestFileMic(PillowTestCase):
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: MicImagePlugin.MicImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()
# End of file

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import Image, MspImagePlugin
TEST_FILE = "Tests/images/hopper.msp"
@ -18,6 +18,9 @@ class TestFileMsp(PillowTestCase):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "MSP")
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: MspImagePlugin.MspImageFile("Tests/images/flower.jpg"))
def test_open(self):
# Arrange
# Act

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import Image, PcxImagePlugin
class TestFilePcx(PillowTestCase):
@ -19,6 +19,9 @@ class TestFilePcx(PillowTestCase):
for mode in ('1', 'L', 'P', 'RGB'):
self._roundtrip(hopper(mode))
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: PcxImagePlugin.PcxImageFile("Tests/images/flower.jpg"))
def test_odd(self):
# see issue #523, odd sized images should have a stride that's even.
# not that imagemagick or gimp write pcx that way.

View File

@ -52,6 +52,12 @@ class TestFilePdf(PillowTestCase):
# Act / Assert
self.helper_save_as_pdf(mode)
def test_unsupported_mode(self):
im = hopper("LA")
outfile = self.tempfile("temp_LA.pdf")
self.assertRaises(ValueError, lambda: im.save(outfile))
if __name__ == '__main__':
unittest.main()

View File

@ -81,6 +81,9 @@ class TestFilePng(PillowTestCase):
hopper("I").save(test_file)
im = Image.open(test_file)
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: PngImagePlugin.PngImageFile("Tests/images/flower.jpg"))
def test_broken(self):
# Check reading of totally broken files. In this case, the test
# file was checked into Subversion as a text file.

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import Image, PsdImagePlugin
# sample ppm stream
test_file = "Tests/images/hopper.psd"
@ -16,6 +16,9 @@ class TestImagePsd(PillowTestCase):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "PSD")
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: PsdImagePlugin.PsdImageFile("Tests/images/flower.jpg"))
def test_n_frames(self):
im = Image.open("Tests/images/hopper_merged.psd")
self.assertEqual(im.n_frames, 1)

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import Image, SgiImagePlugin
class TestFileSgi(PillowTestCase):
@ -32,6 +32,9 @@ class TestFileSgi(PillowTestCase):
# Act / Assert
self.assertRaises(ValueError, lambda: Image.open(test_file))
def test_invalid_file(self):
self.assertRaises(ValueError, lambda: SgiImagePlugin.SgiImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import Image, SunImagePlugin
class TestFileSun(PillowTestCase):
@ -16,6 +16,8 @@ class TestFileSun(PillowTestCase):
# Assert
self.assertEqual(im.size, (128, 128))
self.assertRaises(SyntaxError, lambda: SunImagePlugin.SunImageFile("Tests/images/flower.jpg"))
if __name__ == '__main__':
unittest.main()

View File

@ -15,6 +15,25 @@ class TestFileTga(PillowTestCase):
# Assert
self.assertEqual(im.size, (100, 100))
def test_save(self):
test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file)
test_file = self.tempfile("temp.tga")
# Save
im.save(test_file)
test_im = Image.open(test_file)
self.assertEqual(test_im.size, (100, 100))
# RGBA save
im.convert("RGBA").save(test_file)
test_im = Image.open(test_file)
self.assertEqual(test_im.size, (100, 100))
# Unsupported mode save
self.assertRaises(IOError, lambda: im.convert("LA").save(test_file))
if __name__ == '__main__':
unittest.main()

View File

@ -72,6 +72,11 @@ class TestFileWebp(PillowTestCase):
target = hopper("RGB")
self.assert_image_similar(image, target, 12)
def test_write_unsupported_mode(self):
temp_file = self.tempfile("temp.webp")
self.assertRaises(IOError, lambda: hopper("L").save(temp_file))
if __name__ == '__main__':
unittest.main()

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import Image, XpmImagePlugin
# sample ppm stream
TEST_FILE = "Tests/images/hopper.xpm"
@ -18,6 +18,9 @@ class TestFileXpm(PillowTestCase):
# large error due to quantization->44 colors.
self.assert_image_similar(im.convert('RGB'), hopper('RGB'), 60)
def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: XpmImagePlugin.XpmImageFile("Tests/images/flower.jpg"))
def test_load_read(self):
# Arrange
im = Image.open(TEST_FILE)

View File

@ -15,6 +15,10 @@ class TestFontBdf(PillowTestCase):
self.assertIsInstance(font, FontFile.FontFile)
self.assertEqual(len([_f for _f in font.glyph if _f]), 190)
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: BdfFontFile.BdfFontFile(fp))
if __name__ == '__main__':
unittest.main()

View File

@ -30,6 +30,10 @@ class TestFontPcf(PillowTestCase):
def test_sanity(self):
self.save_font()
def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: PcfFontFile.PcfFontFile(fp))
def xtest_draw(self):
tempname = self.save_font()

View File

@ -88,6 +88,12 @@ class TestImageFilter(PillowTestCase):
self.assertEqual(rankfilter("I"), (0, 4, 8))
self.assertEqual(rankfilter("F"), (0.0, 4.0, 8.0))
def test_rankfilter_properties(self):
rankfilter = ImageFilter.RankFilter(1,2)
self.assertEqual(rankfilter.size, 1)
self.assertEqual(rankfilter.rank, 2)
if __name__ == '__main__':
unittest.main()

View File

@ -21,12 +21,14 @@ class TestImageMode(PillowTestCase):
m = ImageMode.getmode("1")
self.assertEqual(m.mode, "1")
self.assertEqual(str(m), "1")
self.assertEqual(m.bands, ("1",))
self.assertEqual(m.basemode, "L")
self.assertEqual(m.basetype, "L")
m = ImageMode.getmode("RGB")
self.assertEqual(m.mode, "RGB")
self.assertEqual(str(m), "RGB")
self.assertEqual(m.bands, ("R", "G", "B"))
self.assertEqual(m.basemode, "RGB")
self.assertEqual(m.basetype, "L")

View File

@ -18,6 +18,8 @@ class TestImageColor(PillowTestCase):
(255, 0, 0, 0), ImageColor.getrgb("rgba(255, 0, 0, 0)"))
self.assertEqual((255, 0, 0), ImageColor.getrgb("red"))
self.assertRaises(ValueError, lambda: ImageColor.getrgb("invalid color"))
# look for rounding errors (based on code by Tim Hatch)
def test_rounding_errors(self):

View File

@ -52,6 +52,11 @@ class TestImageDraw(PillowTestCase):
self.assert_warning(DeprecationWarning, lambda: draw.setink(0))
self.assert_warning(DeprecationWarning, lambda: draw.setfill(0))
def test_mode_mismatch(self):
im = hopper("RGB").copy()
self.assertRaises(ValueError, lambda: ImageDraw.ImageDraw(im, mode="L"))
def helper_arc(self, bbox):
# Arrange
im = Image.new("RGB", (W, H))

View File

@ -93,6 +93,9 @@ class TestImageFile(PillowTestCase):
self.assert_image_equal(im1, im2)
def test_raise_ioerror(self):
self.assertRaises(IOError, lambda: ImageFile.raise_ioerror(1))
if __name__ == '__main__':
unittest.main()

View File

@ -22,6 +22,8 @@ class TestImageSequence(PillowTestCase):
self.assertEqual(index, 1)
self.assertRaises(AttributeError, lambda: ImageSequence.Iterator(0))
def _test_multipage_tiff(self, dbg=False):
im = Image.open('Tests/images/multipage.tiff')
for index, frame in enumerate(ImageSequence.Iterator(im)):

View File

@ -7,25 +7,20 @@ import PIL.OleFileIO as OleFileIO
class TestOleFileIo(PillowTestCase):
def test_isOleFile_false(self):
# Arrange
non_ole_file = "Tests/images/flower.jpg"
# Act
is_ole = OleFileIO.isOleFile(non_ole_file)
# Assert
self.assertFalse(is_ole)
def test_isOleFile_true(self):
# Arrange
def test_isOleFile(self):
ole_file = "Tests/images/test-ole-file.doc"
# Act
is_ole = OleFileIO.isOleFile(ole_file)
self.assertTrue(OleFileIO.isOleFile(ole_file))
with open(ole_file, 'rb') as fp:
self.assertTrue(OleFileIO.isOleFile(fp))
self.assertTrue(OleFileIO.isOleFile(fp.read()))
# Assert
self.assertTrue(is_ole)
non_ole_file = "Tests/images/flower.jpg"
self.assertFalse(OleFileIO.isOleFile(non_ole_file))
with open(non_ole_file, 'rb') as fp:
self.assertFalse(OleFileIO.isOleFile(fp))
self.assertFalse(OleFileIO.isOleFile(fp.read()))
def test_exists_worddocument(self):
# Arrange