65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
# This file is part of the Glowtables software
|
|
# Copyright (C) 2023 Valentin Lorentz
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it under the
|
|
# terms of the GNU Affero General Public License version 3, as published by the
|
|
# Free Software Foundation.
|
|
#
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
# PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License along with
|
|
# this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""pytest fixtures"""
|
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
import pytest
|
|
import rdflib
|
|
|
|
from glowtables.cache import Cache
|
|
from glowtables.sparql import RemoteSparqlBackend
|
|
|
|
|
|
@pytest.fixture()
|
|
def rdflib_graph() -> rdflib.Graph:
|
|
"""Returns an empty rdflib graph."""
|
|
return rdflib.Graph()
|
|
|
|
|
|
@pytest.fixture()
|
|
def rdflib_sparql(requests_mock, rdflib_graph: rdflib.Graph) -> RemoteSparqlBackend:
|
|
"""Returns a SPARQL backend instance for ``rdflib_graph``."""
|
|
|
|
def rdflib_to_json(o) -> dict:
|
|
if isinstance(o, rdflib.Literal):
|
|
return {"type": "literal", "value": str(o)}
|
|
elif isinstance(o, rdflib.URIRef):
|
|
return {"type": "uri", "value": str(o)}
|
|
else:
|
|
raise NotImplementedError(o)
|
|
|
|
def json_callback(request, context):
|
|
results = rdflib_graph.query(request.text)
|
|
context.status_code = 200
|
|
return {
|
|
"head": {"vars": results.vars},
|
|
"results": {
|
|
"bindings": [
|
|
{
|
|
k: rdflib_to_json(v)
|
|
for (k, v) in zip(results.vars, result)
|
|
if v is not None
|
|
}
|
|
for result in results
|
|
]
|
|
},
|
|
}
|
|
|
|
requests_mock.register_uri("POST", "mock://sparql.example.org/", json=json_callback)
|
|
return RemoteSparqlBackend(
|
|
"mock://sparql.example.org/", agent="Mock Client", cache=Cache(":memory:")
|
|
)
|