mirror of
https://github.com/creating2morrow/neveko.git
synced 2024-12-22 11:39:22 +00:00
add tests for db.rs
This commit is contained in:
parent
b9b71c63ff
commit
2f0d0c8c45
2 changed files with 53 additions and 2 deletions
2
.github/workflows/rust.yml
vendored
2
.github/workflows/rust.yml
vendored
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue