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
[dependencies]
anyhow = "1"
chrono = "0.4"
lazy_static = "1"
oxigraph = { path = "../lib" }

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

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

@ -1,8 +1,8 @@
use crate::files::load_store;
use crate::manifest::Test;
use crate::report::TestResult;
use anyhow::{anyhow, Result};
use chrono::Utc;
use oxigraph::{Error, Result};
pub fn evaluate_parser_tests(
manifest: impl Iterator<Item = Result<Test>>,
@ -24,7 +24,7 @@ fn evaluate_parser_test(test: &Test) -> Result<()> {
let action = test
.action
.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"
|| test.kind == "http://www.w3.org/ns/rdftest#TestNQuadsPositiveSyntax"
|| test.kind == "http://www.w3.org/ns/rdftest#TestTurtlePositiveSyntax"
@ -32,7 +32,7 @@ fn evaluate_parser_test(test: &Test) -> Result<()> {
{
match load_store(action) {
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"
|| 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"
{
match load_store(action) {
Ok(_) => Err(Error::msg(
"File parsed with an error even if it should not",
)),
Ok(_) => Err(anyhow!("File parsed with an error even if it should not",)),
Err(_) => Ok(()),
}
} 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) {
Ok(())
} else {
Err(Error::msg(format!(
Err(anyhow!(
"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 {
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 {
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 oxigraph::model::NamedNode;
use oxigraph::Result;
#[derive(Debug)]
pub struct TestResult {

@ -2,11 +2,12 @@ use crate::files::*;
use crate::manifest::*;
use crate::report::*;
use crate::vocab::*;
use anyhow::{anyhow, Result};
use chrono::Utc;
use oxigraph::model::vocab::*;
use oxigraph::model::*;
use oxigraph::sparql::*;
use oxigraph::{Error, MemoryStore, Result};
use oxigraph::{Error, MemoryStore};
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
@ -36,20 +37,17 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
let query_file = test
.action
.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)) {
Err(error) => Err(Error::msg(format!(
"Not able to parse {} with error: {}",
test, error
))),
Err(error) => Err(anyhow!("Not able to parse {} with error: {}", test, error)),
Ok(query) => match Query::parse(&query.to_string(), None) {
Ok(_) => Ok(()),
Err(error) => Err(Error::msg(format!(
Err(error) => Err(anyhow!(
"Failure to deserialize \"{}\" of {} with error: {}",
query.to_string(),
test,
error
))),
)),
},
}
} else if test.kind
@ -60,12 +58,13 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
let query_file = test
.action
.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)) {
Ok(result) => Err(Error::msg(format!(
Ok(result) => Err(anyhow!(
"Oxigraph parses even if it should not {}. The output tree is: {}",
test, result
))),
test,
result
)),
Err(_) => Ok(()),
}
} else if test.kind
@ -81,26 +80,25 @@ fn evaluate_sparql_test(test: &Test) -> Result<()> {
let query_file = test
.query
.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()
.with_service_handler(StaticServiceHandler::new(&test.service_data)?);
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: {}",
test, error
))),
test,
error
)),
Ok(query) => match store.query(query, options) {
Err(error) => Err(Error::msg(format!(
Err(error) => Err(anyhow!(
"Failure to execute query of {} with error: {}",
test, error
))),
test,
error
)),
Ok(actual_results) => {
let expected_results = load_sparql_query_result(test.result.as_ref().unwrap())
.map_err(|e| {
Error::msg(format!(
"Error constructing expected graph for {}: {}",
test, e
))
anyhow!("Error constructing expected graph for {}: {}", test, e)
})?;
let with_order =
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) {
Ok(())
} 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,
actual_results,
expected_results,
Query::parse(&read_file_to_string(query_file)?, Some(query_file)).unwrap(),
store
)))
))
}
}
},
}
} 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 {
fn handle(&self, service_name: NamedNode, query: Query) -> Result<QueryResult> {
fn handle(&self, service_name: NamedNode, query: Query) -> oxigraph::Result<QueryResult> {
self.services
.get(&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> {
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) => {
let store = MemoryStore::new();
let result_set = BlankNode::default();

@ -1,4 +1,4 @@
use oxigraph::Result;
use anyhow::Result;
use oxigraph_testsuite::manifest::TestManifest;
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::parser_evaluator::evaluate_parser_tests;

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

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

Loading…
Cancel
Save