From 3393741b3cde0521f3f5e1623fdc1d762820ca87 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 14 Sep 2023 16:17:36 +1000 Subject: [PATCH] Black code --- prez/models/search_method.py | 2 +- prez/reference_data/prez_ns.py | 2 +- prez/renderers/renderer.py | 2 +- prez/routers/search.py | 28 ++++++++-------- prez/sparql/methods.py | 8 ++--- tests/search/test_search.py | 59 +++++++++++++++++++--------------- 6 files changed, 55 insertions(+), 46 deletions(-) diff --git a/prez/models/search_method.py b/prez/models/search_method.py index 54ef5ab0..a9e5e1eb 100644 --- a/prez/models/search_method.py +++ b/prez/models/search_method.py @@ -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, } ) diff --git a/prez/reference_data/prez_ns.py b/prez/reference_data/prez_ns.py index 0855231f..25a7f5fd 100644 --- a/prez/reference_data/prez_ns.py +++ b/prez/reference_data/prez_ns.py @@ -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#") \ No newline at end of file +REG = Namespace("http://purl.org/linked-data/registry#") diff --git a/prez/renderers/renderer.py b/prez/renderers/renderer.py index a8d85870..3f47bc95 100644 --- a/prez/renderers/renderer.py +++ b/prez/renderers/renderer.py @@ -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__) diff --git a/prez/routers/search.py b/prez/routers/search.py index 18d9dd27..86dc4c6a 100644 --- a/prez/routers/search.py +++ b/prez/routers/search.py @@ -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) @@ -34,7 +34,7 @@ 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 = "" @@ -42,23 +42,25 @@ async def search( 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") diff --git a/prez/sparql/methods.py b/prez/sparql/methods.py index e8dd4117..c6bf46e5 100644 --- a/prez/sparql/methods.py +++ b/prez/sparql/methods.py @@ -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__) @@ -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"] @@ -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"] diff --git a/tests/search/test_search.py b/tests/search/test_search.py index ecfa2a2f..c51d859f 100644 --- a/tests/search/test_search.py +++ b/tests/search/test_search.py @@ -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)}" @@ -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)}" @@ -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"}, [], []), ],