use crate::db::tests::example_item::{example_items, ExampleItem};
use crate::db::{Hash, Table};
use crate::errors::AppError;
use crate::logging::Logger;
use crate::testing::TempDirectory;
use std::collections::BTreeMap;
use std::marker::PhantomData;
#[tokio::test]
async fn table_end_to_end() -> Result<(), AppError> {
Logger::force_init();
let table = Table::<20, 1, ExampleItem> {
directory: TempDirectory::create("table"),
phantom: PhantomData,
};
let items = example_items();
let expected_count = items.len();
table.set_many(items, true).await?;
let items: BTreeMap<Hash<20>, ExampleItem> = table.get_all().await?;
let actual_count = items.len();
assert_eq!(expected_count, actual_count);
let mut bytes = [0; 20];
bytes[0] = 0xac;
bytes[1] = 0x32;
let hash = Hash::<20>::new(bytes);
let new_item = ExampleItem {
hash,
success: true,
optional: Some("New item".to_owned()),
};
table.set(hash, new_item.clone()).await?;
let items: BTreeMap<Hash<20>, ExampleItem> = table.get_all().await?;
let actual_count = items.len();
assert_eq!(expected_count + 1, actual_count);
Ok(())
}