Skip to main content

sparql-endpoint-fixture

RDFLIB-based SPARQL Endpoint Fixture for pytest

Enable the fixture explicitly in your tests or conftest.py (not required when using setuptools entry points):

pytest_plugins = [
    "sparql_endpoint_fixture.endpoint"
]

The endpoint fixture uses httpretty to intercept all HTTP calls to the specified URL and can be initialized with RDF data prior to use.

import requests

def test_request_get(sparql_endpoint):
    repo_uri = 'https://my.rdfdb.com/repo/sparql'
    rdf_files = ['tests/upper_ontology.ttl',
                 'tests/domain_ontology.ttl',
                 'tests/instance_data.ttl']
    endpoint = sparql_endpoint(repo_uri, rdf_files)
    query = "select distinct ?class where { [] a ?class } order by ?class"
    response = requests.get(url=repo_uri, params={'query': query}, headers={'Accept': 'application/json'})
    assert len(response.json()['results']['bindings']) == '10'

Since the backing store for the simulated endpoint is a RDFLib ConjuntiveGraph, initial data can be loaded into specified named graphs:

import requests

def test_multiple_graphs(sparql_endpoint):
    repo_uri = 'https://my.rdfdb.com/repo/sparql'
    rdf_files = [{'http://example.com/graph/upper': 'tests/upper_ontology.ttl',
                  'http://example.com/graph/domain': 'tests/domain_ontology.ttl',
                  'http://example.com/graph/instance': 'tests/instance_data.ttl'}]
    endpoint = sparql_endpoint(repo_uri, rdf_files)  # noqa: F841
    query = "select ?graph (count(?s) as ?size) where { graph ?graph { ?s ?p ?o } } group by ?graph"
    response = requests.get(url=repo_uri, params={'query': query}, headers={'Accept': 'application/json'})
    results = dict(
        (row['graph']['value'], row['size']['value'])
        for row in response.json()['results']['bindings'])

    expected = {'http://example.com/graph/upper': '18',
                'http://example.com/graph/domain': '21',
                'http://example.com/graph/instance': '15'}
    assert results == expected

Specifying the dataset context for the query is supported via query parameters as per the SPARQL HTTP Protocol, using default-graph-uri/named-graph-uri URI request parameters for queries and using-graph-uri/using-named-graph-uri for updates. Datasets specified via request parameters will override any dataset specification in the query itself (via FROM or USING) - no attempt will be made to merge them.

response = requests.get(url=repo_uri,
                        params={
                            'query': "select * where { ?s ?p ?o }",
                            'default-graph-uri': ['http://example.com/graph/upper', 'http://example.com/graph/domain']
                        },
                        headers={'Accept': 'application/json'})

The fixture also supports predefined responses for non-SPARQL requests, so that administrative and utility calls generated by 3rd-party clients can be handled. The matching path can be specified as either a fixed string or a re.Pattern, and the response can be a fixed (status_code, header_dict, body) tuple or a method that takes a HTTPrettyRequest parameter and returns such a tuple. In order to handle these varied paths, the endpoint URI should be specified as a Pattern as well. For example:

    endpoint = sparql_endpoint(
        re.compile(repo_uri + '.*'),
        rdf_files,
        predefined={
            # Fixed match, fixed response
            '/repo/ok': (200, {}, 'OK'),
            # Regex match, fixed response
            re.compile(r'/repo/transaction/.*'): (201, {}, ''),
            # Regex match, dynamic response
            re.compile(r'/repo/admin/.*'): lambda r: (200, {}, r.path[15:])
        }
    )

Planned Development

Support will be added for the graph store protocol in the future.

Metadata

Release files for sparql-endpoint-fixture 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparql-endpoint-fixture 1.0.1
File Size Uploaded Provenance
sparql_endpoint_fixture-1.0.1.tar.gz 8.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sparql-endpoint-fixture 1.0.1
File Interpreter ABI Platform
sparql_endpoint_fixture-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / sparql_endpoint_fixture-1.0.1.tar.gz

Download URL sparql_endpoint_fixture-1.0.1.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a2e0e592336e1e152fb55bdcc72f135888e301671654f5ac5522f2c95f7b1641
BLAKE2b-256 checksum
How to use checksums
603b04b192ac7331a18ffc675d0f4060a6b17b0f825856eaeb329e4122a21529
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / sparql_endpoint_fixture-1.0.1-py3-none-any.whl

Download URL sparql_endpoint_fixture-1.0.1-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3ceb25a8f71f406f741b406daafbaea1c271181033f124cfa37b3881476c818
BLAKE2b-256 checksum
How to use checksums
a01a59c646eaa80fa10f1058b203ab400762b6071eb8ae87b99810eb6e69e71b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for sparql-endpoint-fixture 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page