Continuing channel setup.
This commit is contained in:
parent
30ea8d978c
commit
dd1c45ddbe
@ -1,3 +1,4 @@
|
||||
use super::{ErrorCode, MTTError, Store, ENTRY};
|
||||
use async_std::{channel::Receiver, path::PathBuf};
|
||||
|
||||
pub struct Cache;
|
||||
@ -15,6 +16,18 @@ impl Cache {
|
||||
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)]
|
||||
@ -23,8 +36,50 @@ mod engine {
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[async_std::test]
|
||||
async fn create() {
|
||||
async fn get_entry() {
|
||||
let dir = tempdir().unwrap();
|
||||
Cache::new(dir.path()).await;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,15 @@ use std::{error::Error, fmt};
|
||||
pub enum ErrorCode {
|
||||
// General
|
||||
Undefined(String),
|
||||
// Cache
|
||||
IDNotFound(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
|
||||
ErrorCode::IDNotFound(id) => write!(f, "ID '{}' not found", id),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -26,15 +29,23 @@ mod errorcodes {
|
||||
assert_eq!(err.to_string(), item);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_missing() {
|
||||
for item in ITEMS {
|
||||
let err = ErrorCode::IDNotFound(item.to_string());
|
||||
assert_eq!(err.to_string(), format!("ID '{}' not found", item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MTTError {
|
||||
code: ErrorCode,
|
||||
pub code: ErrorCode,
|
||||
}
|
||||
|
||||
impl MTTError {
|
||||
fn new<S>(msg: S) -> Self
|
||||
pub fn new<S>(msg: S) -> Self
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
@ -44,7 +55,7 @@ impl MTTError {
|
||||
}
|
||||
}
|
||||
|
||||
fn from_code(code: ErrorCode) -> Self {
|
||||
pub fn from_code(code: ErrorCode) -> Self {
|
||||
Self { code: code }
|
||||
}
|
||||
}
|
||||
@ -86,4 +97,14 @@ mod errors {
|
||||
_ => assert!(false, "{:?} is not undefined", err.code),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_missing_id_from_code() {
|
||||
let code = ErrorCode::IDNotFound("123".to_string());
|
||||
let err = MTTError::from_code(code);
|
||||
match err.code {
|
||||
ErrorCode::IDNotFound(_) => (),
|
||||
_ => assert!(false, "{:?} is not undefined", err.code),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user