Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

implement DeprecatedImagesPipelineTestCase

Max Arnold69595233383f
diff --git a/scrapy/tests/test_pipeline_images.py b/scrapy/tests/test_pipeline_images.py
index 449ff6dc2..6f7e66d8f 100644
--- a/scrapy/tests/test_pipeline_images.py
+++ b/scrapy/tests/test_pipeline_images.py
@@ -1,4 +1,6 @@
import os
+import hashlib
+import warnings
from cStringIO import StringIO
from tempfile import mkdtemp
from shutil import rmtree
@@ -8,6 +10,7 @@ from twisted.trial import unittest
from scrapy.item import Item, Field
from scrapy.http import Request
from scrapy.settings import Settings
+from scrapy.contrib.pipeline.images import ImagesPipeline
skip = False
try:
@@ -30,7 +33,6 @@ class ImagesPipelineTestCase(unittest.TestCase):
skip = skip
def setUp(self):
- from scrapy.contrib.pipeline.images import ImagesPipeline
self.tempdir = mkdtemp()
self.pipeline = ImagesPipeline(self.tempdir, download_func=_mocked_download_func)
@@ -86,6 +88,76 @@ class ImagesPipelineTestCase(unittest.TestCase):
self.assertEquals(converted.getcolors(), [(10000, (205, 230, 255))])
+class DeprecatedImagesPipeline(ImagesPipeline):
+ def file_key(self, url):
+ return self.image_key(url)
+
+ def image_key(self, url):
+ image_guid = hashlib.sha1(url).hexdigest()
+ return 'empty/%s.jpg' % (image_guid)
+
+ def thumb_key(self, url, thumb_id):
+ thumb_guid = hashlib.sha1(url).hexdigest()
+ return 'thumbsup/%s/%s.jpg' % (thumb_id, thumb_guid)
+
+
+class DeprecatedImagesPipelineTestCase(unittest.TestCase):
+ def setUp(self):
+ self.tempdir = mkdtemp()
+
+ def init_pipeline(self, pipeline_class):
+ self.pipeline = pipeline_class(self.tempdir, download_func=_mocked_download_func)
+ self.pipeline.open_spider(None)
+
+ def test_default_file_key_method(self):
+ self.init_pipeline(ImagesPipeline)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ self.assertEqual(self.pipeline.file_key("https://dev.mydeco.com/mydeco.gif"),
+ 'full/3fd165099d8e71b8a48b2683946e64dbfad8b52d.jpg')
+ self.assertEqual(len(w), 1)
+ self.assertTrue('image_key(url) and file_key(url) methods are deprecated' in str(w[-1].message))
+
+ def test_default_image_key_method(self):
+ self.init_pipeline(ImagesPipeline)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ self.assertEqual(self.pipeline.image_key("https://dev.mydeco.com/mydeco.gif"),
+ 'full/3fd165099d8e71b8a48b2683946e64dbfad8b52d.jpg')
+ self.assertEqual(len(w), 1)
+ self.assertTrue('image_key(url) and file_key(url) methods are deprecated' in str(w[-1].message))
+
+ def test_overridden_file_key_method(self):
+ self.init_pipeline(DeprecatedImagesPipeline)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ self.assertEqual(self.pipeline.file_path(Request("https://dev.mydeco.com/mydeco.gif")),
+ 'empty/3fd165099d8e71b8a48b2683946e64dbfad8b52d.jpg')
+ self.assertEqual(len(w), 1)
+ self.assertTrue('image_key(url) and file_key(url) methods are deprecated' in str(w[-1].message))
+
+ def test_default_thumb_key_method(self):
+ self.init_pipeline(ImagesPipeline)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ self.assertEqual(self.pipeline.thumb_key("file:///tmp/foo.jpg", 50),
+ 'thumbs/50/38a86208c36e59d4404db9e37ce04be863ef0335.jpg')
+ self.assertEqual(len(w), 1)
+ self.assertTrue('thumb_key(url) method is deprecated' in str(w[-1].message))
+
+ def test_overridden_thumb_key_method(self):
+ self.init_pipeline(DeprecatedImagesPipeline)
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter('always')
+ self.assertEqual(self.pipeline.thumb_path(Request("file:///tmp/foo.jpg"), 50),
+ 'thumbsup/50/38a86208c36e59d4404db9e37ce04be863ef0335.jpg')
+ self.assertEqual(len(w), 1)
+ self.assertTrue('thumb_key(url) method is deprecated' in str(w[-1].message))
+
+ def tearDown(self):
+ rmtree(self.tempdir)
+
+
class ImagesPipelineTestCaseFields(unittest.TestCase):
def test_item_fields_default(self):