Skip to content

Commit

Permalink
Black code
Browse files Browse the repository at this point in the history
  • Loading branch information
recalcitrantsupplant committed Sep 14, 2023
1 parent 266bb5c commit 3393741
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 46 deletions.
2 changes: 1 addition & 1 deletion prez/models/search_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ def populate_query(self, term, limit, focus_to_filter, filter_to_focus, predicat
"LIMIT": limit,
"FOCUS_TO_FILTER": focus_to_filter,
"FILTER_TO_FOCUS": filter_to_focus,
"PREDICATES": predicates
"PREDICATES": predicates,
}
)
2 changes: 1 addition & 1 deletion prez/reference_data/prez_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
PREZ = Namespace("https://prez.dev/")
ONT = Namespace("https://prez.dev/ont/")
ALTREXT = Namespace("http://www.w3.org/ns/dx/conneg/altr-ext#")
REG = Namespace("http://purl.org/linked-data/registry#")
REG = Namespace("http://purl.org/linked-data/registry#")
2 changes: 1 addition & 1 deletion prez/renderers/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from prez.sparql.methods import send_queries, rdf_query_to_graph
from prez.sparql.objects_listings import (
generate_item_construct,
get_annotation_properties
get_annotation_properties,
)

log = logging.getLogger(__name__)
Expand Down
28 changes: 15 additions & 13 deletions prez/routers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@router.get("/search", summary="Global Search")
async def search(
request: Request,
request: Request,
):
term = request.query_params.get("term")
limit = request.query_params.get("limit", 20)
Expand All @@ -34,31 +34,33 @@ async def search(
return PlainTextResponse(
status_code=400,
content=f'Search method "{selected_method}" not found. Available methods are: '
f"{', '.join([str(m) for m in search_methods.keys()])}",
f"{', '.join([str(m) for m in search_methods.keys()])}",
)
search_query = search_methods[Literal(selected_method)].copy()
filter_to_focus_str = ""
focus_to_filter_str = ""

if filt_2_foc:
for idx, filter_pair in enumerate(filt_2_foc, start=1):
filter_values = ' '.join(f"<{f}>" for f in filter_pair[1].split(","))
filter_to_focus_str += (
f"""?filter_to_focus_{idx} <{filter_pair[0]}> ?search_result_uri.
filter_values = " ".join(f"<{f}>" for f in filter_pair[1].split(","))
filter_to_focus_str += f"""?filter_to_focus_{idx} <{filter_pair[0]}> ?search_result_uri.
VALUES ?filter_to_focus_{idx} {{ {filter_values} }}"""
)

if foc_2_filt:
for idx, filter_pair in enumerate(foc_2_filt, start=1):
filter_values = ' '.join(f"<{f}>" for f in filter_pair[1].split(","))
focus_to_filter_str += (
f"""?search_result_uri <{filter_pair[0]}> ?focus_to_filter_{idx}.
filter_values = " ".join(f"<{f}>" for f in filter_pair[1].split(","))
focus_to_filter_str += f"""?search_result_uri <{filter_pair[0]}> ?focus_to_filter_{idx}.
VALUES ?focus_to_filter_{idx} {{ {filter_values} }}"""
)

predicates = settings.label_predicates + settings.description_predicates + settings.provenance_predicates
predicates_sparql_string = ' '.join(f"<{p}>" for p in predicates)
search_query.populate_query(term, limit, filter_to_focus_str, focus_to_filter_str, predicates_sparql_string)
predicates = (
settings.label_predicates
+ settings.description_predicates
+ settings.provenance_predicates
)
predicates_sparql_string = " ".join(f"<{p}>" for p in predicates)
search_query.populate_query(
term, limit, filter_to_focus_str, focus_to_filter_str, predicates_sparql_string
)

full_query = generate_item_construct(
search_query, URIRef("https://prez.dev/profile/open")
Expand Down
8 changes: 4 additions & 4 deletions prez/sparql/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
auth=(settings.sparql_username, settings.sparql_password)
if settings.sparql_username
else None,
timeout=settings.sparql_timeout
timeout=settings.sparql_timeout,
)

client = Client(
auth=(settings.sparql_username, settings.sparql_password)
if settings.sparql_username
else None,
timeout=settings.sparql_timeout
timeout=settings.sparql_timeout,
)

log = logging.getLogger(__name__)
Expand All @@ -38,7 +38,7 @@ def sparql_query_non_async(query: str) -> Tuple[bool, Union[List, Dict]]:
headers={
"Accept": "application/json",
"Content-Type": "application/sparql-query",
}
},
)
if 200 <= response.status_code < 300:
return True, response.json()["results"]["bindings"]
Expand All @@ -58,7 +58,7 @@ def sparql_ask_non_async(query: str):
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate",
}
},
)
if 200 <= response.status_code < 300:
return True, response.json()["boolean"]
Expand Down
59 changes: 33 additions & 26 deletions tests/search/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ def test_search_filter_to_focus(test_client: TestClient):
)
assert isomorphic(expected_graph, response_graph)

