-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit-tests: Extension and middleware base test setup
- Loading branch information
Wesley van Lee
committed
Oct 11, 2024
1 parent
47bd64f
commit c20c5a7
Showing
8 changed files
with
154 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Example data was generated using https://github.com/webrecorder/archiveweb.page v0.12.8. The data in this folder is based on the WARC Format 1.1 (https://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1/) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from scrapy.exceptions import NotConfigured | ||
from scrapy.pipelines.files import FSFilesStore, FTPFilesStore, GCSFilesStore, S3FilesStore | ||
from scrapy.utils.test import get_crawler | ||
|
||
from scrapy_webarchive.extensions import WaczExporter | ||
|
||
|
||
class TestWaczExporterExtension: | ||
def test_archive_export_uri_invalid_raises_not_configured(self): | ||
crawler = get_crawler(settings_dict={}) | ||
with pytest.raises(NotConfigured): | ||
WaczExporter.from_crawler(crawler) | ||
|
||
@mock.patch('scrapy_webarchive.extensions.S3FilesStore.__init__', return_value=None) | ||
@mock.patch('scrapy_webarchive.extensions.GCSFilesStore.__init__', return_value=None) | ||
@mock.patch('scrapy_webarchive.extensions.FTPFilesStore.__init__', return_value=None) | ||
@mock.patch('scrapy_webarchive.extensions.FSFilesStore.__init__', return_value=None) | ||
def test_get_store(self, *args): | ||
crawler = get_crawler(settings_dict={"ARCHIVE_EXPORT_URI": "/tmp/scrapy-webarchive/wacz/"}) | ||
crawler.spider = crawler._create_spider("quotes") | ||
extension = WaczExporter.from_crawler(crawler) | ||
assert isinstance(extension.store, FSFilesStore) | ||
|
||
crawler = get_crawler(settings_dict={"ARCHIVE_EXPORT_URI": "s3://scrapy-webarchive/wacz/"}) | ||
crawler.spider = crawler._create_spider("quotes") | ||
extension = WaczExporter.from_crawler(crawler) | ||
assert isinstance(extension.store, S3FilesStore) | ||
|
||
crawler = get_crawler(settings_dict={"ARCHIVE_EXPORT_URI": "gs://scrapy-webarchive/wacz/"}) | ||
crawler.spider = crawler._create_spider("quotes") | ||
extension = WaczExporter.from_crawler(crawler) | ||
assert isinstance(extension.store, GCSFilesStore) | ||
|
||
crawler = get_crawler(settings_dict={"ARCHIVE_EXPORT_URI": "ftp://scrapy-webarchive/wacz/"}) | ||
crawler.spider = crawler._create_spider("quotes") | ||
extension = WaczExporter.from_crawler(crawler) | ||
assert isinstance(extension.store, FTPFilesStore) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from contextlib import contextmanager | ||
|
||
from scrapy.http.request import Request | ||
from scrapy.settings import Settings | ||
from scrapy.utils.test import get_crawler | ||
|
||
from scrapy_webarchive.middleware import WaczCrawlMiddleware | ||
|
||
from . import get_test_data_path | ||
|
||
|
||
class TestWaczCrawlMiddlewareWarc11: | ||
def setup_method(self): | ||
self.crawler = get_crawler() | ||
self.spider = self.crawler._create_spider("quotes") | ||
|
||
def _get_settings(self, **new_settings): | ||
settings = { | ||
"WACZ_SOURCE_URL": get_test_data_path("warc_1_1", "quotes.wacz.gz").as_uri(), | ||
"WACZ_TIMEOUT": 60, | ||
} | ||
settings.update(new_settings) | ||
return Settings(settings) | ||
|
||
@contextmanager | ||
def _middleware(self, **new_settings): | ||
settings = self._get_settings(**new_settings) | ||
mw = WaczCrawlMiddleware(settings, self.crawler.stats) | ||
mw.spider_opened(self.spider) | ||
yield mw | ||
|
||
def test_wacz_archive_is_ignored_follow_original_behaviour(self): | ||
request = Request("https://quotes.toscrape.com") | ||
|
||
with self._middleware(WACZ_CRAWL=False) as mw: | ||
out = list(mw.process_start_requests([request], self.spider)) | ||
assert out == [request] | ||
|
||
def test_wacz_archive_iterates_all_records(self): | ||
with self._middleware(WACZ_CRAWL=True) as mw: | ||
out = list(mw.process_start_requests([], self.spider)) | ||
assert len(out) == 101 | ||
|
||
def test_wacz_archive_filters_allowed_domains(self): | ||
setattr(self.spider, "allowed_domains", "quotes.toscrape.com") | ||
|
||
with self._middleware(WACZ_CRAWL=True) as mw: | ||
out = list(mw.process_start_requests([], self.spider)) | ||
assert len(out) == 61 | ||
|
||
def test_wacz_archive_filters_archive_regex(self): | ||
setattr(self.spider, "archive_regex", r"https://quotes\.toscrape\.com/page/\d+/") | ||
|
||
with self._middleware(WACZ_CRAWL=True) as mw: | ||
out = list(mw.process_start_requests([], self.spider)) | ||
assert len(out) == 9 |