Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize the spider params schema. #76

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sh_scrapy/commands/shub_image_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
result['metadata'] = {}
for spider_name in result['spiders']:
spider_cls = self.crawler_process.spider_loader.load(spider_name)
metadata_dict = get_spider_metadata(spider_cls)
metadata_dict = get_spider_metadata(spider_cls, normalize=True)

Check warning on line 48 in sh_scrapy/commands/shub_image_info.py

View check run for this annotation

Codecov / codecov/patch

sh_scrapy/commands/shub_image_info.py#L48

Added line #L48 was not covered by tests
try:
# make sure it's serializable
json.dumps(metadata_dict)
Expand Down
92 changes: 92 additions & 0 deletions tests/test_crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,95 @@ class MySpider(Spider):
if not SPIDER_METADATA_AVAILABLE:
del expected["metadata"]
assert data == expected


@pytest.mark.skipif(not SPIDER_METADATA_AVAILABLE, reason="scrapy-spider-metadata is not installed")
def test_image_info_args(tmp_path):
project_dir = create_project(tmp_path, spider_text="""
from enum import Enum
from scrapy import Spider
from scrapy_spider_metadata import Args
from pydantic import BaseModel, Field

class ToolEnum(Enum):
spanner = "spanner"
wrench = "wrench"

class Parameters(BaseModel):
tool: ToolEnum = ToolEnum.spanner

class MySpider(Args[Parameters], Spider):
name = "myspider"
""")
out, _ = call_command(project_dir, "shub-image-info")
data = json.loads(out)
expected = {
"project_type": "scrapy",
"spiders": ["myspider"],
"metadata": {
"myspider": {
"param_schema": {
"properties": {
"tool": {
"default": "spanner",
"enum": ["spanner", "wrench"],
"title": "Tool",
"type": "string",
},
},
"title": "Parameters",
"type": "object",
},
},
},
}
if not SPIDER_METADATA_AVAILABLE:
del expected["metadata"]
assert data == expected


@pytest.mark.skipif(not SPIDER_METADATA_AVAILABLE, reason="scrapy-spider-metadata is not installed")
def test_image_info_args_metadata(tmp_path):
project_dir = create_project(tmp_path, spider_text="""
from enum import Enum
from scrapy import Spider
from scrapy_spider_metadata import Args
from pydantic import BaseModel, Field

class ToolEnum(Enum):
spanner = "spanner"
wrench = "wrench"

class Parameters(BaseModel):
tool: ToolEnum = ToolEnum.spanner

class MySpider(Args[Parameters], Spider):
name = "myspider"
metadata = {"foo": 42}
""")
out, _ = call_command(project_dir, "shub-image-info")
data = json.loads(out)
expected = {
"project_type": "scrapy",
"spiders": ["myspider"],
"metadata": {
"myspider": {
"foo": 42,
"param_schema": {
"properties": {
"tool": {
"default": "spanner",
"enum": ["spanner", "wrench"],
"title": "Tool",
"type": "string",
},
},
"title": "Parameters",
"type": "object",
},
},
},
}
if not SPIDER_METADATA_AVAILABLE:
del expected["metadata"]
assert data == expected
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ deps =
hubstorage
packaging
py36-scrapy16: Scrapy==1.6
scrapy-spider-metadata; python_version >= "3.8"
scrapy-spider-metadata>=0.1.1; python_version >= "3.8"
pydantic>=2; python_version >= "3.8"

commands =
pytest --verbose --cov=sh_scrapy --cov-report=term-missing --cov-report=html --cov-report=xml {posargs: sh_scrapy tests}
Loading