Skip to content

Commit

Permalink
fixes #119
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Jan 7, 2024
1 parent 92d9cef commit 8b17e52
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
9 changes: 9 additions & 0 deletions lodstorage/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,19 @@ def rawQuery(self, queryString, method=POST):
Returns:
list: the raw query result as bindings
"""
queryString=self.fix_comments(queryString)
self.sparql.setQuery(queryString)
self.sparql.method = method
queryResult = self.sparql.query()
return queryResult


def fix_comments(self, query_string: str) -> str:
"""
make sure broken SPARQLWrapper will find comments
"""
return "#\n"+query_string


def getValue(self, sparqlQuery: str, attr: str):
"""
Expand Down
45 changes: 43 additions & 2 deletions tests/testSPARQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def getJena(self, mode="query", debug=False, typedLiterals=False, profile=False)
profile=profile,
)
return jena

def get_wikidata_endpoint(self)->SPARQL:
"""
get the default wikidata query service endpoint
"""
endpoint = "https://query.wikidata.org/sparql"
wd = SPARQL(endpoint)
return wd

def testJenaQuery(self):
"""
Expand Down Expand Up @@ -272,8 +280,7 @@ def testWikdata(self):
# if getpass.getuser()=="wf":
# # use 2018 wikidata copy
# endpoint="http://jena.zeus.bitplan.com/wikidata/"
endpoint = "https://query.wikidata.org/sparql"
wd = SPARQL(endpoint)
wd=self.get_wikidata_endpoint()
queryString = """# get a list of whisky distilleries
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
Expand Down Expand Up @@ -409,6 +416,40 @@ def test_query_with_authentication(self):
qres = sparql.queryAsListOfDicts(query)
self.assertEqual(2, len(qres))

def testIssue199(self):
"""
test https://github.com/WolfgangFahl/pyLoDStorage/issues/119
405 (Method not allowed) from endpoints for tt.genWdPropertyStatistic #
"""
wd=self.get_wikidata_endpoint()
sparql_query="""# Count all Q44613:monastery items
# with the given street address(P6375) https://www.wikidata.org/wiki/Property:P6375
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?count (COUNT(?count) AS ?frequency) WHERE {{
SELECT ?item ?itemLabel (COUNT (?value) AS ?count)
WHERE
{
# instance of monastery
?item wdt:P31 wd:Q44613.
?item rdfs:label ?itemLabel.
FILTER (LANG(?itemLabel) = "en").
# street address
?item wdt:P6375 ?value.
} GROUP BY ?item ?itemLabel
}}
GROUP BY ?count
ORDER BY DESC (?frequency)
"""
lod=wd.queryAsListOfDicts(sparql_query)


if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
Expand Down
3 changes: 1 addition & 2 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,7 @@ def testIssue61(self):
if debug:
print(result)
self.assertTrue("WorksAndAuthor" in result)



class TestEndpoints(Basetest):
"""
tests Endpoint
Expand Down

0 comments on commit 8b17e52

Please sign in to comment.