use std::collections::HashMap;
use graphdblite::{Database, NodeId, Value};
#[test]
fn write_transaction_commit() {
let mut db = Database::open_memory().unwrap();
{
let tx = db.write_tx().unwrap();
tx.create_node("A", HashMap::new()).unwrap();
tx.commit().unwrap();
}
{
let tx = db.read_tx().unwrap();
assert!(tx.node_exists(NodeId(1)).unwrap());
tx.commit().unwrap();
}
}
#[test]
fn write_transaction_rollback() {
let mut db = Database::open_memory().unwrap();
{
let tx = db.write_tx().unwrap();
tx.create_node("A", HashMap::new()).unwrap();
tx.rollback().unwrap();
}
{
let tx = db.read_tx().unwrap();
assert!(!tx.node_exists(NodeId(1)).unwrap());
tx.commit().unwrap();
}
}
#[test]
fn write_transaction_drop_rollsback() {
let mut db = Database::open_memory().unwrap();
{
let tx = db.write_tx().unwrap();
tx.create_node("A", HashMap::new()).unwrap();
}
{
let tx = db.read_tx().unwrap();
assert!(!tx.node_exists(NodeId(1)).unwrap());
tx.commit().unwrap();
}
}
#[test]
fn persistent_across_reopen() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.db");
{
let mut db = Database::open(&path).unwrap();
let tx = db.write_tx().unwrap();
tx.create_node("Person", {
let mut m = HashMap::new();
m.insert("name".to_string(), Value::String("Alice".into()));
m
})
.unwrap();
tx.commit().unwrap();
}
{
let mut db = Database::open(&path).unwrap();
let tx = db.read_tx().unwrap();
let node = tx.get_node(NodeId(1)).unwrap();
assert_eq!(node.labels, vec!["Person".to_string()]);
assert_eq!(
node.properties.get("name"),
Some(&Value::String("Alice".into()))
);
tx.commit().unwrap();
}
}