Skip to content

Commit

Permalink
Merge pull request #319 from candleindark/enh-search-review
Browse files Browse the repository at this point in the history
Modify tests organization
  • Loading branch information
candleindark authored Feb 28, 2024
2 parents c38fdba + 1bdd6a3 commit 209d424
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
8 changes: 4 additions & 4 deletions datalad_registry/tests/test_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ def test_sorting(
("datalad AND handbook", ["https://handbook.datalad.org"]),
],
)
def test_search_query(
def test_search_with_valid_query(
self, search_query: Optional[str], expected_results: list[str], flask_client
):
"""
Test for the filtering of dataset URLs in the overview page
Test searching with a valid query
"""

resp = flask_client.get("/overview/", query_string={"query": search_query})
Expand All @@ -187,9 +187,9 @@ def test_search_query(
"unknown_field:example",
],
)
def test_search_query_error(self, search_query: Optional[str], flask_client):
def test_search_with_invalid_query(self, search_query: Optional[str], flask_client):
"""
Test for the filtering of dataset URLs in the overview page
Test searching with an invalid query
"""

resp = flask_client.get("/overview/", query_string={"query": search_query})
Expand Down
69 changes: 45 additions & 24 deletions datalad_registry/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# This test module contains fixtures and tests for the search functionality
# enabled by the datalad_registry/search.py module.

from lark.exceptions import VisitError
import pytest
from sqlalchemy import select
Expand All @@ -7,24 +10,6 @@
from ..search import parse_query


@pytest.mark.parametrize(
"query, exc, err",
[
("unknown_field:example", ValueError, None),
# Lark masks exceptions. We did not provide dedicated ones for all
# of them, but let's test that error message as expected
("ds_id:=example", VisitError, "Operation = is not implemented"),
# r'(haxby or halchenko) AND metadata:BIDSmetadata[bids_dataset,metalad_core]:'
# r'"BIDSVersion\": \"v"',
],
)
def test_search_errors(query, exc, err):
with pytest.raises(exc) as ce:
parse_query(query)
if err:
assert err in str(ce.value)


@pytest.fixture
def populate_with_url_metadata_for_search(
populate_with_dataset_urls, # noqa: U100 (unused argument)
Expand Down Expand Up @@ -69,6 +54,28 @@ def populate_with_url_metadata_for_search(
db.session.commit()


@pytest.mark.parametrize(
"query, exc, err",
[
("unknown_field:example", ValueError, None),
# Lark masks exceptions. We did not provide dedicated ones for all
# of them, but let's test that error message as expected
("ds_id:=example", VisitError, "Operation = is not implemented"),
# r'(haxby or halchenko) AND '
# r'metadata:BIDSmetadata[bids_dataset,metalad_core]:'
# r'"BIDSVersion\": \"v"',
],
)
def test_with_invalid_query(query, exc, err):
"""
Test the search functionality in handling invalid queries
"""
with pytest.raises(exc) as ce:
parse_query(query)
if err:
assert err in str(ce.value)


@pytest.mark.usefixtures("populate_with_url_metadata_for_search")
@pytest.mark.parametrize(
"query, expected",
Expand Down Expand Up @@ -100,13 +107,22 @@ def populate_with_url_metadata_for_search(
("handbook OR ds_id:datalad", [3]),
("url:handbook OR ds_id:datalad", [3]),
("url:handbook OR ds_id:844c", [1, 2, 3]),
("(url:handbook OR metadata[metalad_core]:meta1value) AND ds_id:844c", [2, 3]),
(
"(url:handbook OR metadata[metalad_core]:meta1value) AND ds_id:844c",
[2, 3],
),
(
"(url:?handbook OR metadata[metalad_core]:?meta1value) AND ds_id:?844c",
[2, 3],
),
("(url:handbook OR metadata[metalad_core]:meta3value) AND ds_id:844C", [1, 3]),
("(url:handbook OR metadata[metalad_core]:value) AND ds_id:844c", [1, 2, 3]),
(
"(url:handbook OR metadata[metalad_core]:meta3value) AND ds_id:844C",
[1, 3],
),
(
"(url:handbook OR metadata[metalad_core]:value) AND ds_id:844c",
[1, 2, 3],
),
(
"(url:handbook OR metadata[metalad_core]:value) ds_id:844c",
[1, 2, 3],
Expand All @@ -123,14 +139,19 @@ def populate_with_url_metadata_for_search(
('metadata[metalad_core,metalad_studyminimeta,unknown]:"value"', [1, 2]),
# Prototypical query for which we do not have full support yet, e.g.
# regex matching :~
# (r"""((jim AND NOT haxby AND "important\" paper") OR ds_id:~"^000[3-9]..$"
# OR url:"example.com") AND metadata:non AND metadata[ex1,ex2]:"specific data"
# (r"""((jim AND NOT haxby AND "important\" paper") OR
# ds_id:~"^000[3-9]..$"
# OR url:"example.com") AND metadata:non
# AND metadata[ex1,ex2]:"specific data"
# AND metadata[extractor2]:data""", []),
# Find datasets with the last
# (metadata[bids_dataset][Authors][-1]:haxby ...)
],
)
def test_search_cases(flask_app, query, expected):
def test_with_valid_query(flask_app, query, expected):
"""
Test the search functionality in handling valid queries
"""
r = parse_query(query)
# print(f"QUERY {query}: {r}")
with flask_app.app_context():
Expand Down

0 comments on commit 209d424

Please sign in to comment.