# 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 . """pytest fixtures""" # pylint: disable=redefined-outer-name import urllib.parse 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): params = urllib.parse.parse_qs(request.text) (query,) = params["query"] results = rdflib_graph.query(query) context.status_code = 200 return { "head": {"vars": results.vars}, "results": { "bindings": [dict(zip(results.vars, result)) 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")