Skip to content

Commit ccf4767

Browse files
committed
Run the example queries agains the local fuseki
0. skip the test if the sparql endpoints host is not available 1. prepare the local fuseki store with some data 2. execute the example queries
1 parent 28261ff commit ccf4767

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

examples/sparqlstore_example.py

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,53 @@
22
Simple examples showing how to use the SPARQLStore
33
"""
44

5+
import sys
6+
from urllib.request import urlopen
7+
58
from rdflib import Graph, Namespace, URIRef
6-
from rdflib.plugins.stores.sparqlstore import SPARQLStore
7-
from rdflib.term import Identifier
9+
from rdflib.namespace import RDF, SKOS
10+
from rdflib.plugins.stores.sparqlstore import SPARQLStore, SPARQLUpdateStore
11+
from rdflib.term import Identifier, Literal
12+
13+
# Shows examples of the useage of SPARQLStore and SPARQLUpdateStore against local SPARQL1.1 endpoint if
14+
# available. This assumes SPARQL1.1 query/update endpoints running locally at
15+
# http://localhost:3030/db/
16+
#
17+
# It uses the same endpoint as the test_dataset.py!
18+
#
19+
# For the tests here to run, you can for example start fuseki with:
20+
# ./fuseki-server --mem --update /db
21+
22+
# THIS WILL ADD DATA TO THE /db dataset
23+
24+
25+
HOST = "http://localhost:3030"
826

927
if __name__ == "__main__":
28+
try:
29+
assert len(urlopen(HOST).read()) > 0
30+
except Exception:
31+
print(f"{HOST} is unavailable.")
32+
sys.exit(126)
33+
1034
dbo = Namespace("http://dbpedia.org/ontology/")
35+
dbr = Namespace("http://dbpedia.org/resource/")
36+
37+
# EXAMPLE Update Store:
38+
update_store = SPARQLUpdateStore(
39+
query_endpoint="http://localhost:3030/db/sparql",
40+
update_endpoint="http://localhost:3030/db/update",
41+
)
42+
graph = Graph(store=update_store, identifier="http://dbpedia.org")
43+
graph.add((dbr.Berlin, dbo.populationTotal, Literal(3)))
44+
graph.add((dbr.Brisbane, dbo.populationTotal, Literal(2)))
45+
graph.add((dbr["Category:Capitals_in_Europe"], RDF.type, SKOS.Concept))
46+
graph.add((dbr["Category:Holy_Grail"], RDF.type, SKOS.Concept))
47+
graph.add((dbr["Category:Hospital_ships_of_Japan"], RDF.type, SKOS.Concept))
1148

12-
# EXAMPLE 1: using a Graph with the Store type string set to "SPARQLStore"
49+
# EXAMPLE Store 1: using a Graph with the Store type string set to "SPARQLStore"
1350
graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
14-
graph.open("http://dbpedia.org/sparql")
51+
graph.open("http://localhost:3030/db/sparql")
1552

1653
pop = graph.value(URIRef("http://dbpedia.org/resource/Berlin"), dbo.populationTotal)
1754
assert isinstance(pop, Identifier)
@@ -23,33 +60,43 @@
2360
)
2461
print()
2562

26-
# EXAMPLE 2: using a SPARQLStore object directly
27-
st = SPARQLStore(query_endpoint="http://dbpedia.org/sparql")
63+
# EXAMPLE Query 2: using a SPARQLStore object directly
64+
st = SPARQLStore(query_endpoint="http://localhost:3030/db/sparql")
2865

2966
for p in st.objects(
3067
URIRef("http://dbpedia.org/resource/Brisbane"), dbo.populationTotal
3168
):
3269
assert isinstance(p, Identifier)
33-
print(
34-
"According to DBPedia, Brisbane has a population of " "{0}".format(int(p))
35-
)
70+
print("According to DBPedia, Brisbane has a population of {0}".format(int(p)))
3671
print()
3772

38-
# EXAMPLE 3: doing RDFlib triple navigation using SPARQLStore as a Graph()
73+
# EXAMPLE Query 3: doing RDFlib triple navigation using SPARQLStore as a Graph()
3974
print("Triple navigation using SPARQLStore as a Graph():")
4075
graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
41-
graph.open("http://dbpedia.org/sparql")
76+
graph.open("http://localhost:3030/db/sparql")
77+
# we are asking DBPedia for 3 skos:Concept instances
78+
count = 0
79+
80+
for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept):
81+
count += 1
82+
print(f"\t- {s}")
83+
if count >= 3:
84+
break
85+
86+
# EXAMPLE Query 4: doing RDFlib triple navigation using a Graph() with a SPARQLStore backend
87+
print("Triple navigation using a Graph() with a SPARQLStore backend:")
88+
st = SPARQLStore(query_endpoint="http://localhost:3030/db/sparql")
89+
graph = Graph(store=st)
4290
# we are asking DBPedia for 3 skos:Concept instances
4391
count = 0
44-
from rdflib.namespace import RDF, SKOS
4592

4693
for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept):
4794
count += 1
4895
print(f"\t- {s}")
4996
if count >= 3:
5097
break
5198

52-
# EXAMPLE 4: using a SPARQL endpoint that requires Basic HTTP authentication
99+
# EXAMPLE Store 5: using a SPARQL endpoint that requires Basic HTTP authentication
53100
# NOTE: this example won't run since the endpoint isn't live (or real)
54101
sparql_store = SPARQLStore(
55102
query_endpoint="http://fake-sparql-endpoint.com/repository/x",

test/test_examples.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ def test_example(example_file: Path) -> None:
3939

4040
try:
4141
result.check_returncode()
42-
except subprocess.CalledProcessError:
43-
if (
44-
example_file.stem == "sparqlstore_example"
45-
and "http.client.RemoteDisconnected: Remote end closed connection without response"
46-
in result.stderr.decode("utf-8")
47-
):
48-
pytest.skip("this test uses dbpedia which is down sometimes")
42+
except subprocess.CalledProcessError as process_error:
43+
if process_error.returncode == 126:
44+
pytest.skip("This test returned 126 indikating to skip it.")
4945
raise

0 commit comments

Comments
 (0)