use crate::utils::{db_put_simple, open_database, tmpdir};
use leveldb::database::Slice;
use leveldb::database::kv::KV;
use leveldb::options::{ReadOptions, WriteOptions};

#[test]
fn test_write_to_database() {
    let tmp = tmpdir("write");
    let database = open_database(tmp.path(), true);
    let write_opts = WriteOptions::new();
    let k = Slice::from("1".as_bytes());
    let v = Slice::from("1".as_bytes());
    let result = database.put(write_opts, k, v);
    assert!(result.is_ok());
}

#[test]
fn test_delete_from_database() {
    let tmp = tmpdir("delete_simple");
    let database = &mut open_database(tmp.path(), true);
    db_put_simple(database, &[1], &[1]);

    let write2 = WriteOptions::new();
    let k = Slice::from("1".as_bytes());
    let res2 = database.delete(write2, k);
    assert!(res2.is_ok());
}

#[test]
fn test_get_from_empty_database() {
    let tmp = tmpdir("get_simple");
    let database = &mut open_database(tmp.path(), true);
    let read_opts = ReadOptions::new();
    let k = Slice::from("1".as_bytes());
    let res = database.get(read_opts, k);
    assert!(res.unwrap().is_none())
}

#[test]
fn test_get_from_filled_database() {
    let tmp = tmpdir("get_filled");
    let database = &mut open_database(tmp.path(), true);
    db_put_simple(database, b"1", b"1");

    let read_opts = ReadOptions::new();
    let k = Slice::from("1".as_bytes());
    let res = database.get(read_opts, k);
    assert_eq!(res.unwrap().unwrap().as_bytes(), b"1");
}
