Skip to main content

A Spec By Example framework for RDF and SPARQL, Inspired by Cucumber.

Project description

MustRD

"MustRD: Validate your SPARQL queries and transformations with precision and confidence, using BDD and Given-When-Then principles."

Coverage Badge

Why?

SPARQL is a powerful query language for RDF data, but how can you ensure your queries and transformations are doing what you intend? Whether you're working on a pipeline or a standalone query, certainty is key.

While RDF and SPARQL offer great flexibility, we noticed a gap in tooling to validate their behavior. We missed the robust testing frameworks available in imperative programming languages that help ensure your code works as expected.

With MustRD, you can:

  • Define data scenarios and verify that queries produce the expected results.
  • Test edge cases to ensure your queries remain reliable.
  • Isolate small SPARQL enrichment or transformation steps and confirm you're only inserting what you intend.

What?

MustRD is a Spec-By-Example ontology with a reference Python implementation, inspired by tools like Cucumber. It uses the Given-When-Then approach to define and validate SPARQL queries and transformations.

MustRD is designed to be triplestore/SPARQL engine agnostic, leveraging open standards to ensure compatibility across different platforms.

What it is NOT

MustRD is not an alternative to SHACL. While SHACL validates data structures, MustRD focuses on validating data transformations and query results.

How?

You define your specs in Turtle (.ttl) or TriG (.trig) files using the Given-When-Then approach:

  • Given: Define the starting dataset.
  • When: Specify the action (e.g., a SPARQL query).
  • Then: Outline the expected results.

Depending on the type of SPARQL query (CONSTRUCT, SELECT, INSERT/DELETE), MustRD runs the query and compares the results against the expectations defined in the spec.

Expectations can also be defined as:

  • INSERT queries.
  • SELECT queries.
  • Higher-order expectation languages, similar to those used in various platforms.

Example

Configuration File

You'll have a configuration .ttl file, which acts as a suite of tests. It tells MustRD where to look for test specifications and any triplestore configurations you might have:

:test_example a :MustrdTest;
              :hasSpecPath "test/specs/";
              :hasDataPath "test/data/";
              :hasPytestPath "example";
              :triplestoreSpecPath "test/triplestore_config/triplestores.ttl";
              :filterOnTripleStore triplestore:example_test .

Test Specification

In the directory specified by :hasSpecPath, you'll have one or more .mustrd.ttl files. These can be organized in a directory structure. MustRD collects them and reports results to your test runner.

:test_example :given [ a :FileDataset ;
                       :file "test/data/given.ttl" ] ;
              :when [ a :TextSparqlSource ;
                     :queryText "SELECT ?s ?p ?o WHERE { ?s ?p ?o }" ;
                     :queryType :SelectSparql ] ;
              :then [ a :OrderedTableDataset ;
                     :hasRow [ :variable "s" ; :boundValue "example:subject" ;
                               :variable "p" ; :boundValue "example:predicate" ;
                               :variable "o" ; :boundValue "example:object" ] ].

And you will have a 'test/data/given.ttl' which contains the given ttl.

example:subject example:predicate example:object .

Running Tests

Run the test using the MustRD Pytest plugin:

poetry run pytest --mustrd --config=test/mustrd_configuration.ttl --md=render/github_job_summary.md

This will validate your SPARQL queries against the defined dataset and expected results, ensuring your transformations behave as intended.

You can refer to SPARQL inline, in files, or in Anzo Graphmarts, Steps, or Layers. See GETSTARTED.adoc for more details.

Integrating with Visual Studio Code (vscode)

We have a pytest plugin.

  1. Choose a python interpreter (probably a venv)
  2. pip install mustrd in it.
  3. add to your settings.json
    "python.testing.pytestArgs": [
        "--mustrd", "--md=junit/github_job_summary.md", "--config=test/test_config_local.ttl"
    ],
  1. VS Code should auto discover your tests and they'll show up in the flask icon 'tab'. alt text

When?

MustRD is a work in progress, built to meet the needs of our projects across multiple clients and vendor stacks. While we find it useful, it may not meet your needs out of the box.

We invite you to try it, raise issues, or contribute via pull requests. If you need custom features, contact us for consultancy rates, and we may prioritize your request.

Support

Semantic Partners is a specialist consultancy in Semantic Technology. If you need more support, contact us at info@semanticpartners.com or mustrd@semanticpartners.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mustrd-0.7.4.tar.gz (43.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mustrd-0.7.4-py3-none-any.whl (50.8 kB view details)

Uploaded Python 3

File details

Details for the file mustrd-0.7.4.tar.gz.

File metadata

  • Download URL: mustrd-0.7.4.tar.gz
  • Upload date:
  • Size: 43.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for mustrd-0.7.4.tar.gz
Algorithm Hash digest
SHA256 3bdd695c07dc0942fdb005bcb830bef6e3c8a70e7cd4f192259927ba94af07da
MD5 a314542dec5aca315f72a9c8e80adca4
BLAKE2b-256 2afe6da5532588c36dad2227258f173286f9e0d567b6344285b108620a6e60cc

See more details on using hashes here.

File details

Details for the file mustrd-0.7.4-py3-none-any.whl.

File metadata

  • Download URL: mustrd-0.7.4-py3-none-any.whl
  • Upload date:
  • Size: 50.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for mustrd-0.7.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f8898752a3e61671dfd48f6c3eca5510969c69db3292d94077ca5b837b32cd22
MD5 b82ce866a04e1e1b2ee865200ebdfd70
BLAKE2b-256 15c3938a12afeb9ca34d2629c753f3e4dc0d6ef04a6702b1112e5ebe6089375e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page