From a582847bd730af97be573e8d5e68e736a1761148 Mon Sep 17 00:00:00 2001 From: Tpt Date: Thu, 24 Feb 2022 21:07:01 +0100 Subject: [PATCH] SPARQL tests: validates results graph --- testsuite/src/sparql_evaluator.rs | 53 ++++++++++++++++--------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/testsuite/src/sparql_evaluator.rs b/testsuite/src/sparql_evaluator.rs index f2ba6370..773792ab 100644 --- a/testsuite/src/sparql_evaluator.rs +++ b/testsuite/src/sparql_evaluator.rs @@ -325,7 +325,7 @@ fn load_sparql_query_result(url: &str) -> Result { false, ) } else { - Ok(StaticQueryResults::from_graph(load_graph(url)?)) + StaticQueryResults::from_graph(load_graph(url)?) } } @@ -547,10 +547,10 @@ impl fmt::Display for StaticQueryResults { impl StaticQueryResults { fn from_query_results(results: QueryResults, with_order: bool) -> Result { - Ok(Self::from_graph(to_graph(results, with_order)?)) + Self::from_graph(to_graph(results, with_order)?) } - fn from_graph(graph: Graph) -> StaticQueryResults { + fn from_graph(graph: Graph) -> Result { // Hack to normalize literals let store = Store::new().unwrap(); for t in graph.iter() { @@ -563,28 +563,30 @@ impl StaticQueryResults { if let Some(result_set) = graph.subject_for_predicate_object(rdf::TYPE, rs::RESULT_SET) { if let Some(bool) = graph.object_for_subject_predicate(result_set, rs::BOOLEAN) { // Boolean query - StaticQueryResults::Boolean(bool == Literal::from(true).as_ref().into()) + Ok(StaticQueryResults::Boolean( + bool == Literal::from(true).as_ref().into(), + )) } else { // Regular query let mut variables: Vec = graph .objects_for_subject_predicate(result_set, rs::RESULT_VARIABLE) - .filter_map(|object| { + .map(|object| { if let TermRef::Literal(l) = object { - Some(Variable::new_unchecked(l.value())) + Ok(Variable::new_unchecked(l.value())) } else { - None + Err(anyhow!("Invalid rs:resultVariable: {}", object)) } }) - .collect(); + .collect::>>()?; variables.sort(); - let mut solutions: Vec<_> = graph + let mut solutions = graph .objects_for_subject_predicate(result_set, rs::SOLUTION) - .filter_map(|object| { + .map(|object| { if let TermRef::BlankNode(solution) = object { let mut bindings = graph .objects_for_subject_predicate(solution, rs::BINDING) - .filter_map(|object| { + .map(|object| { if let TermRef::BlankNode(binding) = object { if let (Some(TermRef::Literal(variable)), Some(value)) = ( graph.object_for_subject_predicate( @@ -593,50 +595,51 @@ impl StaticQueryResults { ), graph.object_for_subject_predicate(binding, rs::VALUE), ) { - Some(( + Ok(( Variable::new_unchecked(variable.value()), value.into_owned(), )) } else { - None + Err(anyhow!("Invalid rs:binding: {}", binding)) } } else { - None + Err(anyhow!("Invalid rs:binding: {}", object)) } }) - .collect::>(); + .collect::>>()?; bindings.sort_by(|(a, _), (b, _)| a.cmp(b)); let index = graph .object_for_subject_predicate(solution, rs::INDEX) - .and_then(|object| { + .map(|object| { if let TermRef::Literal(l) = object { - u64::from_str(l.value()).ok() + Ok(u64::from_str(l.value())?) } else { - None + Err(anyhow!("Invalid rs:index: {}", object)) } - }); - Some((bindings, index)) + }) + .transpose()?; + Ok((bindings, index)) } else { - None + Err(anyhow!("Invalid rs:solution: {}", object)) } }) - .collect(); + .collect::>>()?; solutions.sort_by(|(_, index_a), (_, index_b)| index_a.cmp(index_b)); let ordered = solutions.iter().all(|(_, index)| index.is_some()); - StaticQueryResults::Solutions { + Ok(StaticQueryResults::Solutions { variables, solutions: solutions .into_iter() .map(|(solution, _)| solution) .collect(), ordered, - } + }) } } else { graph.canonicalize(); - StaticQueryResults::Graph(graph) + Ok(StaticQueryResults::Graph(graph)) } } }