Protect against duplicate databases.

This commit is contained in:
Jeff Baskin 2023-06-24 10:48:35 -04:00
parent d90dc3b9fc
commit c26089caed
2 changed files with 56 additions and 7 deletions

View File

@ -6,6 +6,8 @@ pub enum ErrorCode {
Undefined(String), Undefined(String),
// Cache // Cache
IDNotFound(String), IDNotFound(String),
// Store
DuplicateDatabase(String),
} }
impl fmt::Display for ErrorCode { impl fmt::Display for ErrorCode {
@ -13,6 +15,7 @@ impl fmt::Display for ErrorCode {
match self { match self {
ErrorCode::Undefined(msg) => write!(f, "{}", msg), ErrorCode::Undefined(msg) => write!(f, "{}", msg),
ErrorCode::IDNotFound(id) => write!(f, "ID '{}' not found", id), ErrorCode::IDNotFound(id) => write!(f, "ID '{}' not found", id),
ErrorCode::DuplicateDatabase(name) => write!(f, "database '{}' already exists", name),
} }
} }
} }
@ -37,6 +40,17 @@ mod errorcodes {
assert_eq!(err.to_string(), format!("ID '{}' not found", item)); assert_eq!(err.to_string(), format!("ID '{}' not found", item));
} }
} }
#[test]
fn duplicate_database() {
for item in ITEMS {
let err = ErrorCode::DuplicateDatabase(item.to_string());
assert_eq!(
err.to_string(),
format!("database '{}' already exists", item)
);
}
}
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,4 +1,4 @@
use super::{Data, Database}; use super::{Data, Database, ErrorCode, MTTError};
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -13,9 +13,18 @@ impl Store {
} }
} }
pub fn add(&mut self, name: &str) { pub fn add<S>(&mut self, name: S) -> Result<(), MTTError>
let storage = Data::from_data(Database::new()); where
self.data.insert(name.to_string(), storage); S: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateDatabase(db_name))),
None => {
self.data.insert(db_name, Data::from_data(Database::new()));
Ok(())
}
}
} }
pub fn get(&self, name: &str) -> Option<&Data<Database>> { pub fn get(&self, name: &str) -> Option<&Data<Database>> {
@ -29,7 +38,7 @@ impl Store {
#[cfg(test)] #[cfg(test)]
mod storage { mod storage {
use super::{super::MTTError, *}; use super::*;
#[test] #[test]
fn create_new() { fn create_new() {
@ -39,14 +48,40 @@ mod storage {
} }
#[test] #[test]
fn add_database() { fn add_db_by_str() {
let mut store = Store::new(); let mut store = Store::new();
let name = "Melvin"; let name = "Melvin";
store.add(name); store.add(name).unwrap();
let output = store.get(name); let output = store.get(name);
assert!(output.is_some(), "Get returned none."); assert!(output.is_some(), "Get returned none.");
} }
#[test]
fn add_db_by_string() {
let mut store = Store::new();
let name = "Marvin";
store.add(name.to_string()).unwrap();
let output = store.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn fail_on_duplicates() -> Result<(), MTTError> {
let mut store = Store::new();
let name = "Mickie";
store.add(name).unwrap();
match store.add(name) {
Ok(_) => Err(MTTError::new("duplicates should error")),
Err(err) => match err.code {
ErrorCode::DuplicateDatabase(db_name) => {
assert_eq!(db_name, name);
Ok(())
}
_ => Err(MTTError::new(format!("{:?} is not DuplicateDatabase", err))),
},
}
}
#[test] #[test]
fn get_bad_database() -> Result<(), MTTError> { fn get_bad_database() -> Result<(), MTTError> {
let store = Store::new(); let store = Store::new();