mycelium_core 0.1.1

Library for Mycelium DDM
Documentation
use super::*;
use rayon::prelude::*;

#[derive(Serialize, Deserialize)]
struct TestItem {
    field1: String,
    field2: i32,
    field3: i64,
    field4: u32,
    field5: usize,
    field6: f32,
    field7: std::ops::Range<i32>,
}

#[test]
fn empty_test() {
    let db = Db::new().expect("Failed to create db");
    let db_emtpy = db.empty();

    assert!(db.working_dir == db_emtpy.working_dir);
}

#[test]
fn get_item_test() {
    let stew = Db::init(None).expect("Failed to init stew in get_item_test()");
    let id = stew
        .add("Test get_item_test()".as_bytes(), "get_item_test")
        .expect("Failed to add item in get_item_test()");
    let item = stew
        .get("get_item_test", id)
        .expect("Failed to find item in get_item_test()");
    if item.get_item().as_slice() != "Test get_item_test()".as_bytes() {
        assert!(false, "Item returned was incorrect in get_item_test()");
    }
    assert!(true)
}

#[test]
fn list_tags_test() {
    let stew = Db::init(None).expect("Failed to init stew in list_tags_test()");

    match stew.add("some bytes".as_bytes(), "list_tags_test") {
        Ok(_) => (),
        Err(e) => (assert!(false, format!("Failure list_tags_test(): {:?}", e))),
    }

    let tags = stew.list_tags();
    if tags.len() == 1 {
        let test = tags.first().unwrap();
        if test != "list_tags_test" {
            assert!(false, "wrong tag in list_tags_test.")
        }
    } else {
        assert!(false, "Wrong number of tags.")
    }

    assert!(true)
}

#[test]
#[should_panic]
fn refresh_test() {
    let stew = Db::init(None).expect("Failed to init stew in refresh_test()");
    let item_id = stew
        .add("Refresh Test Item".as_bytes(), "refresh_test")
        .expect("Failed to add item for refresh_test");
    match stew.refresh("refresh_test") {
        Ok(_) => (),
        Err(e) => assert!(false, format!("Failure refresh_test(): {:?}", e)),
    }
    let _item = stew
        .get("refresh_test", item_id)
        .expect("Should panic to pass.");
}

#[test]
fn insert_1k_concurrent_mcore() {
    let tag = "insert_10k_concurrent";
    let db = Db::init(None).expect("Failed to init stew in refresh_test()");
    let mut values = vec![];
    for i in 0..1000 {
        values.push(format!("a duck in a pickup: {}", i).as_bytes().to_vec());
    }

    let ids: Vec<[u8; 16]> = values
        .par_iter()
        .map(|x| {
            let result = db.add(x.as_slice(), tag);
            let id = match result {
                Ok(id) => Some(id),
                Err(_) => None,
            };
            id
        })
        .filter_map(|opt_id| opt_id)
        .collect();

    let items = db.get_tag(tag).unwrap();
    for item in items {
        assert!(ids.contains(&item.get_id()));
    }
}