Skip to content

Commit

Permalink
Merge pull request #346 from candleindark/handle-web-ui-empty-query
Browse files Browse the repository at this point in the history
Handle empty search query string in web UI
  • Loading branch information
yarikoptic authored Mar 29, 2024
2 parents 55e067e + c70b3b9 commit 4da200b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion datalad_registry/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def overview(): # No type hints due to mypy#7187.
# as we would add search to individual files.
query = request.args.get("query", None, type=str)
search_error = None
if query:
if query is not None:
lgr.debug("Search by '%s'", query)
try:
criteria = parse_query(query)
Expand Down
2 changes: 1 addition & 1 deletion datalad_registry/templates/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<div class="content">
<form action="{{ url_for('.overview') }}" method="get">
<label for='search' class="sr-only">Search</label>
<input id='search' type='search' name='query'
<input id='search' type='search' name='query' required pattern=".*\S.*"
{%- if search_query %} value="{{ search_query|escape }}"
{%- endif -%}
placeholder='Search query'
Expand Down
17 changes: 11 additions & 6 deletions datalad_registry/tests/test_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,19 @@ def test_search_with_valid_query(

@pytest.mark.usefixtures("populate_with_dataset_urls")
@pytest.mark.parametrize(
"search_query",
"search_query, err_msg_prefix",
[
"unknown_field:example",
("unknown_field:example", "Unknown field: 'unknown_field'. Known are:"),
("", "Query string cannot be empty"),
(" \t \n", "Query string cannot contain only whitespace"),
(" ", "Query string cannot contain only whitespace"),
(" ", "Query string cannot contain only whitespace"),
(" \t \n \t ", "Query string cannot contain only whitespace"),
],
)
def test_search_with_invalid_query(self, search_query: Optional[str], flask_client):
def test_search_with_invalid_query(
self, search_query: str, err_msg_prefix: str, flask_client
):
"""
Test searching with an invalid query
"""
Expand All @@ -197,9 +204,7 @@ def test_search_with_invalid_query(self, search_query: Optional[str], flask_clie
soup = BeautifulSoup(resp.text, "html.parser")

assert (error_span := soup.find("span", class_="error"))
assert error_span.text.startswith(
"ERROR: Unknown field: 'unknown_field'. Known are:"
)
assert error_span.text.startswith(f"ERROR: {err_msg_prefix}")

def test_pagination(self, populate_with_dataset_urls, flask_client):
"""
Expand Down

0 comments on commit 4da200b

Please sign in to comment.