Added cache update function.
This commit is contained in:
		@@ -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"),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user