Skip to content

Commit

Permalink
all tests are in separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Nov 24, 2024
1 parent 4dfe89e commit 13a60e4
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 112 deletions.
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ fn cmp_input_file(
}
mod triple_common_parser_test_misc;
mod turtle_doc_test;
mod turtle_doc_test_misc;
mod turtle_parser_test_misc;
106 changes: 106 additions & 0 deletions src/tests/turtle_doc_test_misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::{
shared::XSD_STRING,
turtle::turtle_doc::{
Literal, Node, RdfJsonNode, RdfJsonNodeResult, RdfJsonTriple, Statement, TurtleDoc,
},
};
use serial_test::serial;
use std::borrow::Cow;
use Cow::Borrowed;
use Node::Iri;

#[test]
#[serial]
fn turtle_doc_add_test() {
let doc1 = r#"
@prefix : <http://example.com/>.
:a :b ( "apple" "banana" ) .
"#;
let doc2 = r#"
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
foaf:name "Bob" ;
foaf:lastName "George", "Joshua" ;
foaf:knows [
foaf:name "Eve" ] ;
foaf:mbox <[email protected]>] .
"#;
let turtle1: TurtleDoc = (doc1, None).try_into().unwrap();
assert_eq!(5, turtle1.list_statements(None, None, None).len());

let turtle2: TurtleDoc = (doc2, None).try_into().unwrap();
assert_eq!(8, turtle2.list_statements(None, None, None).len());

let turtle3 = turtle1 + turtle2;
assert_eq!(13, turtle3.list_statements(None, None, None).len());
let mut turtle = TurtleDoc::default();
turtle.add_statement(
Iri(Borrowed("http://xxx.com/123")),
Iri(Borrowed("http://bar.com/345")),
Node::Literal(Literal::Decimal(123f32)),
);
let turtle4 = turtle + turtle3;
assert_eq!(14, turtle4.list_statements(None, None, None).len());
}

#[test]
#[serial]
fn turtle_doc_list_statements_test() {
let doc = r#"
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
foaf:name "Bob" ;
foaf:lastName "George", "Joshua" ;
foaf:knows [
foaf:name "Eve" ] ;
foaf:mbox <[email protected]>] .
"#;
let turtle: TurtleDoc = (doc, None).try_into().unwrap();
let statements = turtle.list_statements(None, None, Some(&Iri(Borrowed("[email protected]"))));
assert_eq!(1, statements.len());
println!("{statements:?}");
let statement = statements[0];
let statements = turtle.list_statements(Some(&statement.subject), None, None);
assert_eq!(5, statements.len());
}

