Scrapy, a fast high-level web crawling & scraping framework for Python.
[test] Spider middleware: catch exceptions right after the spider callback
Eugenio Lacuesta6ce1ad310713
diff --git a/tests/test_spidermiddleware_output_chain.py b/tests/test_spidermiddleware_output_chain.pyindex 739cf1c2d..b19a74609 100644--- a/tests/test_spidermiddleware_output_chain.py+++ b/tests/test_spidermiddleware_output_chain.py@@ -1,10 +1,10 @@-from testfixtures import LogCapture-from twisted.trial.unittest import TestCasefrom twisted.internet import defer+from twisted.trial.unittest import TestCase-from scrapy import Spider, Request+from scrapy import Request, Spiderfrom scrapy.utils.test import get_crawler+from tests.mockserver import MockServer@@ -74,7 +74,7 @@ class ProcessSpiderInputSpiderWithErrback(ProcessSpiderInputSpiderWithoutErrbackname = 'ProcessSpiderInputSpiderWithErrback'def start_requests(self):- yield Request(url=self.mockserver.url('/status?n=200'), callback=self.parse, errback=self.errback)+ yield Request(self.mockserver.url('/status?n=200'), self.parse, errback=self.errback)def errback(self, failure):self.logger.info('Got a Failure on the Request errback')@@ -100,6 +100,17 @@ class GeneratorCallbackSpider(Spider):raise ImportError()+# ================================================================================+# (2.1) exceptions from a spider callback (generator, middleware right after callback)+class GeneratorCallbackSpiderMiddlewareRightAfterSpider(GeneratorCallbackSpider):+ name = 'GeneratorCallbackSpiderMiddlewareRightAfterSpider'+ custom_settings = {+ 'SPIDER_MIDDLEWARES': {+ __name__ + '.LogExceptionMiddleware': 100000,+ },+ }++# ================================================================================# (3) exceptions from a spider callback (not a generator)class NotGeneratorCallbackSpider(Spider):@@ -117,6 +128,17 @@ class NotGeneratorCallbackSpider(Spider):return [{'test': 1}, {'test': 1/0}]+# ================================================================================+# (3.1) exceptions from a spider callback (not a generator, middleware right after callback)+class NotGeneratorCallbackSpiderMiddlewareRightAfterSpider(NotGeneratorCallbackSpider):+ name = 'NotGeneratorCallbackSpiderMiddlewareRightAfterSpider'+ custom_settings = {+ 'SPIDER_MIDDLEWARES': {+ __name__ + '.LogExceptionMiddleware': 100000,+ },+ }++# ================================================================================# (4) exceptions from a middleware process_spider_output method (generator)class GeneratorOutputChainSpider(Spider):@@ -320,6 +342,16 @@ class TestSpiderMiddleware(TestCase):self.assertIn("Middleware: ImportError exception caught", str(log2))self.assertIn("'item_scraped_count': 2", str(log2))+ @defer.inlineCallbacks+ def test_generator_callback_right_after_callback(self):+ """+ (2.1) Special case of (2): Exceptions should be caught+ even if the middleware is placed right after the spider+ """+ log21 = yield self.crawl_log(GeneratorCallbackSpiderMiddlewareRightAfterSpider)+ self.assertIn("Middleware: ImportError exception caught", str(log21))+ self.assertIn("'item_scraped_count': 2", str(log21))+@defer.inlineCallbacksdef test_not_a_generator_callback(self):"""@@ -330,6 +362,16 @@ class TestSpiderMiddleware(TestCase):self.assertIn("Middleware: ZeroDivisionError exception caught", str(log3))self.assertNotIn("item_scraped_count", str(log3))+ @defer.inlineCallbacks+ def test_not_a_generator_callback_right_after_callback(self):+ """+ (3.1) Special case of (3): Exceptions should be caught+ even if the middleware is placed right after the spider+ """+ log31 = yield self.crawl_log(NotGeneratorCallbackSpiderMiddlewareRightAfterSpider)+ self.assertIn("Middleware: ZeroDivisionError exception caught", str(log31))+ self.assertNotIn("item_scraped_count", str(log31))+@defer.inlineCallbacksdef test_generator_output_chain(self):"""