@pytest.mark.xfail(reason="This generates a valid query that has been tested in Fuseki, which RDFLib struggles with")

@pytest.mark.xfail(
reason="This generates a valid query that has been tested in Fuseki, which RDFLib struggles with"
)
def test_search_filter_to_focus_multiple(test_client: TestClient):
base_url = "/search"
params = {
"term": "storage",
"method": "default",
"filter-to-focus[skos:broader]": "http://resource.geosciml.org/classifier/cgi/contacttype/metamorphic_contact,http://linked.data.gov.au/def/borehole-purpose/greenhouse-gas-storage"
"filter-to-focus[skos:broader]": "http://resource.geosciml.org/classifier/cgi/contacttype/metamorphic_contact,http://linked.data.gov.au/def/borehole-purpose/greenhouse-gas-storage",
}
# Constructing the final URL
final_url = f"{base_url}?{urlencode(params)}"
Expand All @@ -101,13 +104,16 @@ def test_search_filter_to_focus_multiple(test_client: TestClient):
)
assert isomorphic(expected_graph, response_graph)

@pytest.mark.xfail(reason="This generates a valid query that has been tested in Fuseki, which RDFLib struggles with")

@pytest.mark.xfail(
reason="This generates a valid query that has been tested in Fuseki, which RDFLib struggles with"
)
def test_search_focus_to_filter_multiple(test_client: TestClient):
base_url = "/search"
params = {
"term": "storage",
"method": "default",
"focus-to-filter[skos:broader]": "http://linked.data.gov.au/def/borehole-purpose/carbon-capture-and-storage,http://linked.data.gov.au/def/borehole-purpose/pggd"
"focus-to-filter[skos:broader]": "http://linked.data.gov.au/def/borehole-purpose/carbon-capture-and-storage,http://linked.data.gov.au/def/borehole-purpose/pggd",
}
# Constructing the final URL
final_url = f"{base_url}?{urlencode(params)}"
Expand All @@ -120,36 +126,37 @@ def test_search_focus_to_filter_multiple(test_client: TestClient):
)
assert isomorphic(expected_graph, response_graph)


@pytest.mark.parametrize(
"qsas, expected_focus_to_filter, expected_filter_to_focus",
[
(
{
"term": "value1",
"method": "value2",
"filter-to-focus[rdfs:member]": "value3",
"focus-to-filter[dcterms:title]": "value4",
},
[(URIRef("http://purl.org/dc/terms/title"), "value4")],
[(URIRef("http://www.w3.org/2000/01/rdf-schema#member"), "value3")],
{
"term": "value1",
"method": "value2",
"filter-to-focus[rdfs:member]": "value3",
"focus-to-filter[dcterms:title]": "value4",
},
[(URIRef("http://purl.org/dc/terms/title"), "value4")],
[(URIRef("http://www.w3.org/2000/01/rdf-schema#member"), "value3")],
),
(
{
"term": "value1",
"method": "value2",
"focus-to-filter[dcterms:title]": "value4",
},
[(URIRef("http://purl.org/dc/terms/title"), "value4")],
[],
{
"term": "value1",
"method": "value2",
"focus-to-filter[dcterms:title]": "value4",
},
[(URIRef("http://purl.org/dc/terms/title"), "value4")],
[],
),
(
{
"term": "value1",
"method": "value2",
"filter-to-focus[rdfs:member]": "value3",
},
[],
[(URIRef("http://www.w3.org/2000/01/rdf-schema#member"), "value3")],
{
"term": "value1",
"method": "value2",
"filter-to-focus[rdfs:member]": "value3",
},
[],
[(URIRef("http://www.w3.org/2000/01/rdf-schema#member"), "value3")],
),
({"term": "value1", "method": "value2"}, [], []),
],
Expand Down

0 comments on commit 3393741

Please sign in to comment.