diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 5281288..38089a3 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -61,6 +61,16 @@ impl MoreThanText { None => Err(DBError::new("cache entry not found")), } } + + async fn update_entry(&self, id: &str, entry: CacheEntry) -> Result<(), DBError> { + match self.get_entry(id).await { + Ok(_) => (), + Err(err) => return Err(err), + } + let mut cache = self.cache.lock().await; + cache.insert(id.to_string(), entry); + Ok(()) + } } #[cfg(test)] @@ -157,4 +167,23 @@ mod cache { Err(err) => assert_eq!(err.to_string(), "cache entry not found"), } } + + #[async_std::test] + async fn update_cache_entry() { + let mtt = MTT::new().await; + let id = mtt.db.add_entry(CacheEntry::Raw("same".to_string())).await; + let expected = "different"; + mtt.db.update_entry(&id, CacheEntry::Raw(expected.to_string())).await.unwrap(); + let output = mtt.db.get_entry(&id).await.unwrap(); + assert_eq!(output.to_string(), expected); + } + + #[async_std::test] + async fn update_bad_id() { + let mtt = MTT::new().await; + match mtt.db.update_entry("wilma", CacheEntry::Raw("wrong".to_string())).await { + Ok(_) => assert!(false, "Bad id should raise an error."), + Err(err) => assert_eq!(err.to_string(), "cache entry not found"), + } + } }