aboutsummaryrefslogtreecommitdiff
path: root/src/db/test.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-06-02 14:59:26 +0200
committerAlex Auvolat <alex@adnab.me>2022-06-02 14:59:26 +0200
commitfd8d5c37f73d93a0cdfd8ab08f7447b0f6d3ce3e (patch)
tree5c3e486be1efebb588f4095d10e5191d79078fd5 /src/db/test.rs
parentb54a938724e5551f6436f551cafec3d1324a6260 (diff)
downloadgarage-fd8d5c37f73d93a0cdfd8ab08f7447b0f6d3ce3e.tar.gz
garage-fd8d5c37f73d93a0cdfd8ab08f7447b0f6d3ce3e.zip
First iteration of a generic DB layer
Diffstat (limited to 'src/db/test.rs')
-rw-r--r--src/db/test.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/db/test.rs b/src/db/test.rs
new file mode 100644
index 00000000..f0e6c5de
--- /dev/null
+++ b/src/db/test.rs
@@ -0,0 +1,49 @@
+use crate::*;
+
+use crate::sled_adapter::SledDb;
+
+fn test_suite(db: Db) -> Result<()> {
+ let tree = db.tree("tree")?;
+
+ let va: &[u8] = &b"plop"[..];
+ let vb: &[u8] = &b"plip"[..];
+ let vc: &[u8] = &b"plup"[..];
+
+ tree.put(b"test", va)?;
+ assert_eq!(tree.get(b"test")?, Some(va.into()));
+
+ let res = db.transaction::<_, (), _>(|tx| {
+ assert_eq!(tx.get(&tree, b"test")?, Some(va.into()));
+
+ tx.put(&tree, b"test", vb)?;
+
+ assert_eq!(tx.get(&tree, b"test")?, Some(vb.into()));
+
+ tx.commit(12)
+ });
+ assert!(matches!(res, Ok(12)));
+ assert_eq!(tree.get(b"test")?, Some(vb.into()));
+
+ let res = db.transaction::<(), _, _>(|tx| {
+ assert_eq!(tx.get(&tree, b"test")?, Some(vb.into()));
+
+ tx.put(&tree, b"test", vc)?;
+
+ assert_eq!(tx.get(&tree, b"test")?, Some(vc.into()));
+
+ tx.abort(42)
+ });
+ assert!(matches!(res, Err(TxError::Abort(42))));
+ assert_eq!(tree.get(b"test")?, Some(vb.into()));
+
+ Ok(())
+}
+
+#[test]
+fn test_sled_db() -> Result<()> {
+ let path = mktemp::Temp::new_dir().unwrap();
+ let db = SledDb::new(sled::open(path.to_path_buf()).unwrap());
+ test_suite(db)?;
+ drop(path);
+ Ok(())
+}