morethantext-web/src/morethantext/cache.rs

86 lines
2.2 KiB
Rust

use super::{ErrorCode, MTTError, Store, ENTRY};
use async_std::{channel::Receiver, path::PathBuf};
pub struct Cache;
impl Cache {
pub async fn new<P>(_dir: P) -> Self
where
P: Into<PathBuf>,
{
Self {}
}
pub async fn listen(&self, listener: Receiver<String>) {
loop {
listener.recv().await.unwrap();
}
}
pub fn get<S>(&self, id: S) -> Result<Store, MTTError>
where
S: Into<String>,
{
let idd = id.into();
if idd == ENTRY {
Ok(Store::new())
} else {
Err(MTTError::from_code(ErrorCode::IDNotFound(idd)))
}
}
}
#[cfg(test)]
mod engine {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn get_entry() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let store = cache.get(ENTRY).unwrap();
let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected);
}
#[async_std::test]
async fn get_bad_entry() -> Result<(), MTTError> {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let ids = ["bad1", "bad2"];
for id in ids {
match cache.get(id) {
Ok(_) => return Err(MTTError::new("should have errored")),
Err(err) => match err.code {
ErrorCode::IDNotFound(_) => {
assert!(
err.to_string().contains(id),
"Had error: {}, Did not contain: {}",
err.to_string(),
id
);
}
_ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))),
},
}
}
Ok(())
}
}
#[cfg(test)]
mod messages {
use super::*;
use super::super::start_db;
use tempfile::tempdir;
#[async_std::test]
async fn get_the_store() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
in_s.send(ENTRY.to_string()).await.unwrap();
}
}