Uses anyhow for testsuite error

Allows to simplify the code
pull/46/head
Tpt 4 years ago
parent 9d4fe700d6
commit 4dd7912300
  1. 1
      testsuite/Cargo.toml
  2. 21
      testsuite/src/files.rs
  3. 18
      testsuite/src/manifest.rs
  4. 25
      testsuite/src/parser_evaluator.rs
  5. 2
      testsuite/src/report.rs
  6. 56
      testsuite/src/sparql_evaluator.rs
  7. 2
      testsuite/tests/oxigraph.rs
  8. 2
      testsuite/tests/parser.rs
  9. 2
      testsuite/tests/sparql.rs
  10. 6
      wikibase/src/loader.rs

@ -12,6 +12,7 @@ edition = "2018"
publish = false publish = false
[dependencies] [dependencies]
anyhow = "1"
chrono = "0.4" chrono = "0.4"
lazy_static = "1" lazy_static = "1"
oxigraph = { path = "../lib" } oxigraph = { path = "../lib" }

@ -1,5 +1,6 @@
use anyhow::{anyhow, Result};
use oxigraph::model::GraphName; use oxigraph::model::GraphName;
use oxigraph::{DatasetSyntax, Error, GraphSyntax, MemoryStore, Result}; use oxigraph::{DatasetSyntax, GraphSyntax, MemoryStore};
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, Read}; use std::io::{BufRead, BufReader, Read};
use std::path::PathBuf; use std::path::PathBuf;
@ -26,7 +27,7 @@ pub fn read_file(url: &str) -> Result<impl BufRead> {
"oxigraph-tests/", "oxigraph-tests/",
)) ))
} else { } else {
Err(Error::msg(format!("Not supported url for file: {}", url))) Err(anyhow!("Not supported url for file: {}", url))
}?); }?);
Ok(BufReader::new(File::open(&path)?)) Ok(BufReader::new(File::open(&path)?))
} }
@ -44,31 +45,29 @@ pub fn load_to_store(url: &str, store: &MemoryStore, to_graph_name: &GraphName)
GraphSyntax::NTriples, GraphSyntax::NTriples,
to_graph_name, to_graph_name,
Some(url), Some(url),
) )?
} else if url.ends_with(".ttl") { } else if url.ends_with(".ttl") {
store.load_graph( store.load_graph(
read_file(url)?, read_file(url)?,
GraphSyntax::Turtle, GraphSyntax::Turtle,
to_graph_name, to_graph_name,
Some(url), Some(url),
) )?
} else if url.ends_with(".rdf") { } else if url.ends_with(".rdf") {
store.load_graph( store.load_graph(
read_file(url)?, read_file(url)?,
GraphSyntax::RdfXml, GraphSyntax::RdfXml,
to_graph_name, to_graph_name,
Some(url), Some(url),
) )?
} else if url.ends_with(".nq") { } else if url.ends_with(".nq") {
store.load_dataset(read_file(url)?, DatasetSyntax::NQuads, Some(url)) store.load_dataset(read_file(url)?, DatasetSyntax::NQuads, Some(url))?
} else if url.ends_with(".trig") { } else if url.ends_with(".trig") {
store.load_dataset(read_file(url)?, DatasetSyntax::TriG, Some(url)) store.load_dataset(read_file(url)?, DatasetSyntax::TriG, Some(url))?
} else { } else {
Err(Error::msg(format!( return Err(anyhow!("Serialization type not found for {}", url));
"Serialization type not found for {}",
url
)))
} }
Ok(())
} }
pub fn load_store(url: &str) -> Result<MemoryStore> { pub fn load_store(url: &str) -> Result<MemoryStore> {

@ -1,8 +1,9 @@
use crate::files::load_to_store; use crate::files::load_to_store;
use crate::vocab::*; use crate::vocab::*;
use anyhow::{anyhow, Result};
use oxigraph::model::vocab::*; use oxigraph::model::vocab::*;
use oxigraph::model::*; use oxigraph::model::*;
use oxigraph::{Error, MemoryStore, Result}; use oxigraph::MemoryStore;
use std::fmt; use std::fmt;
pub struct Test { pub struct Test {
@ -145,18 +146,18 @@ impl Iterator for TestManifest {
.collect(); .collect();
(None, query, data, graph_data, service_data) (None, query, data, graph_data, service_data)
} }
Some(_) => return Some(Err(Error::msg("invalid action"))), Some(_) => return Some(Err(anyhow!("invalid action"))),
None => { None => {
return Some(Err(Error::msg(format!( return Some(Err(anyhow!(
"action not found for test {}", "action not found for test {}",
test_subject test_subject
)))); )));
} }
}; };
let result = let result =
match object_for_subject_predicate(&self.graph, &test_subject, &*mf::RESULT) { match object_for_subject_predicate(&self.graph, &test_subject, &*mf::RESULT) {
Some(Term::NamedNode(n)) => Some(n.into_string()), Some(Term::NamedNode(n)) => Some(n.into_string()),
Some(_) => return Some(Err(Error::msg("invalid result"))), Some(_) => return Some(Err(anyhow!("invalid result"))),
None => None, None => None,
}; };
Some(Ok(Test { Some(Ok(Test {
@ -195,7 +196,7 @@ impl Iterator for TestManifest {
), ),
); );
} }
Some(_) => return Some(Err(Error::msg("invalid tests list"))), Some(_) => return Some(Err(anyhow!("invalid tests list"))),
None => (), None => (),
} }
@ -206,10 +207,7 @@ impl Iterator for TestManifest {
.extend(RdfListIterator::iter(&self.graph, list.into())); .extend(RdfListIterator::iter(&self.graph, list.into()));
} }
Some(term) => { Some(term) => {
return Some(Err(Error::msg(format!( return Some(Err(anyhow!("Invalid tests list. Got term {}", term)));
"Invalid tests list. Got term {}",
term
))));
} }
None => (), None => (),
} }

