#[cfg(test)]
mod tests {
use super::super::core::Graph;
use std::fs;
use std::io::Write;
use tempfile::TempDir;
use testcontainers::{exec::SUCCESS_EXIT_CODE, ContainerClient, GenericContainer};
#[test]
fn test_load_from_file() {
let temp_dir = TempDir::new().unwrap();
let rdf_file = temp_dir.path().join("data.ttl");
let mut file = fs::File::create(&rdf_file).unwrap();
writeln!(
file,
r#"@prefix ex: <http://example.org/> .
ex:alice a ex:Person ."#
)
.unwrap();
drop(file);
let graph = Graph::load_from_file(&rdf_file).unwrap();
assert!(!graph.is_empty());
assert!(graph.len() > 0);
}
#[test]
fn test_load_path_auto_format() {
let temp_dir = TempDir::new().unwrap();
let rdf_file = temp_dir.path().join("data.ttl");
let mut file = fs::File::create(&rdf_file).unwrap();
writeln!(
file,
r#"@prefix ex: <http://example.org/> .
ex:alice a ex:Person ."#
)
.unwrap();
drop(file);
let graph = Graph::new().unwrap();
graph.load_path(&rdf_file).unwrap();
assert!(!graph.is_empty());
}
#[test]
fn test_load_from_nonexistent_file() {
let temp_dir = TempDir::new().unwrap();
let nonexistent = temp_dir.path().join("nonexistent.ttl");
let graph = Graph::new().unwrap();
assert!(graph.load_path(&nonexistent).is_err());
}
#[cfg(feature = "docker")]
#[test]
fn test_load_file_in_container_with_verification() {
let client = ContainerClient::default();
let container = GenericContainer::with_command(
client.client(),
"alpine",
"latest",
"sleep",
&["infinity"],
)
.unwrap();
let rdf_content = r#"@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
ex:bob a ex:Person ."#;
let rdf_file = "/workspace/data.ttl";
let dir_setup = container
.exec("sh", &["-c", "mkdir -p /workspace"])
.unwrap();
assert_eq!(
dir_setup.exit_code, SUCCESS_EXIT_CODE,
"Failed to create workspace in container"
);
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"
);
assert!(
content_check.stdout.contains("bob"),
"File should contain all expected RDF content"
);
let size_check = container.exec("wc", &["-c", rdf_file]).unwrap();
assert_eq!(
size_check.exit_code, SUCCESS_EXIT_CODE,
"Should be able to check file size in container"
);
let file_size: usize = size_check
.stdout
.split_whitespace()
.next()
.unwrap_or("0")
.parse()
.unwrap_or(0);
assert!(file_size > 0, "File should have content in container");
let list_result = container.exec("ls", &["-lah", "/workspace"]).unwrap();
assert_eq!(
list_result.exit_code, SUCCESS_EXIT_CODE,
"Should be able to list files in container"
);
assert!(
list_result.stdout.contains("data.ttl"),
"Should see RDF file in container filesystem listing"
);
}
}