|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from scrapy.settings import Settings |
| 4 | + |
| 5 | +from apify.scrapy.utils import apply_apify_settings |
| 6 | + |
| 7 | + |
| 8 | +def test__apply_apify_settings__overrides_scheduler() -> None: |
| 9 | + settings = Settings() |
| 10 | + new_settings = apply_apify_settings(settings=settings) |
| 11 | + |
| 12 | + assert new_settings.get('SCHEDULER') == 'apify.scrapy.scheduler.ApifyScheduler' |
| 13 | + |
| 14 | + |
| 15 | +def test__apply_apify_settings__update_item_pipelines() -> None: |
| 16 | + settings = Settings( |
| 17 | + { |
| 18 | + 'ITEM_PIPELINES': { |
| 19 | + 'scrapy.pipelines.files.FilesPipeline': 1, |
| 20 | + } |
| 21 | + } |
| 22 | + ) |
| 23 | + new_settings = apply_apify_settings(settings=settings) |
| 24 | + |
| 25 | + assert new_settings.get('ITEM_PIPELINES') == { |
| 26 | + 'scrapy.pipelines.files.FilesPipeline': 1, |
| 27 | + 'apify.scrapy.pipelines.ActorDatasetPushPipeline': 1000, |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | +def test__apply_apify_settings__update_downloader_middlewares() -> None: |
| 32 | + settings = Settings( |
| 33 | + { |
| 34 | + 'DOWNLOADER_MIDDLEWARES': { |
| 35 | + 'scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware': 123, |
| 36 | + 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': 234, |
| 37 | + 'scrapy.downloadermiddlewares.retry.RetryMiddleware': 345, |
| 38 | + 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 543, |
| 39 | + }, |
| 40 | + } |
| 41 | + ) |
| 42 | + new_settings = apply_apify_settings(settings=settings) |
| 43 | + |
| 44 | + assert new_settings.get('DOWNLOADER_MIDDLEWARES') == { |
| 45 | + 'scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware': None, |
| 46 | + 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware': None, |
| 47 | + 'scrapy.downloadermiddlewares.retry.RetryMiddleware': None, |
| 48 | + 'apify.scrapy.middlewares.ApifyHttpProxyMiddleware': 950, |
| 49 | + 'apify.scrapy.middlewares.ApifyRetryMiddleware': 1000, |
| 50 | + 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 543, |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +def test__apply_apify_settings__add_proxy_config() -> None: |
| 55 | + settings = Settings() |
| 56 | + new_settings = apply_apify_settings(settings=settings) |
| 57 | + assert new_settings.get('APIFY_PROXY_SETTINGS') is None |
| 58 | + |
| 59 | + settings = Settings() |
| 60 | + proxy_config = {'useApifyProxy': True, 'apifyProxyGroups': []} |
| 61 | + new_settings = apply_apify_settings(settings=settings, proxy_config=proxy_config) |
| 62 | + assert new_settings.get('APIFY_PROXY_SETTINGS') == {'useApifyProxy': True, 'apifyProxyGroups': []} |
0 commit comments