Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions tests/backends/test_sparqlwrapper_graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,16 @@ def test_graphdb():

# Test SELECT query
query_object = (
"SELECT ?s ?p ?o WHERE { <http://www.example.org/subject> ?p ?o }"
"SELECT ?p ?o WHERE { <http://www.example.org/subject> ?p ?o }"
)

# Run the query using your triplestore instance.
result = ts.query(query_object)

assert set(
[
("http://www.example.org/predicate", "a"),
("http://www.example.org/predicate", "1.0"),
]
) == set(result)
assert set(result) == {
("http://www.example.org/predicate", Literal("a")),
("http://www.example.org/predicate", Literal(1.0)),
}

# Test CONSTRUCT query
# NB adding the PREFIX just to show that it works.
Expand All @@ -100,12 +98,17 @@ def test_graphdb():
"""

# Run the query.
result_generator = ts.query(query)
results = set(ts.query(query))
assert (
"http://www.example.org/subject",
"http://www.example.org/predicate",
Literal("a"),
) in results
assert (
"http://www.example.org/subject",
"http://www.example.org/predicate",
"a",
) in result_generator
Literal(1.0),
) in results

# Test ASK query
query = """
Expand Down
9 changes: 4 additions & 5 deletions tripper/backends/sparqlwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING

from tripper import Literal
from tripper.backends.rdflib import _convert_triples_to_tripper
from tripper.utils import TripperException

try:
Expand Down Expand Up @@ -93,15 +94,15 @@
ret = self.sparql.queryAndConvert()
bindings = ret["results"]["bindings"]
return [
tuple(str(convert_json_entrydict(v)) for v in row.values())
tuple(convert_json_entrydict(v) for v in row.values())
for row in bindings
]
if query_type == "CONSTRUCT":
self.sparql.setReturnFormat(RDFXML)
self.sparql.setMethod(POST)
self.sparql.setQuery(query_object)
graph = self.sparql.queryAndConvert()
return ((str(s), str(p), str(o)) for s, p, o in graph)
return _convert_triples_to_tripper(graph)

Check warning on line 105 in tripper/backends/sparqlwrapper.py

View check run for this annotation

Codecov / codecov/patch

tripper/backends/sparqlwrapper.py#L105

Added line #L105 was not covered by tests

raise NotImplementedError(
f"Query type '{query_type}' not implemented."
Expand Down Expand Up @@ -224,19 +225,17 @@
)


def convert_json_entrydict(entrydict: "Dict[str, str]") -> str:
def convert_json_entrydict(entrydict: dict) -> str:
"""Convert SPARQLWrapper json entry dict (representing a single IRI or
literal) to a tripper type."""
if entrydict["type"] == "uri":
return entrydict["value"]

if entrydict["type"] == "literal":
return Literal(
entrydict["value"],
lang=entrydict.get("xml:lang"),
datatype=entrydict.get("datatype"),
)

if entrydict["type"] == "bnode":
return (
entrydict["value"]
Expand Down