From 14c6c9dffed24c620081055e0743cd12327ad9ab Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 3 Dec 2022 10:22:54 -0500 Subject: [PATCH] Refactored test database setup. --- src/morethantext/mod.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index be947db..1809904 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -33,6 +33,27 @@ impl MoreThanText { } } +#[cfg(test)] +mod setup { + use super::*; + use tempfile::{tempdir, TempDir}; + + pub struct MTT { + pub db: MoreThanText, + pub dir: TempDir, + } + + impl MTT { + pub async fn new() -> Self { + let dir = tempdir().unwrap(); + let db = MoreThanText::new(dir.path().to_str().unwrap()) + .await + .unwrap(); + Self { db: db, dir: dir } + } + } +} + #[cfg(test)] mod init { use super::*; @@ -76,15 +97,12 @@ mod init { #[cfg(test)] mod cache { use super::*; - use tempfile::tempdir; + use setup::MTT; #[async_std::test] - async fn add_entry() { - let dir = tempdir().unwrap(); - let db = MoreThanText::new(dir.path().to_str().unwrap()).await.unwrap(); + async fn add_entry() { + let mtt = MTT::new().await; let data = CacheEntry::Raw("something".to_string()); - db.add_entry(data).await; - drop(db); - dir.close().unwrap(); + mtt.db.add_entry(data).await; } }