use moltendb_core::engine::{Db, StorageBackend, LogEntry};
use serde_json::json;
#[tokio::test]
async fn test_batch_atomicity() {
let log_path = "test_atomicity.log";
let _ = std::fs::remove_file(log_path);
{
let db = Db::open(log_path, true, false, 50000, 100, 1, 1024 * 1024, None).unwrap();
let tx_id = "test-tx-123".to_string();
db.storage.write_entry(&LogEntry {
cmd: "TX_BEGIN".into(),
collection: "test".into(),
key: tx_id.clone(),
value: json!(null),
}).unwrap();
db.storage.write_entry(&LogEntry {
cmd: "INSERT".into(),
collection: "test".into(),
key: "key1".into(),
value: json!({"data": "should not exist"}),
}).unwrap();
}
{
let db = Db::open(log_path, true, false, 50000, 100, 1, 1024 * 1024, None).unwrap();
assert!(db.get("test", "key1").is_none(), "Key1 should not exist because transaction was never committed");
}
{
let db = Db::open(log_path, true, false, 50000, 100, 1, 1024 * 1024, None).unwrap();
let items = vec![
("key2".to_string(), json!({"data": "should exist"})),
("key3".to_string(), json!({"data": "also should exist"})),
];
db.insert_batch("test", items).unwrap();
assert!(db.get("test", "key2").is_some());
assert!(db.get("test", "key3").is_some());
}
{
let db = Db::open(log_path, true, false, 50000, 100, 1, 1024 * 1024, None).unwrap();
assert!(db.get("test", "key2").is_some(), "Key2 should exist after proper commit");
assert!(db.get("test", "key3").is_some(), "Key3 should exist after proper commit");
assert!(db.get("test", "key1").is_none(), "Key1 should still not exist");
}
let _ = std::fs::remove_file(log_path);
let _ = std::fs::remove_file(format!("{}.snapshot", log_path));
}