aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/tests
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2025-02-03 10:27:00 +0200
committerJan Tuomi <jan@jantuomi.fi>2025-02-03 10:27:00 +0200
commitde254873cfd40f2ef2e77bcffb3bf0f9ec0b1c0b (patch)
treebd88e908c7f6b49eab4ff15a44bb58212eec8b15 /log_db/tests
parent106b53212fd04f6dd236826d39a52443d355d553 (diff)
Add transaction support
Diffstat (limited to 'log_db/tests')
-rw-r--r--log_db/tests/integration.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/log_db/tests/integration.rs b/log_db/tests/integration.rs
index 5535adf..5d56fbf 100644
--- a/log_db/tests/integration.rs
+++ b/log_db/tests/integration.rs
@@ -27,6 +27,10 @@ pub fn tmp_dir() -> String {
#[ctor]
fn init_logger() {
let _ = env_logger::builder().is_test(true).try_init();
+
+ // todo add panic hook stuff
+ // - https://stackoverflow.com/questions/54917373/retrieving-backtrace-from-a-panic-in-hook-in-rust
+ // - https://github.com/sndels/yuki/blob/e86b379165ec657197b1c14b78164bd09a8aa1dc/yuki/src/main.rs#L74
}
#[derive(Eq, PartialEq, Clone, Debug)]
@@ -36,6 +40,7 @@ enum Field {
Data,
}
+#[derive(Debug)]
struct Inst {
pub id: i64,
pub name: Option<String>,
@@ -647,3 +652,69 @@ fn test_batch_find_by() {
vec![2, 3, 4]
);
}
+
+#[test]
+fn test_commit_transaction() {
+ let data_dir = tmp_dir();
+ let mut db = DB::<Inst>::configure()
+ .data_dir(&data_dir)
+ .initialize()
+ .expect("Failed to initialize DB instance");
+
+ db.tx_begin().expect("Failed to begin transaction");
+
+ db.upsert(Inst {
+ id: 0,
+ name: Some("John".to_string()),
+ data: vec![3, 4, 5],
+ })
+ .unwrap();
+
+ db.upsert(Inst {
+ id: 1,
+ name: Some("John".to_string()),
+ data: vec![1, 2, 3],
+ })
+ .unwrap();
+
+ db.tx_commit().expect("Failed to commit transaction");
+
+ let johns = db
+ .find_by(&Field::Name, &Value::String("John".to_string()))
+ .unwrap();
+
+ assert_eq!(johns.len(), 2);
+}
+
+#[test]
+fn test_rollback_transaction() {
+ let data_dir = tmp_dir();
+ let mut db = DB::<Inst>::configure()
+ .data_dir(&data_dir)
+ .initialize()
+ .expect("Failed to initialize DB instance");
+
+ db.tx_begin().expect("Failed to begin transaction");
+
+ db.upsert(Inst {
+ id: 0,
+ name: Some("John".to_string()),
+ data: vec![3, 4, 5],
+ })
+ .unwrap();
+
+ db.upsert(Inst {
+ id: 1,
+ name: Some("John".to_string()),
+ data: vec![1, 2, 3],
+ })
+ .unwrap();
+
+ db.tx_rollback().expect("Failed to rollback transaction");
+
+ let johns = db
+ .find_by(&Field::Name, &Value::String("John".to_string()))
+ .unwrap();
+
+ assert_eq!(johns.len(), 0);
+}