Skip to content

Commit f073e69

Browse files
Create test_sparql_endpoint.py
1 parent 0381343 commit f073e69

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from string import Template
2+
3+
import pandas as pd
4+
import pytest
5+
import rdflib
6+
7+
from ted_sws.data_manager.adapters.sparql_endpoint import SPARQLStringEndpoint, TripleStoreEndpointABC, \
8+
SPARQLFileEndpoint
9+
from ted_sws.master_data_registry.resources import TRIPLES_BY_CET_URI_SPARQL_QUERY_TEMPLATE_PATH, \
10+
RDF_FRAGMENT_BY_URI_SPARQL_QUERY_TEMPLATE_PATH
11+
12+
RESULTS_DICT_KEY = "results"
13+
BINDINGS_DICT_KEY = "bindings"
14+
15+
16+
def sparql_endpoint_test_helper(sparql_endpoint: TripleStoreEndpointABC, uri: str):
17+
sparql_query = TRIPLES_BY_CET_URI_SPARQL_QUERY_TEMPLATE_PATH.read_text(encoding="utf-8")
18+
sparql_query = Template(sparql_query).substitute(uri=uri)
19+
query_table_result = sparql_endpoint.with_query(sparql_query=sparql_query).fetch_tabular()
20+
assert query_table_result is not None
21+
assert type(query_table_result) == pd.DataFrame
22+
assert len(query_table_result) == 3
23+
query_dict_result = sparql_endpoint.with_query(sparql_query=sparql_query).fetch_tree()
24+
assert query_dict_result is not None
25+
assert type(query_dict_result) == dict
26+
assert RESULTS_DICT_KEY in query_dict_result.keys()
27+
assert BINDINGS_DICT_KEY in query_dict_result[RESULTS_DICT_KEY].keys()
28+
assert len(query_dict_result[RESULTS_DICT_KEY][BINDINGS_DICT_KEY]) == 3
29+
with pytest.raises(Exception) as exc_info:
30+
sparql_endpoint.with_query(sparql_query=sparql_query).fetch_rdf()
31+
assert str(exc_info.value) == "Fetch RDF method work only with CONSTRUCT and DESCRIBE sparql queries!"
32+
sparql_query = RDF_FRAGMENT_BY_URI_SPARQL_QUERY_TEMPLATE_PATH.read_text(encoding="utf-8")
33+
sparql_query = Template(sparql_query).substitute(uri=uri)
34+
query_rdf_result = sparql_endpoint.with_query(sparql_query=sparql_query).fetch_rdf()
35+
assert query_rdf_result is not None
36+
assert type(query_rdf_result) == rdflib.Graph
37+
38+
def test_sparql_file_endpoint(rdf_file_path, organisation_cet_uri):
39+
sparql_endpoint = SPARQLFileEndpoint(rdf_file_path=rdf_file_path)
40+
sparql_endpoint_test_helper(sparql_endpoint=sparql_endpoint, uri=organisation_cet_uri)
41+
42+
43+
def test_sparql_string_endpoint(rdf_content, organisation_cet_uri):
44+
sparql_endpoint = SPARQLStringEndpoint(rdf_content=rdf_content)
45+
sparql_endpoint_test_helper(sparql_endpoint=sparql_endpoint, uri=organisation_cet_uri)

0 commit comments

Comments
 (0)