glowtables/glowtables/tests/conftest.py

45 lines
1.5 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.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 json_callback(request, context):
result = rdflib_graph.query(request.json())
context.status_code = 200
return {
"head": {"vars": result.vars},
"results": list(result),
}
requests_mock.register_uri("POST", "mock://sparql.example.org/", json=json_callback)
return RemoteSparqlBackend("mock://sparql.example.org/", agent="Mock Client")