add tests for db.rs

This commit is contained in:
creating2morrow 2023-05-17 10:18:22 -04:00
parent b9b71c63ff
commit 2f0d0c8c45
2 changed files with 53 additions and 2 deletions

View file

@ -25,4 +25,4 @@ jobs:
- name: Build - name: Build
run: cargo build --verbose run: cargo build --verbose
- name: Run tests - name: Run tests
run: cargo test --verbose run: cd nevmes-core && cargo test --verbose

View file

@ -14,12 +14,18 @@ use log::{
use crate::utils; use crate::utils;
/// LMDB Interface allows access to the env
///
/// and handle for the write, read and delete
///
/// functionality.
pub struct Interface { pub struct Interface {
pub env: Environment, pub env: Environment,
pub handle: DbHandle, pub handle: DbHandle,
} }
impl Interface { impl Interface {
/// Instantiation of ```Environment``` and ```DbHandle```
pub fn open() -> Self { pub fn open() -> Self {
let release_env = utils::get_release_env(); let release_env = utils::get_release_env();
let file_path = format!( let file_path = format!(
@ -32,10 +38,13 @@ impl Interface {
}; };
let env = EnvBuilder::new() let env = EnvBuilder::new()
.open(format!("{}/{}", file_path, env_str), 0o777) .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(); let handle = env.get_default_db(DbFlags::empty()).unwrap();
Interface { env, handle } 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) { pub fn write(e: &Environment, h: &DbHandle, k: &str, v: &str) {
let txn = e.new_transaction().unwrap(); let txn = e.new_transaction().unwrap();
{ {
@ -51,6 +60,11 @@ impl Interface {
Ok(_) => (), 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 { pub fn read(e: &Environment, h: &DbHandle, k: &str) -> String {
let reader = e.get_reader().unwrap(); let reader = e.get_reader().unwrap();
let db = reader.bind(&h); let db = reader.bind(&h);
@ -63,6 +77,11 @@ impl Interface {
} }
r 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) { pub fn delete(e: &Environment, h: &DbHandle, k: &str) {
let txn = e.new_transaction().unwrap(); 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);
}
}