Pillow/Tests/test_file_spider.py

120 lines
2.9 KiB
Python
Raw Normal View History

from helper import unittest, PillowTestCase, hopper
from PIL import Image
2016-04-04 11:48:21 +03:00
from PIL import ImageSequence
2014-05-26 22:00:20 +04:00
from PIL import SpiderImagePlugin
2017-07-29 03:20:19 +03:00
import tempfile
TEST_FILE = "Tests/images/hopper.spider"
2014-06-10 13:10:47 +04:00
class TestImageSpider(PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_sanity(self):
2014-07-15 13:23:02 +04:00
im = Image.open(TEST_FILE)
2014-06-10 13:10:47 +04:00
im.load()
self.assertEqual(im.mode, "F")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "SPIDER")
2014-05-26 22:00:20 +04:00
2014-06-10 13:10:47 +04:00
def test_save(self):
# Arrange
temp = self.tempfile('temp.spider')
im = hopper()
2014-05-26 22:00:20 +04:00
2014-06-10 13:10:47 +04:00
# Act
im.save(temp, "SPIDER")
2014-05-26 22:00:20 +04:00
2014-06-10 13:10:47 +04:00
# Assert
im2 = Image.open(temp)
self.assertEqual(im2.mode, "F")
self.assertEqual(im2.size, (128, 128))
self.assertEqual(im2.format, "SPIDER")
2014-05-26 22:00:20 +04:00
2017-07-29 03:20:19 +03:00
def test_tempfile(self):
# Arrange
im = hopper()
# Act
with tempfile.TemporaryFile() as fp:
im.save(fp, "SPIDER")
# Assert
fp.seek(0)
reloaded = Image.open(fp)
self.assertEqual(reloaded.mode, "F")
self.assertEqual(reloaded.size, (128, 128))
self.assertEqual(reloaded.format, "SPIDER")
2014-06-10 13:10:47 +04:00
def test_isSpiderImage(self):
2014-07-15 13:23:02 +04:00
self.assertTrue(SpiderImagePlugin.isSpiderImage(TEST_FILE))
def test_tell(self):
# Arrange
im = Image.open(TEST_FILE)
# Act
index = im.tell()
# Assert
self.assertEqual(index, 0)
2015-06-07 18:01:34 +03:00
def test_n_frames(self):
im = Image.open(TEST_FILE)
self.assertEqual(im.n_frames, 1)
2015-06-30 06:25:00 +03:00
self.assertFalse(im.is_animated)
2015-06-07 18:01:34 +03:00
2014-07-15 13:23:02 +04:00
def test_loadImageSeries(self):
# Arrange
not_spider_file = "Tests/images/hopper.ppm"
2014-07-15 13:23:02 +04:00
file_list = [TEST_FILE, not_spider_file, "path/not_found.ext"]
# Act
img_list = SpiderImagePlugin.loadImageSeries(file_list)
# Assert
self.assertEqual(len(img_list), 1)
self.assertIsInstance(img_list[0], Image.Image)
self.assertEqual(img_list[0].size, (128, 128))
def test_loadImageSeries_no_input(self):
# Arrange
file_list = None
# Act
img_list = SpiderImagePlugin.loadImageSeries(file_list)
# Assert
self.assertIsNone(img_list)
2014-07-15 13:23:02 +04:00
def test_isInt_not_a_number(self):
# Arrange
not_a_number = "a"
# Act
ret = SpiderImagePlugin.isInt(not_a_number)
# Assert
self.assertEqual(ret, 0)
2014-05-26 22:00:20 +04:00
def test_invalid_file(self):
invalid_file = "Tests/images/invalid.spider"
2015-12-10 01:35:35 +03:00
2017-09-01 14:05:40 +03:00
self.assertRaises(IOError, Image.open, invalid_file)
2016-04-04 11:48:21 +03:00
def test_nonstack_file(self):
im = Image.open(TEST_FILE)
2017-09-01 14:05:40 +03:00
self.assertRaises(EOFError, im.seek, 0)
2016-04-04 11:48:21 +03:00
def test_nonstack_dos(self):
im = Image.open(TEST_FILE)
for i, frame in enumerate(ImageSequence.Iterator(im)):
if i > 1:
self.fail("Non-stack DOS file test failed")
2014-05-26 22:00:20 +04:00
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()