Added the database datatype.

This commit is contained in:
Jeff Baskin 2023-04-10 07:52:53 -04:00
parent da26693242
commit bb23397eb0
1 changed files with 33 additions and 19 deletions

View File

@ -136,18 +136,37 @@ mod stores {
} }
} }
#[derive(Clone, Debug)]
struct Database;
#[cfg(test)]
mod databases {
use super::*;
#[test]
fn create() {
Database::new();
}
}
impl Database {
fn new() -> Self {
Self {}
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
enum DataType { enum DataType {
DBMap(Store), DBMap(Store),
TableMap(Database),
} }
impl DataType { impl DataType {
fn new(dtype: &str) -> Result<DataType, MTTError> { fn new(dtype: &str) -> DataType {
match dtype { match dtype {
"store" => Ok(Self::DBMap(Store::new())), "store" => Self::DBMap(Store::new()),
_ => Err(MTTError::from_code(ErrorCode::IncorrectDataType( "database" => Self::TableMap(Database::new()),
dtype.to_string(), _ => unreachable!(),
))),
} }
} }
} }
@ -157,24 +176,19 @@ mod datatypes {
use super::*; use super::*;
#[test] #[test]
fn error_on_bad_datatype() { fn create_store() {
let items = ["sdgthsth", "jfjty"]; let dtype = DataType::new("store");
for item in items { match dtype {
match DataType::new(item) { DataType::DBMap(_) => (),
Ok(_) => assert!(false, "bad data types should return an error"), _ => assert!(false, "{:?} is not incorrect data type", dtype),
Err(err) => match err.code {
ErrorCode::IncorrectDataType(dtype) => assert_eq!(dtype, item),
_ => assert!(false, "{:?} is not incorrect data type", err.code),
},
}
} }
} }
#[test] #[test]
fn create_store() { fn create_database() {
let dtype = DataType::new("store").unwrap(); let dtype = DataType::new("database");
match dtype { match dtype {
DataType::DBMap(_) => (), DataType::TableMap(_) => (),
_ => assert!(false, "{:?} is not incorrect data type", dtype), _ => assert!(false, "{:?} is not incorrect data type", dtype),
} }
} }
@ -241,7 +255,7 @@ impl Cache {
for id in data.ids { for id in data.ids {
if id == ENTRY { if id == ENTRY {
let mut holder = HashMap::new(); let mut holder = HashMap::new();
holder.insert(ENTRY.to_string(), DataType::new("store").unwrap()); holder.insert(ENTRY.to_string(), DataType::new("store"));
data.reply.send(FromCache::Data(holder)).await.unwrap(); data.reply.send(FromCache::Data(holder)).await.unwrap();
} else { } else {
data.reply data.reply