Skip to content

Commit

Permalink
Issue #1 WIP Added oxhdt-sys crate with unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
donpellegrino authored and GregHanson committed Nov 4, 2024
1 parent 2d6fd6f commit 9c2f831
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"cli",
"js",
"lib/hdt_driver",
"lib/oxigraph",
"lib/oxrdf",
"lib/oxrdfio",
Expand All @@ -13,11 +14,10 @@ members = [
"lib/spargeo",
"lib/sparopt",
"lib/sparql-smith",
"lib/hdt_driver",
"oxhdt-sys",
"oxrocksdb-sys",
"python",
"testsuite",

"testsuite"
]
resolver = "2"

Expand Down
4 changes: 4 additions & 0 deletions lib/hdt_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
name = "hdt_driver"
version = "0.1.0"
edition = "2021"
authors = ["Don Pellegrino <[email protected]>"]
description = """
Rust test driver for Oxigraph HDT calls.
"""

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
6 changes: 0 additions & 6 deletions lib/hdt_driver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ fn main() {
.write(io::stdout(), QueryResultsFormat::Csv)
.unwrap();

// if let QueryResults::Solutions(solutions) = results.unwrap() {
// for solution in solutions {
// println!("{}", solution.unwrap().get("o").unwrap());
// }
// }

// Test
println!();
println!("Test");
Expand Down
13 changes: 13 additions & 0 deletions oxhdt-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "oxhdt-sys"
version = "0.1.0"
edition = "2021"
authors = ["Don Pellegrino <[email protected]>"]
description = """
Rust bindings for HDT for Oxigraph usage.
"""

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
oxigraph = { path = "../lib/oxigraph" }
72 changes: 72 additions & 0 deletions oxhdt-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#[allow(unused_imports)]
use oxigraph::model::{NamedNode, Literal};
use oxigraph::sparql::EvaluationError;
use oxigraph::sparql::QueryOptions;
use oxigraph::sparql::QueryResults;
use oxigraph::sparql::dataset::HDTDatasetView;
use oxigraph::sparql::evaluate_hdt_query;
use std::rc::Rc;

#[allow(dead_code)]
fn hdt_query(hdt_path: &str, sparql_query: &str) -> Result<QueryResults, EvaluationError> {
// Open the HDT file.
let dataset = Rc::new(HDTDatasetView::new(hdt_path));
let sparql_query = sparql_query;

// SPARQL query
let (results, _explain) = evaluate_hdt_query(
Rc::clone(&dataset),
sparql_query,
QueryOptions::default(),
false,
)
.expect("failed to evaluate SPARQL query");

return results;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn hdt_sparql_select_o_literal_by_s_uri() {
let ex = Literal::new_simple_literal("SPARQL Tutorial");

if let QueryResults::Solutions(mut solutions) = hdt_query(
"/home/u133615/projects/oxigraph/hdt_driver/test.hdt",
"SELECT ?o WHERE { <http://example.org/book/book1> ?p ?o }"
).unwrap() {
assert_eq!(solutions.next().unwrap().unwrap().get("o"), Some(&ex.into()));
}
}

#[test]
fn hdt_sparql_select_s_uri_by_p_uri() {
let ex = NamedNode::new("http://example.org/book/book1").unwrap();

if let QueryResults::Solutions(mut solutions) = hdt_query(
"/home/u133615/projects/oxigraph/hdt_driver/test.hdt",
"SELECT ?s WHERE { ?s <http://purl.org/dc/elements/1.1/title> ?o }"
).unwrap() {
assert_eq!(solutions.next().unwrap().unwrap().get("s"), Some(&ex.into()));
}
}

#[test]
fn hdt_sparql_select_spo_by_s_uri_and_o_literal() {
let ex_s = NamedNode::new("http://example.org/book/book1").unwrap();
let ex_p = NamedNode::new("http://purl.org/dc/elements/1.1/title").unwrap();
let ex_o = Literal::new_simple_literal("SPARQL Tutorial");

if let QueryResults::Solutions(mut solutions) = hdt_query(
"/home/u133615/projects/oxigraph/hdt_driver/test.hdt",
"SELECT ?s ?p ?o WHERE { <http://example.org/book/book1> ?p ?o . ?s ?p \"SPARQL Tutorial\" }"
).unwrap() {
let row = solutions.next().unwrap().unwrap();
assert_eq!(row.get("s"), Some(&ex_s.into()));
assert_eq!(row.get("p"), Some(&ex_p.into()));
assert_eq!(row.get("o"), Some(&ex_o.into()));
}
}
}

0 comments on commit 9c2f831

Please sign in to comment.