Skip to content

Commit

Permalink
Add a non-whitespace pattern to SearchQueriesParam. (#74)
Browse files Browse the repository at this point in the history
* Add a non-whitespace pattern to SearchQueriesParam.

* Fix the regex.

* Improve the regex.

* Add a validation test for serp.
  • Loading branch information
wRAR authored Jan 13, 2025
1 parent 1b72aa8 commit 5a930c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/test_serp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from urllib.parse import quote_plus

import pytest
from pydantic import ValidationError
from scrapy import Request
from scrapy_spider_metadata import get_spider_metadata
from scrapy_zyte_api.responses import ZyteAPITextResponse
Expand Down Expand Up @@ -288,6 +289,7 @@ def test_metadata():
{"type": "null"},
],
"description": "Input 1 search query per line (e.g. foo bar).",
"pattern": r"(.|\r?\n)*\S+(.|\r?\n)*",
"title": "Search Queries",
"widget": "textarea",
},
Expand Down Expand Up @@ -764,3 +766,29 @@ def test_item_type_mappings():

# Also ensure that no dict value is repeated.
assert len(actual_keys) == len(set(ITEM_TYPE_CLASSES.values()))


@pytest.mark.parametrize(
"input_data,raises",
[
({"search_queries": "foo"}, False),
({"search_queries": "foo "}, False),
({"search_queries": " foo "}, False),
({"search_queries": " fo o "}, False),
({"search_queries": "fo o"}, False),
({"search_queries": "fo\n o "}, False),
({"search_queries": ["fo", " o "]}, False),
({"search_queries": ["fo", " "]}, False),
({"search_queries": " "}, True),
({"search_queries": ""}, True),
({"search_queries": " "}, True),
({"search_queries": " \n "}, True),
({"search_queries": [" ", " "]}, True),
],
)
def test_query_validation(input_data, raises):
if raises:
with pytest.raises(ValidationError):
GoogleSearchSpider(**input_data)
else:
GoogleSearchSpider(**input_data)
1 change: 1 addition & 0 deletions zyte_spider_templates/spiders/serp.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class SearchQueriesParam(BaseModel):
description="Input 1 search query per line (e.g. foo bar).",
json_schema_extra={
"widget": "textarea",
"pattern": r"(.|\r?\n)*\S+(.|\r?\n)*",
},
)

Expand Down

0 comments on commit 5a930c7

Please sign in to comment.