Added error for missing cache entry.

This commit is contained in:
Jeff Baskin 2022-12-06 11:42:11 -05:00
parent bdcee41dcf
commit d73a18be2d
1 changed files with 15 additions and 3 deletions

View File

@ -54,9 +54,12 @@ impl MoreThanText {
return id;
}
async fn get_entry(&self, id: &str) -> CacheEntry {
async fn get_entry(&self, id: &str) -> Result<CacheEntry, DBError> {
let cache = self.cache.lock().await;
cache.get(id).unwrap().clone()
match cache.get(id) {
Some(id) => Ok(id.clone()),
None => Err(DBError::new("cache entry not found")),
}
}
}
@ -142,7 +145,16 @@ mod cache {
let data = "something";
let expected = CacheEntry::Raw(data.to_string());
let id = mtt.db.add_entry(expected).await;
let output = mtt.db.get_entry(&id).await;
let output = mtt.db.get_entry(&id).await.unwrap();
assert_eq!(output.to_string(), data);
}
#[async_std::test]
async fn retrieve_bad_id() {
let mtt = MTT::new().await;
match mtt.db.get_entry(&"Not Valid").await {
Ok(_) => assert!(false, "Should have raised an error."),
Err(err) => assert_eq!(err.to_string(), "cache entry not found"),
}
}
}