test for deprecation warning

This commit is contained in:
Anubhav Patel 2019-05-07 15:44:21 +05:30
parent 2a6bcdb413
commit 33925a7761
2 changed files with 47 additions and 36 deletions

View File

@ -141,7 +141,7 @@ class ImagesPipeline(FilesPipeline):
from scrapy.exceptions import ScrapyDeprecationWarning
import warnings
warnings.warn('ImagesPipeline.convert_image() method overriden in a incompatible way, '
'overriden method does not accept response_body attribute.',
'overriden method does not accept response_body argument.',
category=ScrapyDeprecationWarning, stacklevel=1)
convert_image_overriden = _is_convert_image_overriden()
@ -162,6 +162,13 @@ class ImagesPipeline(FilesPipeline):
yield thumb_path, thumb_image, thumb_buf
def convert_image(self, image, size=None, response_body=None):
if not response_body:
from scrapy.exceptions import ScrapyDeprecationWarning
import warnings
warnings.warn('ImagesPipeline.convert_image() method called in a incompatible way, '
'method called without response_body argument.',
category=ScrapyDeprecationWarning, stacklevel=1)
if image.format == 'PNG' and image.mode == 'RGBA':
background = Image.new('RGBA', image.size, (255, 255, 255))
background.paste(image, image)
@ -177,15 +184,8 @@ class ImagesPipeline(FilesPipeline):
if size:
image = image.copy()
image.thumbnail(size, Image.ANTIALIAS)
else:
if not response_body:
from scrapy.exceptions import ScrapyDeprecationWarning
import warnings
warnings.warn('ImagesPipeline.convert_image() method called in a incompatible way, '
'method called without response_body attribute.',
category=ScrapyDeprecationWarning, stacklevel=1)
elif image.format == 'JPEG':
return image, response_body
elif response_body and image.format == 'JPEG':
return image, response_body
buf = BytesIO()
image.save(buf, 'JPEG')

View File

@ -76,37 +76,48 @@ class ImagesPipelineTestCase(unittest.TestCase):
'thumbs/50/850233df65a5b83361798f532f1fc549cd13cbe9.jpg')
def test_convert_image(self):
SIZE = (100, 100)
# straigh forward case: RGB and JPEG
# tests for old API
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
SIZE = (100, 100)
# straigh forward case: RGB and JPEG
COLOUR = (0, 127, 255)
im, buf = _create_image('JPEG', 'RGB', SIZE, COLOUR)
converted, converted_buf = self.pipeline.convert_image(im)
self.assertEqual(converted.mode, 'RGB')
self.assertEqual(converted.getcolors(), [(10000, COLOUR)])
# check that thumbnail keep image ratio
thumbnail, _ = self.pipeline.convert_image(converted, size=(10, 25))
self.assertEqual(thumbnail.mode, 'RGB')
self.assertEqual(thumbnail.size, (10, 10))
# transparency case: RGBA and PNG
COLOUR = (0, 127, 255, 50)
im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)
converted, _ = self.pipeline.convert_image(im)
self.assertEqual(converted.mode, 'RGB')
self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])
# transparency case with palette: P and PNG
COLOUR = (0, 127, 255, 50)
im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)
im = im.convert('P')
converted, _ = self.pipeline.convert_image(im)
self.assertEqual(converted.mode, 'RGB')
self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])
# ensure that we recieved deprecation warnings
self.assertTrue(len(w) >= 4)
# tests for new API
# check that we don't convert JPEGs again
COLOUR = (0, 127, 255)
im, buf = _create_image('JPEG', 'RGB', SIZE, COLOUR)
converted, converted_buf = self.pipeline.convert_image(im, response_body=buf)
self.assertEqual(converted.mode, 'RGB')
self.assertEqual(converted.getcolors(), [(10000, COLOUR)])
# check that we don't convert JPEGs again
self.assertEqual(converted_buf, buf)
# check that thumbnail keep image ratio
thumbnail, _ = self.pipeline.convert_image(converted, size=(10, 25), response_body=converted_buf)
self.assertEqual(thumbnail.mode, 'RGB')
self.assertEqual(thumbnail.size, (10, 10))
# transparency case: RGBA and PNG
COLOUR = (0, 127, 255, 50)
im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)
converted, _ = self.pipeline.convert_image(im, response_body=buf)
self.assertEqual(converted.mode, 'RGB')
self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])
# transparency case with palette: P and PNG
COLOUR = (0, 127, 255, 50)
im, buf = _create_image('PNG', 'RGBA', SIZE, COLOUR)
im = im.convert('P')
converted, _ = self.pipeline.convert_image(im, response_body=buf)
self.assertEqual(converted.mode, 'RGB')
self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])
class DeprecatedImagesPipeline(ImagesPipeline):
def file_key(self, url):
return self.image_key(url)