-
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.
- Loading branch information
Showing
4 changed files
with
15 additions
and
63 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 |
---|---|---|
@@ -1,83 +1,35 @@ | ||
import json | ||
from unittest.mock import MagicMock, patch | ||
from unittest.mock import patch | ||
|
||
from scrapework.config import PipelineConfig | ||
from scrapework.spider import Spider | ||
|
||
|
||
# Create a concrete subclass of Spider for testing purposes | ||
class ConcreteSpider(Spider): | ||
name = "concrete_spider" | ||
|
||
def parse(self): | ||
pass | ||
|
||
|
||
def test_process_items_with_file_backend(): | ||
items = [{"name": "item1"}, {"name": "item2"}] | ||
config = PipelineConfig( | ||
backend=BackendType.FILE, | ||
base_url="https://example.com", | ||
s3_bucket="test-bucket", | ||
filename="test.json", | ||
) | ||
pipeline = ItemPipeline() | ||
|
||
with patch("builtins.open", MagicMock()) as mock_open: | ||
pipeline.process_items(items, config) | ||
|
||
mock_open.assert_called_once_with("test.json", "w") | ||
from scrapework.pipelines import JsonFilePipeline, S3Pipeline | ||
|
||
|
||
def test_process_items_with_s3_backend(): | ||
items = [{"name": "item1"}, {"name": "item2"}] | ||
config = PipelineConfig( | ||
backend=BackendType.S3, | ||
base_url="https://example.com", | ||
s3_bucket="my-bucket", | ||
filename="example.json", | ||
) | ||
pipeline = ItemPipeline() | ||
pipeline = S3Pipeline(s3_bucket="my-bucket") | ||
|
||
with patch("boto3.client") as mock_s3_client: | ||
pipeline.process_items(items, config) | ||
pipeline.process_items(items, config.filename) | ||
|
||
mock_s3_client.assert_called_once_with("s3") | ||
mock_s3_client.return_value.put_object.assert_called_once_with( | ||
Body=json.dumps(items), Bucket="my-bucket", Key="example.json" | ||
) | ||
|
||
|
||
def test_export_to_json(): | ||
def test_process_items_with_json_file_backend(): | ||
items = [{"name": "item1"}, {"name": "item2"}] | ||
config = PipelineConfig( | ||
backend=BackendType.FILE, | ||
base_url="https://example.com", | ||
s3_bucket="my-bucket", | ||
filename="example.json", | ||
) | ||
pipeline = ItemPipeline() | ||
|
||
with patch("builtins.open", MagicMock()) as mock_open: | ||
pipeline.export_to_json(items, config) | ||
|
||
mock_open.assert_called_once_with("example.json", "w") | ||
|
||
filename = "output.json" | ||
pipeline = JsonFilePipeline() | ||
|
||
def test_export_to_s3(): | ||
items = [{"name": "item1"}, {"name": "item2"}] | ||
config = PipelineConfig( | ||
backend=BackendType.S3, | ||
base_url="https://example.com", | ||
s3_bucket="my-bucket", | ||
filename="example.json", | ||
) | ||
pipeline = ItemPipeline() | ||
pipeline.process_items(items, filename) | ||
|
||
with patch("boto3.client") as mock_s3_client: | ||
pipeline.export_to_s3(items, config) | ||
with open(filename, "r") as f: | ||
data = json.load(f) | ||
|
||
mock_s3_client.assert_called_once_with("s3") | ||
mock_s3_client.return_value.put_object.assert_called_once_with( | ||
Body=json.dumps(items), Bucket="my-bucket", Key="example.json" | ||
) | ||
assert data == items |