From 947adf6c1d1a48adc579dcc223f743f2aefda5c8 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Tue, 13 Dec 2022 07:19:05 -0500 Subject: [PATCH] Writes out contents of the cache. --- src/morethantext/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 957ac80..48a0f0c 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -1,7 +1,7 @@ pub mod error; use async_std::{ - fs::{create_dir, write}, + fs::{create_dir, read, write}, path::Path, sync::{Arc, Mutex}, }; @@ -69,7 +69,7 @@ impl MoreThanText { async fn add_entry(&self, entry: CacheEntry) -> String { let id: String = thread_rng().sample_iter(&Alphanumeric).take(32).collect(); let file = Path::new(&self.dir).join(&id); - write(file, "a").await.unwrap(); + write(file, entry.to_bytes()).await.unwrap(); let mut cache = self.cache.lock().await; cache.insert(id.clone(), entry); return id; @@ -171,15 +171,17 @@ mod cache { } #[async_std::test] - async fn retrieve_cache() { + async fn store_cache() { let mtt = MTT::new().await; let data = "something"; let expected = CacheEntry::Raw(data.to_string()); - let id = mtt.db.add_entry(expected).await; + let id = mtt.db.add_entry(expected.clone()).await; let output = mtt.db.get_entry(&id).await.unwrap(); assert_eq!(output.to_string(), data); let dfile = mtt.dir.path().join(DATA).join(&id); assert!(dfile.is_file(), "Cache file should exist."); + let content = read(dfile).await.unwrap(); + assert_eq!(content, expected.to_bytes()); } #[async_std::test]