#[test]
#[serial]
fn test_convert_rdf_triple_to_doc() {
let triple = RdfJsonTriple {
subject: RdfJsonNodeResult::SingleNode(RdfJsonNode {
typ: "uri".into(),
datatype: None,
lang: None,
value: "http://xx.com/xxx".into(),
}),
predicate: RdfJsonNodeResult::SingleNode(RdfJsonNode {
typ: "uri".into(),
datatype: None,
lang: None,
value: "http://xx.com/pred".into(),
}),
object: RdfJsonNodeResult::SingleNode(RdfJsonNode {
typ: "literal".into(),
datatype: Some(XSD_STRING.into()),
lang: None,
value: "hello".into(),
}),
};
let stmt: Statement = (&triple).try_into().unwrap();

let expected = Statement {
subject: Node::Iri(Cow::Borrowed("http://xx.com/xxx")),
predicate: Node::Iri(Cow::Borrowed("http://xx.com/pred")),
object: Node::Literal(Literal::Quoted {
datatype: Some(Box::new(Node::Iri(Cow::Borrowed(
"http://www.w3.org/2001/XMLSchema#string",
)))),
value: Cow::Borrowed("hello"),
lang: None,
}),
};
assert_eq!(stmt, expected);
}
4 changes: 2 additions & 2 deletions src/tests/turtle_parser_test_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ fn predicate_lists_testx() {
_:helium <http://example.org/elements/atomicNumber> "2".
"#;
let (_, res) = triples(s).unwrap();
dbg!(
assert_eq!(
res,
TurtleValue::Statement {
subject: Box::new(TurtleValue::BNode(BlankNode::Labeled("helium",),)),
Expand All @@ -746,7 +746,7 @@ fn predicate_lists_testx() {
<http://en.wikipedia.org/wiki/Helium> <http://example.org/elements/atomicNumber> "2".
"#;
let (_, res) = triples(s).unwrap();
dbg!(
assert_eq!(
res,
TurtleValue::Statement {
subject: Box::new(TurtleValue::Iri(Iri::Enclosed(
Expand Down
110 changes: 0 additions & 110 deletions src/turtle/turtle_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,113 +1098,3 @@ impl Display for TurtleDocError {
write!(f, "error: {}", self.message)
}
}
#[cfg(test)]
mod test {
use crate::{
shared::XSD_STRING,
turtle::turtle_doc::{Literal, Node, RdfJsonTriple, Statement, TurtleDoc},
};
use serial_test::serial;
use std::borrow::Cow;
use Cow::Borrowed;
use Node::Iri;

use super::RdfJsonNodeResult;

#[test]
#[serial]
fn turtle_doc_add_test() {
let doc1 = r#"
@prefix : <http://example.com/>.
:a :b ( "apple" "banana" ) .
"#;
let doc2 = r#"
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
foaf:name "Bob" ;
foaf:lastName "George", "Joshua" ;
foaf:knows [
foaf:name "Eve" ] ;
foaf:mbox <[email protected]>] .
"#;
let turtle1: TurtleDoc = (doc1, None).try_into().unwrap();
assert_eq!(5, turtle1.statements.len());

let turtle2: TurtleDoc = (doc2, None).try_into().unwrap();
assert_eq!(8, turtle2.statements.len());

let turtle3 = turtle1 + turtle2;
assert_eq!(13, turtle3.statements.len());
let mut turtle = TurtleDoc::default();
turtle.add_statement(
Iri(Borrowed("http://xxx.com/123")),
Iri(Borrowed("http://bar.com/345")),
Node::Literal(Literal::Decimal(123f32)),
);
let turtle4 = turtle + turtle3;
assert_eq!(14, turtle4.statements.len());
}

#[test]
#[serial]
fn turtle_doc_list_statements_test() {
let doc = r#"
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
foaf:name "Bob" ;
foaf:lastName "George", "Joshua" ;
foaf:knows [
foaf:name "Eve" ] ;
foaf:mbox <[email protected]>] .
"#;
let turtle: TurtleDoc = (doc, None).try_into().unwrap();
let statements =
turtle.list_statements(None, None, Some(&Iri(Borrowed("[email protected]"))));
assert_eq!(1, statements.len());
println!("{statements:?}");
let statement = statements[0];
let statements = turtle.list_statements(Some(&statement.subject), None, None);
assert_eq!(5, statements.len());
}

#[test]
#[serial]
fn test_convert_rdf_triple_to_doc() {
let triple = RdfJsonTriple {
subject: RdfJsonNodeResult::SingleNode(super::RdfJsonNode {
typ: "uri".into(),
datatype: None,
lang: None,
value: "http://xx.com/xxx".into(),
}),
predicate: RdfJsonNodeResult::SingleNode(super::RdfJsonNode {
typ: "uri".into(),
datatype: None,
lang: None,
value: "http://xx.com/pred".into(),
}),
object: RdfJsonNodeResult::SingleNode(super::RdfJsonNode {
typ: "literal".into(),
datatype: Some(XSD_STRING.into()),
lang: None,
value: "hello".into(),
}),
};
let stmt: Statement = (&triple).try_into().unwrap();

let expected = Statement {
subject: Node::Iri(Cow::Borrowed("http://xx.com/xxx")),
predicate: Node::Iri(Cow::Borrowed("http://xx.com/pred")),
object: Node::Literal(Literal::Quoted {
datatype: Some(Box::new(Node::Iri(Cow::Borrowed(
"http://www.w3.org/2001/XMLSchema#string",
)))),
value: Cow::Borrowed("hello"),
lang: None,
}),
};
assert_eq!(stmt, expected);
}
}

0 comments on commit 13a60e4

Please sign in to comment.