@ -1,8 +1,8 @@
use crate::files::load_store; use crate::files::load_store;
use crate::manifest::Test; use crate::manifest::Test;
use crate::report::TestResult; use crate::report::TestResult;
use anyhow::{anyhow, Result};
use chrono::Utc; use chrono::Utc;
use oxigraph::{Error, Result};
pub fn evaluate_parser_tests( pub fn evaluate_parser_tests(
manifest: impl Iterator<Item = Result<Test>>, manifest: impl Iterator<Item = Result<Test>>,
@ -24,7 +24,7 @@ fn evaluate_parser_test(test: &Test) -> Result<()> {
let action = test let action = test
.action .action
.as_deref() .as_deref()
.ok_or_else(|| Error::msg(format!("No action found for test {}", test)))?; .ok_or_else(|| anyhow!("No action found for test {}", test))?;
if test.kind == "http://www.w3.org/ns/rdftest#TestNTriplesPositiveSyntax" if test.kind == "http://www.w3.org/ns/rdftest#TestNTriplesPositiveSyntax"
|| test.kind == "http://www.w3.org/ns/rdftest#TestNQuadsPositiveSyntax" || test.kind == "http://www.w3.org/ns/rdftest#TestNQuadsPositiveSyntax"
|| test.kind == "http://www.w3.org/ns/rdftest#TestTurtlePositiveSyntax" || test.kind == "http://www.w3.org/ns/rdftest#TestTurtlePositiveSyntax"
@ -32,7 +32,7 @@ fn evaluate_parser_test(test: &Test) -> Result<()> {
{ {
match load_store(action) { match load_store(action) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(e) => Err(Error::msg(format!("Parse error: {}", e))), Err(e) => Err(anyhow!(format!("Parse error: {}", e))),
} }
} else if test.kind == "http://www.w3.org/ns/rdftest#TestNTriplesNegativeSyntax" } else if test.kind == "http://www.w3.org/ns/rdftest#TestNTriplesNegativeSyntax"
|| test.kind == "http://www.w3.org/ns/rdftest#TestNQuadsNegativeSyntax" || test.kind == "http://www.w3.org/ns/rdftest#TestNQuadsNegativeSyntax"
@ -43,9 +43,7 @@ fn evaluate_parser_test(test: &Test) -> Result<()> {
|| test.kind == "http://www.w3.org/ns/rdftest#TestXMLNegativeSyntax" || test.kind == "http://www.w3.org/ns/rdftest#TestXMLNegativeSyntax"
{ {
match load_store(action) { match load_store(action) {
Ok(_) => Err(Error::msg( Ok(_) => Err(anyhow!("File parsed with an error even if it should not",)),
"File parsed with an error even if it should not",
)),
Err(_) => Ok(()), Err(_) => Ok(()),
} }
} else if test.kind == "http://www.w3.org/ns/rdftest#TestTurtleEval" } else if test.kind == "http://www.w3.org/ns/rdftest#TestTurtleEval"
@ -60,21 +58,22 @@ fn evaluate_parser_test(test: &Test) -> Result<()> {
if expected_graph.is_isomorphic(&actual_graph) { if expected_graph.is_isomorphic(&actual_graph) {
Ok(()) Ok(())
} else { } else {
Err(Error::msg(format!( Err(anyhow!(
"The two files are not isomorphic. Expected:\n{}\nActual:\n{}", "The two files are not isomorphic. Expected:\n{}\nActual:\n{}",
expected_graph, actual_graph expected_graph,
))) actual_graph
))
} }
} }
Err(e) => Err(Error::msg(format!("Parse error on file {}: {}", action, e))), Err(e) => Err(anyhow!("Parse error on file {}: {}", action, e)),
} }
} else { } else {
Err(Error::msg("No tests result found".to_string())) Err(anyhow!("No tests result found"))
} }
} }
Err(e) => Err(Error::msg(format!("Parse error on file {}: {}", action, e))), Err(e) => Err(anyhow!("Parse error on file {}: {}", action, e)),
} }
} else { } else {
Err(Error::msg(format!("Unsupported test type: {}", test.kind))) Err(anyhow!("Unsupported test type: {}", test.kind))
} }
} }

