Scrapy, a fast high-level web crawling & scraping framework for Python.
Test returning items from an async def callback.
Andrey Rakhmatullincc825c21deaa
diff --git a/tests/spiders.py b/tests/spiders.pyindex e4f2d5474..3b1ee94b8 100644--- a/tests/spiders.py+++ b/tests/spiders.py@@ -106,6 +106,17 @@ class AsyncDefAsyncioSpider(SimpleSpider):self.logger.info("Got response %d" % status)+class AsyncDefAsyncioReturnSpider(SimpleSpider):++ name = 'asyncdef_asyncio_return'++ async def parse(self, response):+ await asyncio.sleep(0.2)+ status = await get_from_asyncio_queue(response.status)+ self.logger.info("Got response %d" % status)+ return [{'id': 1}, {'id': 2}]++class ItemSpider(FollowAllSpider):name = 'item'diff --git a/tests/test_crawl.py b/tests/test_crawl.pyindex 99b887ff6..85005eba4 100644--- a/tests/test_crawl.py+++ b/tests/test_crawl.py@@ -6,13 +6,14 @@ from testfixtures import LogCapturefrom twisted.internet import deferfrom twisted.trial.unittest import TestCase+from scrapy import signalsfrom scrapy.crawler import CrawlerRunnerfrom scrapy.http import Requestfrom scrapy.utils.python import to_unicodefrom tests.mockserver import MockServerfrom tests.spiders import (FollowAllSpider, DelaySpider, SimpleSpider, BrokenStartRequestsSpider,SingleRequestSpider, DuplicateStartRequestsSpider, CrawlSpiderWithErrback,- AsyncDefSpider, AsyncDefAsyncioSpider)+ AsyncDefSpider, AsyncDefAsyncioSpider, AsyncDefAsyncioReturnSpider)class CrawlTestCase(TestCase):@@ -326,3 +327,19 @@ with multiples lineswith LogCapture() as log:yield runner.join()self.assertIn("Got response 200", str(log))++ @mark.only_asyncio()+ @defer.inlineCallbacks+ def test_async_def_asyncio_parse_list(self):+ items = []++ def _on_item_scraped(item):+ items.append(item)++ crawler = self.runner.create_crawler(AsyncDefAsyncioReturnSpider)+ crawler.signals.connect(_on_item_scraped, signals.item_scraped)+ with LogCapture() as log:+ yield crawler.crawl(self.mockserver.url("/status?n=200"), mockserver=self.mockserver)+ self.assertIn("Got response 200", str(log))+ self.assertIn({'id': 1}, items)+ self.assertIn({'id': 2}, items)