use sqlitegraph::{
GraphEdge, GraphEntity, GraphQuery, PatternTriple, SqliteGraph, SqliteGraphError,
match_triples, match_triples_fast,
};
use tempfile::tempdir;
#[test]
fn test_can_construct_graph_from_path() {
let temp_dir = tempdir().expect("Failed to create temp dir");
let db_path = temp_dir.path().join("test.db");
let graph = SqliteGraph::open(&db_path).expect("Failed to create graph");
let entity1 = graph
.insert_entity(&GraphEntity {
id: 0,
kind: "TestNode".to_string(),
name: "node1".to_string(),
file_path: None,
data: serde_json::json!({"type": "test"}),
})
.expect("Failed to insert entity1");
let entity2 = graph
.insert_entity(&GraphEntity {
id: 0,
kind: "TestNode".to_string(),
name: "node2".to_string(),
file_path: None,
data: serde_json::json!({"type": "test"}),
})
.expect("Failed to insert entity2");
let edge_id = graph
.insert_edge(&GraphEdge {
id: 0,
from_id: entity1,
to_id: entity2,
edge_type: "TEST_EDGE".to_string(),
data: serde_json::json!({"relationship": "test"}),
})
.expect("Failed to insert edge");
let query = GraphQuery::new(&graph);
let neighbors = query.neighbors(entity1).expect("Failed to get neighbors");
assert_eq!(neighbors.len(), 1);
assert_eq!(neighbors[0], entity2);
let retrieved_edge = graph.get_edge(edge_id).expect("Failed to get edge");
assert_eq!(retrieved_edge.from_id, entity1);
assert_eq!(retrieved_edge.to_id, entity2);
assert_eq!(retrieved_edge.edge_type, "TEST_EDGE");
}
#[test]
fn test_pattern_triple_basic_through_lib_api() {
let graph = SqliteGraph::open_in_memory().expect("Failed to create in-memory graph");
let entity1 = graph
.insert_entity(&GraphEntity {
id: 0,
kind: "Function".to_string(),
name: "func1".to_string(),
file_path: None,
data: serde_json::json!({"lang": "rust"}),
})
.expect("Failed to insert entity1");
let entity2 = graph
.insert_entity(&GraphEntity {
id: 0,
kind: "Function".to_string(),
name: "func2".to_string(),
file_path: None,
data: serde_json::json!({"lang": "rust"}),
})
.expect("Failed to insert entity2");
let _edge_id = graph
.insert_edge(&GraphEdge {
id: 0,
from_id: entity1,
to_id: entity2,
edge_type: "CALLS".to_string(),
data: serde_json::json!({}),
})
.expect("Failed to insert edge");
let pattern = PatternTriple::new("CALLS");
let matches = match_triples(&graph, &pattern).expect("Failed to match triples");
assert_eq!(matches.len(), 1);
assert_eq!(matches[0].start_id, entity1);
assert_eq!(matches[0].end_id, entity2);
let fast_matches = match_triples_fast(&graph, &pattern).expect("Failed to match triples fast");
assert_eq!(fast_matches.len(), 1);
assert_eq!(fast_matches[0].start_id, entity1);
assert_eq!(fast_matches[0].end_id, entity2);
assert_eq!(matches, fast_matches);
}
#[test]
fn test_snapshot_and_wal_through_lib_api() {
let graph = SqliteGraph::open_in_memory().expect("Failed to create in-memory graph");
let entity1 = graph
.insert_entity(&GraphEntity {
id: 0,
kind: "TestNode".to_string(),
name: "node1".to_string(),
file_path: None,
data: serde_json::json!({}),
})
.expect("Failed to insert entity1");
let entity2 = graph
.insert_entity(&GraphEntity {
id: 0,
kind: "TestNode".to_string(),
name: "node2".to_string(),
file_path: None,
data: serde_json::json!({}),
})
.expect("Failed to insert entity2");
let _edge_id = graph
.insert_edge(&GraphEdge {
id: 0,
from_id: entity1,
to_id: entity2,
edge_type: "TEST_REL".to_string(),
data: serde_json::json!({}),
})
.expect("Failed to insert edge");
let query = GraphQuery::new(&graph);
let _warmup1 = query.neighbors(entity1);
let _warmup2 = query.neighbors(entity2);
let snapshot = graph
.acquire_snapshot()
.expect("Failed to acquire snapshot");
assert!(snapshot.node_count() > 0);
assert!(snapshot.edge_count() > 0);
assert!(snapshot.contains_node(entity1));
assert!(snapshot.contains_node(entity2));
let outgoing = snapshot.get_outgoing(entity1);
assert!(outgoing.is_some());
assert!(outgoing.unwrap().contains(&entity2));
}
#[test]
fn test_error_types_through_lib_api() {
let graph = SqliteGraph::open_in_memory().expect("Failed to create in-memory graph");
let result = graph.get_entity(999);
assert!(result.is_err());
match result.unwrap_err() {
SqliteGraphError::NotFound(_) => {} other => panic!("Expected NotFound error, got: {:?}", other),
}
let invalid_entity = GraphEntity {
id: 0,
kind: "".to_string(), name: "test".to_string(),
file_path: None,
data: serde_json::json!({}),
};
let result = graph.insert_entity(&invalid_entity);
assert!(result.is_err());
match result.unwrap_err() {
SqliteGraphError::InvalidInput(_) => {} other => panic!("Expected InvalidInput error, got: {:?}", other),
}
}