@ -1,6 +1,6 @@
use anyhow::Result;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use oxigraph::model::NamedNode; use oxigraph::model::NamedNode;
use oxigraph::Result;
#[derive(Debug)] #[derive(Debug)]
pub struct TestResult { pub struct TestResult {

@ -2,11 +2,12 @@ use crate::files::*;
use crate::manifest::*; use crate::manifest::*;
use crate::report::*; use crate::report::*;
use crate::vocab::*; use crate::vocab::*;
use anyhow::{anyhow, Result};
use chrono::Utc; use chrono::Utc;
use oxigraph::model::vocab::*; use oxigraph::model::vocab::*;
use oxigraph::model::*; use oxigraph::model::*;
use oxigraph::sparql::*; use oxigraph::sparql::*;
use oxigraph::{Error, MemoryStore, Result}; use oxigraph::{Error, MemoryStore};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
@ -36,20 +37,17 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
let query_file = test let query_file = test
.action .action
.as_deref() .as_deref()
.ok_or_else(|| Error::msg(format!("No action found for test {}", test)))?; .ok_or_else(|| anyhow!("No action found for test {}", test))?;
match Query::parse(&read_file_to_string(&query_file)?, Some(&query_file)) { match Query::parse(&read_file_to_string(&query_file)?, Some(&query_file)) {
Err(error) => Err(Error::msg(format!( Err(error) => Err(anyhow!("Not able to parse {} with error: {}", test, error)),
"Not able to parse {} with error: {}",
test, error
))),
Ok(query) => match Query::parse(&query.to_string(), None) { Ok(query) => match Query::parse(&query.to_string(), None) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(error) => Err(Error::msg(format!( Err(error) => Err(anyhow!(
"Failure to deserialize \"{}\" of {} with error: {}", "Failure to deserialize \"{}\" of {} with error: {}",
query.to_string(), query.to_string(),
test, test,
error error
))), )),
}, },
} }
} else if test.kind } else if test.kind
@ -60,12 +58,13 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
let query_file = test let query_file = test
.action .action
.as_deref() .as_deref()
.ok_or_else(|| Error::msg(format!("No action found for test {}", test)))?; .ok_or_else(|| anyhow!("No action found for test {}", test))?;
match Query::parse(&read_file_to_string(query_file)?, Some(query_file)) { match Query::parse(&read_file_to_string(query_file)?, Some(query_file)) {
Ok(result) => Err(Error::msg(format!( Ok(result) => Err(anyhow!(
"Oxigraph parses even if it should not {}. The output tree is: {}", "Oxigraph parses even if it should not {}. The output tree is: {}",
test, result test,
))), result
)),
Err(_) => Ok(()), Err(_) => Ok(()),
} }
} else if test.kind } else if test.kind
@ -81,26 +80,25 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
let query_file = test let query_file = test
.query .query
.as_deref() .as_deref()
.ok_or_else(|| Error::msg(format!("No action found for test {}", test)))?; .ok_or_else(|| anyhow!("No action found for test {}", test))?;
let options = QueryOptions::default() let options = QueryOptions::default()
.with_service_handler(StaticServiceHandler::new(&test.service_data)?); .with_service_handler(StaticServiceHandler::new(&test.service_data)?);
match Query::parse(&read_file_to_string(query_file)?, Some(query_file)) { match Query::parse(&read_file_to_string(query_file)?, Some(query_file)) {
Err(error) => Err(Error::msg(format!( Err(error) => Err(anyhow!(
"Failure to parse query of {} with error: {}", "Failure to parse query of {} with error: {}",
test, error test,
))), error
)),
Ok(query) => match store.query(query, options) { Ok(query) => match store.query(query, options) {
Err(error) => Err(Error::msg(format!( Err(error) => Err(anyhow!(
"Failure to execute query of {} with error: {}", "Failure to execute query of {} with error: {}",
test, error test,
))), error
)),
Ok(actual_results) => { Ok(actual_results) => {
let expected_results = load_sparql_query_result(test.result.as_ref().unwrap()) let expected_results = load_sparql_query_result(test.result.as_ref().unwrap())
.map_err(|e| { .map_err(|e| {
Error::msg(format!( anyhow!("Error constructing expected graph for {}: {}", test, e)
"Error constructing expected graph for {}: {}",
test, e
))
})?; })?;
let with_order = let with_order =
if let StaticQueryResults::Solutions { ordered, .. } = &expected_results { if let StaticQueryResults::Solutions { ordered, .. } = &expected_results {
@ -114,19 +112,19 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
if are_query_results_isomorphic(&expected_results, &actual_results) { if are_query_results_isomorphic(&expected_results, &actual_results) {
Ok(()) Ok(())
} else { } else {
Err(Error::msg(format!("Failure on {}.\nExpected file:\n{}\nOutput file:\n{}\nParsed query:\n{}\nData:\n{}\n", Err(anyhow!("Failure on {}.\nExpected file:\n{}\nOutput file:\n{}\nParsed query:\n{}\nData:\n{}\n",
test, test,
actual_results, actual_results,
expected_results, expected_results,
Query::parse(&read_file_to_string(query_file)?, Some(query_file)).unwrap(), Query::parse(&read_file_to_string(query_file)?, Some(query_file)).unwrap(),
store store
))) ))
} }
} }
}, },
} }
} else { } else {
Err(Error::msg(format!("Unsupported test type: {}", test.kind))) Err(anyhow!("Unsupported test type: {}", test.kind))
} }
} }
@ -170,7 +168,7 @@ impl StaticServiceHandler {
} }
impl ServiceHandler for StaticServiceHandler { impl ServiceHandler for StaticServiceHandler {
fn handle(&self, service_name: NamedNode, query: Query) -> Result<QueryResult> { fn handle(&self, service_name: NamedNode, query: Query) -> oxigraph::Result<QueryResult> {
self.services self.services
.get(&service_name) .get(&service_name)
.ok_or_else(|| Error::msg(format!("Service {} not found", service_name)))? .ok_or_else(|| Error::msg(format!("Service {} not found", service_name)))?
@ -183,7 +181,9 @@ impl ServiceHandler for StaticServiceHandler {
fn to_dataset(result: QueryResult, with_order: bool) -> Result<MemoryStore> { fn to_dataset(result: QueryResult, with_order: bool) -> Result<MemoryStore> {
match result { match result {
QueryResult::Graph(graph) => graph.map(|t| t.map(|t| t.in_graph(None))).collect(), QueryResult::Graph(graph) => Ok(graph
.map(|t| t.map(|t| t.in_graph(None)))
.collect::<Result<_, Error>>()?),
QueryResult::Boolean(value) => { QueryResult::Boolean(value) => {
let store = MemoryStore::new(); let store = MemoryStore::new();
let result_set = BlankNode::default(); let result_set = BlankNode::default();

@ -1,4 +1,4 @@
use oxigraph::Result; use anyhow::Result;
use oxigraph_testsuite::manifest::TestManifest; use oxigraph_testsuite::manifest::TestManifest;
use oxigraph_testsuite::sparql_evaluator::evaluate_sparql_tests; use oxigraph_testsuite::sparql_evaluator::evaluate_sparql_tests;

@ -1,4 +1,4 @@
use oxigraph::Result; use anyhow::Result;
use oxigraph_testsuite::manifest::TestManifest; use oxigraph_testsuite::manifest::TestManifest;
use oxigraph_testsuite::parser_evaluator::evaluate_parser_tests; use oxigraph_testsuite::parser_evaluator::evaluate_parser_tests;

@ -1,4 +1,4 @@
use oxigraph::Result; use anyhow::Result;
use oxigraph_testsuite::manifest::TestManifest; use oxigraph_testsuite::manifest::TestManifest;
use oxigraph_testsuite::sparql_evaluator::evaluate_sparql_tests; use oxigraph_testsuite::sparql_evaluator::evaluate_sparql_tests;

@ -274,8 +274,8 @@ impl WikibaseLoader {
GraphSyntax::NTriples, GraphSyntax::NTriples,
&NamedNode::new(uri)?.into(), &NamedNode::new(uri)?.into(),
None, None,
) )?;
})?; Ok(())
Ok(()) })
} }
} }

Loading…
Cancel
Save