Execute SPARQL 1.0 test suite

pull/10/head
Tpt 6 years ago
parent 70d2fab95d
commit 74a2d9859a
  1. 22
      src/sparql/algebra.rs
  2. 42
      src/sparql/sparql_grammar.rustpeg
  3. 41
      tests/rdf_test_cases.rs
  4. 345
      tests/sparql_test_cases.rs

@ -875,10 +875,7 @@ impl From<TripleOrPathPattern> for MultiSetPattern {
impl From<ListPattern> for MultiSetPattern {
fn from(pattern: ListPattern) -> Self {
match pattern {
ListPattern::ToList(pattern) => pattern,
pattern => MultiSetPattern::ToMultiSet(Box::new(pattern)),
}
MultiSetPattern::ToMultiSet(Box::new(pattern))
}
}
@ -955,7 +952,7 @@ impl<'a> fmt::Display for SparqlMultiSetPattern<'a> {
match self.0 {
MultiSetPattern::BGP(p) => {
if p.is_empty() {
write!(f, "{{}}")
Ok(())
} else {
write!(
f,
@ -1126,10 +1123,7 @@ impl Default for ListPattern {
impl From<MultiSetPattern> for ListPattern {
fn from(pattern: MultiSetPattern) -> Self {
match pattern {
MultiSetPattern::ToMultiSet(pattern) => *pattern,
pattern => ListPattern::ToList(pattern),
}
ListPattern::ToList(pattern)
}
}
@ -1187,7 +1181,7 @@ impl<'a> fmt::Display for SparqlListPattern<'a> {
}
write!(f, " }}")
},
ListPattern::ToList(l) => write!(f, "{}", SparqlMultiSetPattern(&*l)),
ListPattern::ToList(l) => write!(f, "{{ {} }}", SparqlMultiSetPattern(&*l)),
ListPattern::OrderBy(l, o) => write!(
f,
"{} ORDER BY {}",
@ -1202,7 +1196,7 @@ impl<'a> fmt::Display for SparqlListPattern<'a> {
),
ListPattern::Project(l, pv) => write!(
f,
"SELECT {} {} WHERE {{ {} }}",
"SELECT {} {} WHERE {}",
build_sparql_select_arguments(pv),
self.dataset,
SparqlListPattern {
@ -1213,7 +1207,7 @@ impl<'a> fmt::Display for SparqlListPattern<'a> {
ListPattern::Distinct(l) => match l.as_ref() {
ListPattern::Project(l, pv) => write!(
f,
"SELECT DISTINCT {} {} WHERE {{ {} }}",
"SELECT DISTINCT {} {} WHERE {}",
build_sparql_select_arguments(pv),
self.dataset,
SparqlListPattern {
@ -1233,7 +1227,7 @@ impl<'a> fmt::Display for SparqlListPattern<'a> {
ListPattern::Reduced(l) => match l.as_ref() {
ListPattern::Project(l, pv) => write!(
f,
"SELECT REDUCED {} {} WHERE {{ {} }}",
"SELECT REDUCED {} {} WHERE {}",
build_sparql_select_arguments(pv),
self.dataset,
SparqlListPattern {
@ -1560,7 +1554,7 @@ impl fmt::Display for Query {
),
Query::DescribeQuery { dataset, algebra } => write!(
f,
"DESCRIBE {} WHERE {{ {} }}",
"DESCRIBE * {} WHERE {{ {} }}",
dataset,
SparqlListPattern {
algebra: &algebra,

@ -177,7 +177,7 @@ HavingClause -> Expression = "HAVING"i _ e:HavingCondition+ {?
HavingCondition -> Expression = Constraint
//[23]
OrderClause -> Vec<OrderComparator> = "ORDER"i "BY"i _ c:OrderClause_item+ { c }
OrderClause -> Vec<OrderComparator> = "ORDER"i _ "BY"i _ c:OrderClause_item+ { c }
OrderClause_item -> OrderComparator = c:OrderCondition _ { c }
//[24]
@ -208,10 +208,16 @@ ValuesClause -> Option<MultiSetPattern> =
{ None }
//[52]
TriplesTemplate -> Vec<TriplePattern> = p:TriplesTemplate_item **<1,> ('.' _) '.'? {
p.into_iter().flat_map(|c| c.into_iter()).collect()
TriplesTemplate -> Vec<TriplePattern> = h:TriplesSameSubject _ t:TriplesTemplate_tail? {
let mut triples = h;
if let Some(l) = t {
triples.extend_from_slice(&l)
}
triples
}
TriplesTemplate_tail -> Vec<TriplePattern> = '.' _ t:TriplesTemplate? _ {
t.unwrap_or_else(Vec::default)
}
TriplesTemplate_item -> Vec<TriplePattern> = p:TriplesSameSubject _ { p }
//[53]
GroupGraphPattern -> MultiSetPattern =
@ -369,14 +375,14 @@ FunctionCall -> Expression = f: iri _ a: ArgList {
//[71]
ArgList -> Vec<Expression> = //TODO: support DISTINCT
NIL { Vec::new() } /
'(' _ 'DISTINCT'? _ e:ArgList_item **<1,> (',' _) _ ')' { e }
'(' _ 'DISTINCT'? _ e:ArgList_item **<1,> (',' _) _ ')' { e } /
NIL { Vec::new() }
ArgList_item -> Expression = e:Expression _ { e }
//[72]
ExpressionList -> Vec<Expression> =
NIL { Vec::default() } /
'(' _ e:ExpressionList_item **<1,> (',' _) ')' { e }
'(' _ e:ExpressionList_item **<1,> (',' _) ')' { e } /
NIL { Vec::default() }
ExpressionList_item -> Expression = e:Expression _ { e }
//[73]
@ -475,7 +481,7 @@ PropertyListPath -> FocusedTripleOrPathPattern<Vec<(VariableOrPropertyPath,Vec<T
//[83]
PropertyListPathNotEmpty -> FocusedTripleOrPathPattern<Vec<(VariableOrPropertyPath,Vec<TermOrVariable>)>> = hp:(VerbPath / VerbSimple) _ ho:ObjectListPath _ t:PropertyListPathNotEmpty_item* {
t.into_iter().fold(FocusedTripleOrPathPattern {
t.into_iter().flat_map(|e| e.into_iter()).fold(FocusedTripleOrPathPattern {
focus: vec![(hp, ho.focus)],
patterns: ho.patterns
}, |mut a, b| {
@ -484,7 +490,10 @@ PropertyListPathNotEmpty -> FocusedTripleOrPathPattern<Vec<(VariableOrPropertyPa
a
})
}
PropertyListPathNotEmpty_item -> FocusedTriplePattern<(VariableOrPropertyPath,Vec<TermOrVariable>)> = ';' _ p:(VerbPath / VerbSimple) _ o:ObjectList _ { //TODO: make values after ';' optional
PropertyListPathNotEmpty_item -> Option<FocusedTriplePattern<(VariableOrPropertyPath,Vec<TermOrVariable>)>> = ';' _ c:PropertyListPathNotEmpty_item_content? {
c
}
PropertyListPathNotEmpty_item_content -> FocusedTriplePattern<(VariableOrPropertyPath,Vec<TermOrVariable>)> = p:(VerbPath / VerbSimple) _ o:ObjectList _ {
FocusedTriplePattern {
focus: (p, o.focus),
patterns: o.patterns
@ -858,9 +867,12 @@ Aggregate -> Aggregation =
"GROUP_CONCAT"i _ '(' _ e:Expression _ ')' { Aggregation::GroupConcat(Box::new(e), false, None) }
//[128]
iriOrFunction -> Expression =
FunctionCall /
i:iri { Expression::ConstantExpression(i.into()) }
iriOrFunction -> Expression = i: iri _ a: ArgList? {
match a {
Some(a) => Expression::CustomFunctionCall(i, a),
None => Expression::ConstantExpression(i.into())
}
}
//[129]
RDFLiteral -> Literal =
@ -896,7 +908,7 @@ BooleanLiteral -> Literal =
"false" { false.into() }
//[135]
String -> String = STRING_LITERAL1 / STRING_LITERAL2 / STRING_LITERAL_LONG1 / STRING_LITERAL_LONG2
String -> String = STRING_LITERAL_LONG1 / STRING_LITERAL_LONG2 / STRING_LITERAL1 / STRING_LITERAL2
//[136]
iri -> NamedNode = i:(IRIREF / PrefixedName) {?
@ -953,7 +965,7 @@ INTEGER -> () = [0-9]+
DECIMAL -> () = [0-9]* '.' [0-9]+
//[148]
DOUBLE -> () = ([0-9]+ "." [0-9]* / "."? [0-9]+) EXPONENT
DOUBLE -> () = ([0-9]+ "."? [0-9]* / "." [0-9]+) EXPONENT
//[149]
INTEGER_POSITIVE -> () = '+' _ INTEGER

@ -15,8 +15,6 @@ use rudf::model::*;
use rudf::rio::ntriples::read_ntriples;
use rudf::rio::turtle::read_turtle;
use rudf::rio::xml::read_rdf_xml;
use rudf::sparql::algebra::Query;
use rudf::sparql::parser::read_sparql_query;
use rudf::store::isomorphism::GraphIsomorphism;
use rudf::store::MemoryGraph;
use std::error::Error;
@ -182,41 +180,6 @@ fn rdf_xml_w3c_testsuite() -> Result<()> {
Ok(())
}
#[test]
fn sparql_w3c_syntax_testsuite() {
let manifest_url = Url::parse(
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-query/manifest.ttl",
).unwrap();
let client = RDFClient::default();
for test_result in TestManifest::new(&client, manifest_url) {
let test = test_result.unwrap();
if test.kind == "PositiveSyntaxTest11" {
match client.load_sparql_query(test.action.clone()) {
Err(error) => assert!(false, "Failure on {} with error: {}", test, error),
Ok(query) => {
if let Err(error) = read_sparql_query(query.to_string().as_bytes(), None) {
assert!(
false,
"Failure to deserialize \"{}\" of {} with error: {}",
query.to_string(),
test,
error
)
}
}
}
} else if test.kind == "NegativeSyntaxTest11" {
//TODO
if let Ok(result) = client.load_sparql_query(test.action.clone()) {
eprintln!("Failure on {}. The output tree is: {}", test, result);
}
} else {
assert!(false, "Not supported test: {}", test);
}
}
}
pub struct RDFClient {
client: Client,
}
@ -242,10 +205,6 @@ impl RDFClient {
read_rdf_xml(BufReader::new(self.get(&url)?), Some(url)).collect()
}
pub fn load_sparql_query(&self, url: Url) -> Result<Query> {
read_sparql_query(self.get(&url)?, Some(url))
}
fn get(&self, url: &Url) -> Result<Response> {
match self.client.get(url.clone()).send() {
Ok(response) => Ok(response),

@ -0,0 +1,345 @@
///! Integration tests based on [SPARQL 1.1 Test Cases](https://www.w3.org/2009/sparql/docs/tests/README.html)
#[macro_use]
extern crate lazy_static;
extern crate reqwest;
extern crate rudf;
extern crate url;
use reqwest::Client;
use reqwest::Response;
use rudf::errors::*;
use rudf::model::vocab::rdf;
use rudf::model::vocab::rdfs;
use rudf::model::*;
use rudf::rio::ntriples::read_ntriples;
use rudf::rio::turtle::read_turtle;
use rudf::rio::xml::read_rdf_xml;
use rudf::sparql::algebra::Query;
use rudf::sparql::parser::read_sparql_query;
use rudf::store::MemoryGraph;
use std::error::Error;
use std::fmt;
use std::io::BufReader;
use std::str::FromStr;
use url::Url;
#[test]
fn sparql_w3c_syntax_testsuite() {
let manifest_10_url =
Url::parse("https://www.w3.org/2001/sw/DataAccess/tests/data-r2/manifest-syntax.ttl")
.unwrap();
let manifest_11_url = Url::parse(
"http://www.w3.org/2009/sparql/docs/tests/data-sparql11/syntax-query/manifest.ttl",
).unwrap();
let test_blacklist = vec![
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/syntax-sparql2/manifest#syntax-form-construct02").unwrap(),
//TODO: Deserialization of the serialization failing:
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/syntax-sparql2/manifest#syntax-form-construct04").unwrap(),
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/syntax-sparql2/manifest#syntax-function-04").unwrap(),
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/syntax-sparql1/manifest#syntax-lit-08").unwrap(),
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/syntax-sparql1/manifest#syntax-qname-04").unwrap(),
];
let client = RDFClient::default();
for test_result in TestManifest::new(&client, manifest_10_url)
.chain(TestManifest::new(&client, manifest_11_url))
{
let test = test_result.unwrap();
if test_blacklist.contains(&test.id) {
continue;
}
if test.kind == "PositiveSyntaxTest" || test.kind == "PositiveSyntaxTest11" {
match client.load_sparql_query(test.action.clone()) {
Err(error) => assert!(false, "Failure on {} with error: {}", test, error),
Ok(query) => {
if let Err(error) = read_sparql_query(query.to_string().as_bytes(), None) {
assert!(
false,
"Failure to deserialize \"{}\" of {} with error: {}",
query.to_string(),
test,
error
)
}
}
}
} else if test.kind == "NegativeSyntaxTest" || test.kind == "NegativeSyntaxTest11" {
//TODO
if let Ok(result) = client.load_sparql_query(test.action.clone()) {
eprintln!("Failure on {}. The output tree is: {}", test, result);
}
} else {
assert!(false, "Not supported test: {}", test);
}
}
}
pub struct RDFClient {
client: Client,
}
impl Default for RDFClient {
fn default() -> Self {
Self {
client: Client::new(),
}
}
}
impl RDFClient {
pub fn load_turtle(&self, url: Url) -> Result<MemoryGraph> {
Ok(read_turtle(self.get(&url)?, Some(url))?.collect())
}
pub fn load_ntriples(&self, url: Url) -> Result<MemoryGraph> {
read_ntriples(self.get(&url)?).collect()
}
pub fn load_rdf_xml(&self, url: Url) -> Result<MemoryGraph> {
read_rdf_xml(BufReader::new(self.get(&url)?), Some(url)).collect()
}
pub fn load_sparql_query(&self, url: Url) -> Result<Query> {
read_sparql_query(self.get(&url)?, Some(url))
}
fn get(&self, url: &Url) -> Result<Response> {
match self.client.get(url.clone()).send() {
Ok(response) => Ok(response),
Err(error) => if error.description() == "parsed HTTP message from remote is incomplete"
{
self.get(url)
} else {
Err(format!("HTTP request error: {}", error.description()).into())
},
}
}
}
pub struct Test {
pub id: NamedNode,
pub kind: String,
pub name: Option<String>,
pub comment: Option<String>,
pub action: Url,
pub result: Option<Url>,
}
impl fmt::Display for Test {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.kind)?;
for name in &self.name {
write!(f, " named \"{}\"", name)?;
}
for comment in &self.comment {
write!(f, " with comment \"{}\"", comment)?;
}
write!(f, " on file \"{}\"", self.action)?;
Ok(())
}
}
pub struct TestManifest<'a> {
client: &'a RDFClient,
graph: MemoryGraph,
tests_to_do: Vec<Term>,
manifests_to_do: Vec<Url>,
}
impl<'a> TestManifest<'a> {
pub fn new(client: &'a RDFClient, url: Url) -> TestManifest<'a> {
Self {
client,
graph: MemoryGraph::default(),
tests_to_do: Vec::default(),
manifests_to_do: vec![url],
}
}
}
pub mod mf {
use rudf::model::NamedNode;
use std::str::FromStr;
lazy_static! {
pub static ref INCLUDE: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#include")
.unwrap();
pub static ref ENTRIES: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#entries")
.unwrap();
pub static ref NAME: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#name")
.unwrap();
pub static ref ACTION: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#action")
.unwrap();
pub static ref RESULT: NamedNode =
NamedNode::from_str("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#result")
.unwrap();
}
}
impl<'a> Iterator for TestManifest<'a> {
type Item = Result<Test>;
fn next(&mut self) -> Option<Result<Test>> {
match self.tests_to_do.pop() {
Some(Term::NamedNode(test_node)) => {
let test_subject = NamedOrBlankNode::from(test_node.clone());
let kind = match self
.graph
.object_for_subject_predicate(&test_subject, &rdf::TYPE)
.unwrap()
{
Some(Term::NamedNode(c)) => match c.value().split("#").last() {
Some(k) => k.to_string(),
None => return Some(Err("no type".into())),
},
_ => return Some(Err("no type".into())),
};
let name = match self
.graph
.object_for_subject_predicate(&test_subject, &mf::NAME)
.unwrap()
{
Some(Term::Literal(c)) => Some(c.value().to_string()),
_ => None,
};
let comment = match self
.graph
.object_for_subject_predicate(&test_subject, &rdfs::COMMENT)
.unwrap()
{
Some(Term::Literal(c)) => Some(c.value().to_string()),
_ => None,
};
let action = match self
.graph
.object_for_subject_predicate(&test_subject, &*mf::ACTION)
.unwrap()
{
Some(Term::NamedNode(n)) => n.url().clone(),
Some(_) => return Some(Err("invalid action".into())),
None => return Some(Err("action not found".into())),
};
let result = match self
.graph
.object_for_subject_predicate(&test_subject, &*mf::RESULT)
.unwrap()
{
Some(Term::NamedNode(n)) => Some(n.url().clone()),
Some(_) => return Some(Err("invalid result".into())),
None => None,
};
Some(Ok(Test {
id: test_node,
kind,
name,
comment,
action,
result,
}))
}
Some(_) => Some(Err("invalid test list".into())),
None => {
match self.manifests_to_do.pop() {
Some(url) => {
let manifest = NamedOrBlankNode::from(NamedNode::new(url.clone()));
match self.client.load_turtle(url) {
Ok(g) => g
.iter()
.unwrap()
.for_each(|g| self.graph.insert(&g.unwrap()).unwrap()),
Err(e) => return Some(Err(e.into())),
}
// New manifests
match self
.graph
.object_for_subject_predicate(&manifest, &*mf::INCLUDE)
.unwrap()
{
Some(Term::BlankNode(list)) => {
self.manifests_to_do.extend(
RdfListIterator::iter(&self.graph, list.clone().into())
.flat_map(|m| match m {
Term::NamedNode(nm) => Some(nm.url().clone()),
_ => None,
}),
);
}
Some(_) => return Some(Err("invalid tests list".into())),
None => (),
}
// New tests
match self
.graph
.object_for_subject_predicate(&manifest, &*mf::ENTRIES)
.unwrap()
{
Some(Term::BlankNode(list)) => {
self.tests_to_do.extend(RdfListIterator::iter(
&self.graph,
list.clone().into(),
));
}
Some(term) => {
return Some(Err(
format!("Invalid tests list. Got term {}", term).into()
))
}
None => (),
}
}
None => return None,
}
self.next()
}
}
}
}
pub struct RdfListIterator<'a, G: 'a + Graph> {
graph: &'a G,
current_node: Option<NamedOrBlankNode>,
}
impl<'a, G: 'a + Graph> RdfListIterator<'a, G> {
fn iter(graph: &'a G, root: NamedOrBlankNode) -> RdfListIterator<'a, G> {
RdfListIterator {
graph,
current_node: Some(root),
}
}
}
impl<'a, G: 'a + Graph> Iterator for RdfListIterator<'a, G> {
type Item = Term;
fn next(&mut self) -> Option<Term> {
match self.current_node.clone() {
Some(current) => {
let result = self
.graph
.object_for_subject_predicate(&current, &rdf::FIRST)
.unwrap()?
.clone();
self.current_node = match self
.graph
.object_for_subject_predicate(&current, &rdf::REST)
.unwrap()
{
Some(Term::NamedNode(ref n)) if *n == *rdf::NIL => None,
Some(Term::NamedNode(n)) => Some(n.clone().into()),
Some(Term::BlankNode(n)) => Some(n.clone().into()),
_ => None,
};
Some(result)
}
None => None,
}
}
}
Loading…
Cancel
Save