From 07baa7bbec2b13fb9459671e7bd9089aa31b007b Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 7 Aug 2022 09:29:08 -0400 Subject: [PATCH] Can retrieve a table now. --- src/morethantext/mod.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index eca7685..cb335a4 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -25,13 +25,23 @@ impl MoreThanText { match tables.get(&name) { Some(_) => Err(MTTError::new(format!("table {} already exists", name))), None => { - tables.insert(name, Table::new().await); - Ok(Table::new().await) + let table = Table::new().await; + tables.insert(name, table.clone()); + Ok(table) } } } + + pub async fn get_table(&self, name: &str) -> Option { + let tables = self.tables.read().await; + match tables.get(name) { + Some(tbl) => Some(tbl.clone()), + None => None, + } + } } +#[derive(Clone)] pub struct Table; impl Table { @@ -77,6 +87,22 @@ mod database { } } } + + #[async_std::test] + async fn get_non_existant_table() { + let db = MoreThanText::new().await; + let table = db.get_table("missing").await; + assert!(table.is_none(), "There should be no table."); + } + + #[async_std::test] + async fn get_a_table() { + let db = MoreThanText::new().await; + let name = "here"; + db.new_table(name).await.unwrap(); + let table = db.get_table(name).await; + assert!(table.is_some(), "Table should be found."); + } } #[cfg(test)]