working on tests

pull/11/head
Dustin Whitney 5 years ago
parent a8ce5653f7
commit 75c786b350
  1. 44
      lib/src/sparql/eval.rs
  2. 71
      lib/tests/service_test_cases.rs
  3. 4
      lib/tests/sparql_test_cases.rs

@ -138,7 +138,7 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
} => { } => {
match &options.service_handler { match &options.service_handler {
None => if *silent { None => if *silent {
return Box::new(vec![].into_iter()); return Box::new(empty());
} else { } else {
return Box::new(once(Err(format_err!( return Box::new(once(Err(format_err!(
"No handler was supplied to resolve the given service" "No handler was supplied to resolve the given service"
@ -147,21 +147,30 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
Some(handler) => { Some(handler) => {
let pattern_option = match get_pattern_value(service_name, &[]) { let pattern_option = match get_pattern_value(service_name, &[]) {
None => if *silent { None => if *silent {
return Box::new(vec![].into_iter()); return Box::new(empty());
} else { } else {
return Box::new(once(Err(format_err!( return Box::new(once(Err(format_err!(
"The handler supplied was unable to evaluate the given service" "The handler supplied was unable to evaluate the given service"
)))) as EncodedTuplesIterator<'_>; )))) as EncodedTuplesIterator<'_>;
}, },
Some(term) => { Some(term) => {
let named_node = self.dataset.decode_named_node(term).unwrap(); match self.dataset.decode_named_node(term) {
Err(err) => if *silent {
return Box::new(empty());
} else {
return Box::new(once(Err(err))) as EncodedTuplesIterator<'_>;
},
Ok(named_node) => {
println!("named_node: {:?}", named_node);
handler.handle(named_node) handler.handle(named_node)
}
}
}, },
}; };
match pattern_option { match pattern_option {
None => if *silent { None => if *silent {
return Box::new(vec![].into_iter()); return Box::new(empty());
} else { } else {
return Box::new(once(Err(format_err!( return Box::new(once(Err(format_err!(
"The handler supplied was unable to produce any result set on the given service" "The handler supplied was unable to produce any result set on the given service"
@ -178,7 +187,13 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
buffered_results: vec![], buffered_results: vec![],
}) })
}, },
Err(err) => return Box::new(once(Err(err))) as EncodedTuplesIterator<'_> Err(err) => {
if *silent {
return Box::new(empty());
} else {
return Box::new(once(Err(err))) as EncodedTuplesIterator<'_>
}
}
} }
}, },
} }
@ -933,14 +948,23 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
_ => None, _ => None,
}?; }?;
let iri = self.dataset.get_str(iri_id).ok()??; let iri = self.dataset.get_str(iri_id).ok()??;
let base_iri = options.base_iri.map(|base_iri| Iri::parse(base_iri)); match options.base_iri {
if let Some(Ok(base_iri)) = base_iri { None => {
self.build_named_node(&base_iri.resolve(&iri).ok()?.into_inner()) Iri::parse(iri).ok()?;
} else { Some(EncodedTerm::NamedNode { iri_id })
},
Some(str_iri) => {
match Iri::parse(str_iri) {
Ok(base_iri) => self.build_named_node(&base_iri.resolve(&iri).ok()?.into_inner()),
_ => {
Iri::parse(iri).ok()?; Iri::parse(iri).ok()?;
Some(EncodedTerm::NamedNode { iri_id }) Some(EncodedTerm::NamedNode { iri_id })
} }
} }
}
}
}
PlanExpression::BNode(id) => match id { PlanExpression::BNode(id) => match id {
Some(id) => { Some(id) => {
if let EncodedTerm::StringLiteral { value_id } = if let EncodedTerm::StringLiteral { value_id } =

@ -105,18 +105,17 @@ fn two_service_test() {
} }
#[test] #[test]
fn silent_service_test() { fn silent_service_empty_set_test() {
#[derive(Clone,Copy)] #[derive(Clone,Copy)]
struct TwoServiceTest; struct ServiceTest;
impl ServiceHandler for TwoServiceTest { impl ServiceHandler for ServiceTest {
fn handle<'a>(&'a self, _named_node: NamedNode) -> Option<(fn(GraphPattern) -> Result<BindingsIterator<'a>>)> { fn handle<'a>(&'a self, _named_node: NamedNode) -> Option<(fn(GraphPattern) -> Result<BindingsIterator<'a>>)> {
Some(TwoServiceTest::handle_service) Some(ServiceTest::handle_service)
} }
} }
impl ServiceTest {
impl TwoServiceTest {
fn handle_service<'a>(_graph_pattern: GraphPattern) -> Result<BindingsIterator<'a>> { fn handle_service<'a>(_graph_pattern: GraphPattern) -> Result<BindingsIterator<'a>> {
Err(format_err!("This is supposed to fail")) Err(format_err!("This is supposed to fail"))
} }
@ -127,30 +126,60 @@ fn silent_service_test() {
SELECT ?name ?mbox SELECT ?name ?mbox
WHERE WHERE
{ {
SERVICE <http://service1.org> SERVICE SILENT <http://service1.org>
{ ?s foaf:name ?name { ?s foaf:name ?name
} }
SERVICE <http://service2.org>
{ ?s foaf:mbox ?mbox
}
} }
ORDER BY ?name ORDER BY ?name
"#.to_string(); "#.to_string();
let triples = br#" let triples = b"".as_ref();
<http://example.com/bob> <http://xmlns.com/foaf/0.1/name> "Bob" . let options = QueryOptions::default().with_service_handler(Box::new(ServiceTest));
<http://example.com/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
"#.as_ref();
let options = QueryOptions::default().with_service_handler(Box::new(TwoServiceTest));
let results = do_query(triples, query, options).unwrap(); let results = do_query(triples, query, options).unwrap();
let collected = results.into_values_iter().map(move |b| b.unwrap()).collect::<Vec<_>>(); let collected = results.into_values_iter().map(move |b| b.unwrap()).collect::<Vec<_>>();
let solution = vec![ println!("Collected: {:?}", collected);
vec![ Some(literal("Alice".to_string())), Some(mailto("alice@example.com".to_string())) ], assert_eq!(collected.len(), 0);
vec![ Some(literal("Bob".to_string())), Some(mailto("bob@example.com".to_string())) ], }
];
assert_eq!(collected, solution); #[test]
fn non_silent_service_test() {
#[derive(Clone,Copy)]
struct ServiceTest;
impl ServiceHandler for ServiceTest {
fn handle<'a>(&'a self, _named_node: NamedNode) -> Option<(fn(GraphPattern) -> Result<BindingsIterator<'a>>)> {
Some(ServiceTest::handle_service)
}
}
impl ServiceTest {
fn handle_service<'a>(_graph_pattern: GraphPattern) -> Result<BindingsIterator<'a>> {
Err(format_err!("This is supposed to fail"))
}
}
let query = r#"
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
WHERE
{
SERVICE <http://service1.org>
{ ?s foaf:name ?name
}
}
ORDER BY ?name
"#.to_string();
let triples = b"".as_ref();
let options = QueryOptions::default().with_service_handler(Box::new(ServiceTest));
let results = do_query(triples, query, options).unwrap();
let result = results.into_values_iter().next();
match result {
Some(Err(_)) => assert_eq!(true, true),
_ => assert_eq!(true, false, "This should have been an error since the service fails"),
}
} }
fn ex(id: String) -> Term { fn ex(id: String) -> Term {

@ -158,13 +158,13 @@ fn sparql_w3c_query_evaluation_testsuite() -> Result<()> {
} }
match repository match repository
.connection()? .connection()?
.prepare_query(&read_file_to_string(&test.query)?, None) .prepare_query(&read_file_to_string(&test.query)?, Some(&test.query))
{ {
Err(error) => Err(format_err!( Err(error) => Err(format_err!(
"Failure to parse query of {} with error: {}", "Failure to parse query of {} with error: {}",
test, error test, error
)), )),
Ok(query) => match query.exec(&QueryOptions::default()) { Ok(query) => match query.exec(&QueryOptions::default().with_base_iri(&test.query)) {
Err(error) => Err(format_err!( Err(error) => Err(format_err!(
"Failure to execute query of {} with error: {}", "Failure to execute query of {} with error: {}",
test, error test, error

Loading…
Cancel
Save