Assertion Query

How can a dataset be tested for known patterns?


There are a number of circumstances in which it is useful to test for patterns in a dataset. The most common is likely to be discovery of whether there is data available about a specific resource or from a specific vocabulary. Another very common use of pattern detection is to validate a dataset to check that it conforms to a specific structure. The latter is very common when testing the data generated by an application, e.g. during development or ongoing maintenance, or to check data received from third-party systems.


Use a SPARQL ASK or CONSTRUCT query to probe the dataset for known patterns


The following will return true if the specified pattern is found:

#Is there any data about a specific resource?
 <> ?p ?o.


As described in the Transformation Query pattern, SPARQL provides a declarative syntax for certain kinds of operations on RDF graphs. The SPARQL ASK query form is intentionally provided to support making assertions against an RDF dataset. It is therefore useful in the role of testing the output of an application (e.g. for unit testing) or acceptance testing incoming data received from a third-party.

In some cases it is useful to run a number of queries against a dataset and generate suitable warning messages if any of the assertions succeed. Instead of using an ASK query, a CONSTRUCT query can be used instead, allowing a simple message to be generated as output. These messages can then be used to, e.g. produce a test report from a dataset:

 _:msg ex:message "Every person should have a name".
 ?person a foaf:Person;
   ?person foaf:name ?name.
 FILTER (!bound(?name)) 


Further Reading

  • The Schemarama testing framework uses the CONSTRUCT variant of this pattern.