From 2f0d0c8c4519c7478b6046fef9239806a2dd4ff5 Mon Sep 17 00:00:00 2001 From: creating2morrow Date: Wed, 17 May 2023 10:18:22 -0400 Subject: [PATCH] add tests for db.rs --- .github/workflows/rust.yml | 2 +- nevmes-core/src/db.rs | 53 +++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b095543..e8fd7e9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -25,4 +25,4 @@ jobs: - name: Build run: cargo build --verbose - name: Run tests - run: cargo test --verbose + run: cd nevmes-core && cargo test --verbose diff --git a/nevmes-core/src/db.rs b/nevmes-core/src/db.rs index 4fb1224..573498e 100644 --- a/nevmes-core/src/db.rs +++ b/nevmes-core/src/db.rs @@ -14,12 +14,18 @@ use log::{ use crate::utils; +/// LMDB Interface allows access to the env +/// +/// and handle for the write, read and delete +/// +/// functionality. pub struct Interface { pub env: Environment, pub handle: DbHandle, } impl Interface { + /// Instantiation of ```Environment``` and ```DbHandle``` pub fn open() -> Self { let release_env = utils::get_release_env(); let file_path = format!( @@ -32,10 +38,13 @@ impl Interface { }; let env = EnvBuilder::new() .open(format!("{}/{}", file_path, env_str), 0o777) - .unwrap(); + .expect(&format!("could not open LMDB at {}", file_path)); let handle = env.get_default_db(DbFlags::empty()).unwrap(); Interface { env, handle } } + /// Write a key-value to LMDB. NEVMES does not currently support + /// + /// writing multiple key value pairs. pub fn write(e: &Environment, h: &DbHandle, k: &str, v: &str) { let txn = e.new_transaction().unwrap(); { @@ -51,6 +60,11 @@ impl Interface { Ok(_) => (), } } + /// Read a value from LMDB by passing the key as a static + /// + /// string. If the value does not exist an empty string is + /// + /// returned. NEVMES does not currently support duplicate keys. pub fn read(e: &Environment, h: &DbHandle, k: &str) -> String { let reader = e.get_reader().unwrap(); let db = reader.bind(&h); @@ -63,6 +77,11 @@ impl Interface { } r } + /// Delete a value from LMDB by passing the key as a + /// + /// static string. If the value does not exist then an + /// + /// error will be logged. pub fn delete(e: &Environment, h: &DbHandle, k: &str) { let txn = e.new_transaction().unwrap(); { @@ -76,3 +95,35 @@ impl Interface { } } } + +// Tests +//------------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn write_and_read_test() { + let s = Interface::open(); + let k = "test-key"; + let v = "test-value"; + Interface::write(&s.env, &s.handle, k, v); + let expected = String::from(v); + let actual = Interface::read(&s.env, &s.handle, k); + assert_eq!(expected, actual); + Interface::delete(&s.env, &s.handle, &k); + } + + #[test] + fn write_and_delete_test() { + let s = Interface::open(); + let k = "test-key"; + let v = "test-value"; + Interface::write(&s.env, &s.handle, k, v); + let expected = utils::empty_string(); + Interface::delete(&s.env, &s.handle, &k); + let actual = Interface::read(&s.env, &s.handle, k); + assert_eq!(expected, actual); + } +}