Skip to content

Commit

Permalink
Merge pull request #30 from meaningfy-ws/feature/MDR-327
Browse files Browse the repository at this point in the history
Added prefixes support
  • Loading branch information
mclaurentiu authored Dec 14, 2020
2 parents 003159b + 9cc7503 commit 770c89c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
8 changes: 6 additions & 2 deletions eds4jinja2/adapters/local_sparql_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, filename):
def __reduce_bound_triple_to_string_format(self, dict_of_bound_variables: dict):
return {str(k): str(v) for k, v in dict_of_bound_variables.items()}

def with_query(self, sparql_query: str, substitution_variables: dict = None) -> 'RDFFileDataSource':
def with_query(self, sparql_query: str, substitution_variables: dict = None, prefixes: str = "") -> 'RDFFileDataSource':
"""
Set the query text and return the reference to self for chaining.
:return:
Expand All @@ -44,9 +44,11 @@ def with_query(self, sparql_query: str, substitution_variables: dict = None) ->
else:
self.__query__ = sparql_query

self.__query__ = (prefixes + " " + self.__query__).strip()

return self

def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None) -> 'RDFFileDataSource':
def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None, prefixes: str = "") -> 'RDFFileDataSource':
"""
Set the query text and return the reference to self for chaining.
:return:
Expand All @@ -61,6 +63,8 @@ def with_query_from_file(self, sparql_query_file_path: str, substitution_variabl
template = SubstitutionTemplate(self.__query__)
self.__query__ = template.safe_substitute(substitution_variables)

self.__query__ = (prefixes + " " + self.__query__).strip()

return self

def with_file(self, file: str) -> 'RDFFileDataSource':
Expand Down
16 changes: 10 additions & 6 deletions eds4jinja2/adapters/remote_sparql_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, endpoint_url):
self.__can_be_tree = True
self.__can_be_tabular = True

def with_query(self, sparql_query: str, substitution_variables: dict = None) -> 'RemoteSPARQLEndpointDataSource':
def with_query(self, sparql_query: str, substitution_variables: dict = None, sparql_prefixes: str = "") -> 'RemoteSPARQLEndpointDataSource':
"""
Set the query text and return the reference to self for chaining.
:return:
Expand All @@ -59,10 +59,12 @@ def with_query(self, sparql_query: str, substitution_variables: dict = None) ->
template_query = SubstitutionTemplate(sparql_query)
sparql_query = template_query.safe_substitute(substitution_variables)

self.endpoint.setQuery(sparql_query)
new_query = (sparql_prefixes + " " + sparql_query).strip()

self.endpoint.setQuery(new_query)
return self

def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None) -> 'RemoteSPARQLEndpointDataSource':
def with_query_from_file(self, sparql_query_file_path: str, substitution_variables: dict = None, prefixes: str = "") -> 'RemoteSPARQLEndpointDataSource':
"""
Set the query text and return the reference to self for chaining.
:return:
Expand All @@ -75,7 +77,9 @@ def with_query_from_file(self, sparql_query_file_path: str, substitution_variabl
template_query = SubstitutionTemplate(query_from_file)
query_from_file = template_query.safe_substitute(substitution_variables)

self.endpoint.setQuery(query_from_file)
new_query = (prefixes + " " + query_from_file).strip()

self.endpoint.setQuery(new_query)
return self

def with_uri(self, uri: str, graph_uri: Optional[str] = None) -> 'RemoteSPARQLEndpointDataSource':
Expand All @@ -90,15 +94,15 @@ def with_uri(self, uri: str, graph_uri: Optional[str] = None) -> 'RemoteSPARQLEn
return self

def _fetch_tree(self):
if not self.endpoint.queryString:
if not self.endpoint.queryString or self.endpoint.queryString.isspace():
raise Exception("The query is empty.")

self.endpoint.setReturnFormat(JSON)
query = self.endpoint.query()
return query.convert()

def _fetch_tabular(self):
if not self.endpoint.queryString:
if not self.endpoint.queryString or self.endpoint.queryString.isspace():
raise Exception("The query is empty.")

self.endpoint.setReturnFormat(CSV)
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_local_sparql_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ def test_load_local_query_from_file_sparql_fetch_tabular():
local_rdf_ds.fetch_tabular()


def test_load_local_query_from_file_and_prefixes():
local_rdf_ds = RDFFileDataSource(
str(pathlib.Path("../test_data/shacl.example.shapes.ttl")))

local_rdf_ds.with_query_from_file(pathlib.Path("./tests/test_data/queries/spo_limit_10.txt"), None, "PREFIX TEST")
assert local_rdf_ds.__query__.startswith("PREFIX TEST")


def test_load_local_query_and_prefixes():
local_rdf_ds = RDFFileDataSource(
str(pathlib.Path("../test_data/shacl.example.shapes.ttl")))

local_rdf_ds.with_query("""SELECT *
WHERE {
?s ?p ?o
}
limit 10""", None, "PREFIX TEST")
assert local_rdf_ds.__query__.startswith("PREFIX TEST")


def test_load_local_query_from_file_substitution():
local_rdf_ds = RDFFileDataSource(
str(pathlib.Path("../test_data/shacl.example.shapes.ttl")))
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_sparql_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ def test_query_endpoint_and_fetch_tabular():
assert error is None


def test_query_and_prefixes():
fds = RemoteSPARQLEndpointDataSource(ENDPOINT_REMOTE_CORRECT)
fds.with_query(SPO_LIMIT_10, None, "PREFIX TEST")
assert fds.endpoint.queryString.startswith("PREFIX TEST")


def test_query_from_file_and_prefixes():
fds = RemoteSPARQLEndpointDataSource(ENDPOINT_REMOTE_CORRECT)
fds.with_query_from_file(
pathlib.Path("./tests/test_data/queries/spo_limit_10.txt"), None, "PREFIX TEST")
assert fds.endpoint.queryString.startswith("PREFIX TEST")


def test_query_endpoint_and_fetch_tabular_without_query():
fds = RemoteSPARQLEndpointDataSource(ENDPOINT_REMOTE_CORRECT)
result, error_string = fds.with_query("").fetch_tabular()
Expand Down

0 comments on commit 770c89c

Please sign in to comment.