#[cfg(test)]
mod tests {
use super::super::core::Graph;
use super::super::export::GraphExport;
use oxigraph::io::RdfFormat;
use std::fs;
use tempfile::TempDir;
use testcontainers::{exec::SUCCESS_EXIT_CODE, ContainerClient, GenericContainer};
#[test]
fn test_export_to_file() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
let temp_dir = TempDir::new().unwrap();
let output_path = temp_dir.path().join("output.ttl");
let export = GraphExport::new(&graph);
export
.write_to_file(&output_path, RdfFormat::Turtle)
.unwrap();
assert!(output_path.exists());
let content = fs::read_to_string(&output_path).unwrap();
assert!(!content.is_empty());
assert!(content.contains("alice") || content.contains("Person"));
}
#[test]
fn test_export_to_string() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
let export = GraphExport::new(&graph);
let output = export.write_to_string(RdfFormat::Turtle).unwrap();
assert!(!output.is_empty());
}
#[test]
fn test_export_auto_format() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
let temp_dir = TempDir::new().unwrap();
let export = GraphExport::new(&graph);
let ttl_path = temp_dir.path().join("output.ttl");
export.write_to_file_auto(&ttl_path).unwrap();
let nt_path = temp_dir.path().join("output.nt");
export.write_to_file_auto(&nt_path).unwrap();
assert!(ttl_path.exists());
assert!(nt_path.exists());
}
#[cfg(feature = "docker")]
#[test]
fn test_export_in_container_with_verification() {
let client = ContainerClient::default();
let container = GenericContainer::with_command(
client.client(),
"alpine",
"latest",
"sleep",
&["infinity"],
)
.unwrap();
let workspace_setup = container
.exec("sh", &["-c", "mkdir -p /workspace/exports"])
.unwrap();
assert_eq!(
workspace_setup.exit_code, SUCCESS_EXIT_CODE,
"Failed to create workspace in container"
);
let rdf_content = r#"@prefix ex: <http://example.org/> .
ex:alice a ex:Person ."#;
let rdf_file = "/workspace/exports/input.ttl";
let file_create = container
.exec(
"sh",
&[
"-c",
&format!("cat > {} << 'EOF'\n{}\nEOF", rdf_file, rdf_content),
],
)
.unwrap();
assert_eq!(
file_create.exit_code, SUCCESS_EXIT_CODE,
"Should be able to create RDF file in container"
);
let file_check = container.exec("test", &["-f", rdf_file]).unwrap();
assert_eq!(
file_check.exit_code, SUCCESS_EXIT_CODE,
"RDF file should exist in container"
);
let content_check = container.exec("cat", &[rdf_file]).unwrap();
assert_eq!(
content_check.exit_code, SUCCESS_EXIT_CODE,
"Should be able to read file from container"
);
assert!(
content_check.stdout.contains("alice"),
"File should contain expected RDF content"
);
let list_result = container
.exec("ls", &["-la", "/workspace/exports"])
.unwrap();
assert_eq!(
list_result.exit_code, SUCCESS_EXIT_CODE,
"Should be able to list files in container"
);
assert!(
list_result.stdout.contains("input.ttl"),
"Should see exported file in container filesystem"
);
}
}