Completed making the session trait for databases.

This commit is contained in:
Jeff Baskin 2023-02-05 11:29:00 -05:00
parent 51fc2c8921
commit 5b291e3553
2 changed files with 57 additions and 17 deletions

View File

@ -15,12 +15,13 @@ impl Databases {
fn test_key(key: &str) -> Result<(), DBError> { fn test_key(key: &str) -> Result<(), DBError> {
match key { match key {
"name" => (), "database" => (),
_ => return Err(DBError::new(format!("databases do not have a {}", key))), _ => return Err(DBError::new(format!("databases do not have a {}", key))),
} }
Ok(()) Ok(())
} }
/*
fn add_database(&mut self, name: &str, id: &str) -> Result<String, DBError> { fn add_database(&mut self, name: &str, id: &str) -> Result<String, DBError> {
if name.len() == 0 { if name.len() == 0 {
return Err(DBError::new("database names cannot be empty")); return Err(DBError::new("database names cannot be empty"));
@ -41,6 +42,7 @@ impl Databases {
names.sort(); names.sort();
names names
} }
*/
} }
impl FileData<Self> for Databases { impl FileData<Self> for Databases {
@ -73,9 +75,11 @@ impl FileData<Self> for Databases {
} }
if letter == 0 { if letter == 0 {
if get_id { if get_id {
match output match output.add(
.add_database(str::from_utf8(&name).unwrap(), str::from_utf8(&id).unwrap()) "database",
{ str::from_utf8(&name).unwrap(),
str::from_utf8(&id).unwrap(),
) {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
let mut error = DBError::new("file corruption"); let mut error = DBError::new("file corruption");
@ -128,11 +132,20 @@ impl SessionData for Databases {
Ok(output) Ok(output)
} }
fn show(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError> { fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError> {
Ok(Vec::new()) for key in keys {
match Self::test_key(key) {
Ok(_) => (),
Err(err) => return Err(err),
}
}
let mut names: Vec<String> = self.db_map.clone().into_keys().collect();
names.sort();
Ok(names)
} }
} }
/*
#[cfg(test)] #[cfg(test)]
mod functions { mod functions {
use super::*; use super::*;
@ -191,6 +204,7 @@ mod functions {
assert_eq!(output, expected); assert_eq!(output, expected);
} }
} }
*/
#[cfg(test)] #[cfg(test)]
mod file_data { mod file_data {
@ -210,7 +224,7 @@ mod file_data {
let mut dbs = Databases::new(); let mut dbs = Databases::new();
let name = "something"; let name = "something";
let id = "id"; let id = "id";
dbs.add_database(name, id).unwrap(); dbs.add("database", name, id).unwrap();
let mut expected: Vec<u8> = Vec::new(); let mut expected: Vec<u8> = Vec::new();
expected.append(&mut name.as_bytes().to_vec()); expected.append(&mut name.as_bytes().to_vec());
expected.push(0); expected.push(0);
@ -223,9 +237,9 @@ mod file_data {
#[test] #[test]
fn from_bytes() { fn from_bytes() {
let mut dbs = Databases::new(); let mut dbs = Databases::new();
dbs.add_database("one", "1").unwrap(); dbs.add("database", "one", "1").unwrap();
dbs.add_database("two", "2").unwrap(); dbs.add("database", "two", "2").unwrap();
dbs.add_database("three", "3").unwrap(); dbs.add("database", "three", "3").unwrap();
let data = dbs.to_bytes(); let data = dbs.to_bytes();
let mut feed = data.iter(); let mut feed = data.iter();
let output = Databases::from_bytes(&mut feed).unwrap(); let output = Databases::from_bytes(&mut feed).unwrap();
@ -256,7 +270,8 @@ mod file_data {
#[test] #[test]
fn from_bytes_handles_error() { fn from_bytes_handles_error() {
let mut data = "duplicate".as_bytes().to_vec(); let name = "duplicate";
let mut data = name.as_bytes().to_vec();
data.push(0); data.push(0);
data.append(&mut "first".as_bytes().to_vec()); data.append(&mut "first".as_bytes().to_vec());
data.push(0); data.push(0);
@ -273,7 +288,10 @@ mod file_data {
err.source().is_some(), err.source().is_some(),
"Should state file corruption cause." "Should state file corruption cause."
); );
assert_eq!(err.source().unwrap().to_string(), "database already exists") assert_eq!(
err.source().unwrap().to_string(),
format!("database {} already exists", name)
);
} }
} }
} }
@ -286,7 +304,7 @@ mod session_data {
#[test] #[test]
fn add_new() { fn add_new() {
let mut dbs = Databases::new(); let mut dbs = Databases::new();
let key = "name"; let key = "database";
let value = "marvin"; let value = "marvin";
let data = "123456"; let data = "123456";
assert_eq!(dbs.add(key, value, data).unwrap(), [data]); assert_eq!(dbs.add(key, value, data).unwrap(), [data]);
@ -318,8 +336,8 @@ mod session_data {
fn unique_names() { fn unique_names() {
let mut dbs = Databases::new(); let mut dbs = Databases::new();
let value = "wilma"; let value = "wilma";
dbs.add("name", value, "something").unwrap(); dbs.add("database", value, "something").unwrap();
match dbs.add("name", value, "overwrite") { match dbs.add("database", value, "overwrite") {
Ok(_) => assert!(false, "Duplicate names should produce an error."), Ok(_) => assert!(false, "Duplicate names should produce an error."),
Err(err) => assert_eq!( Err(err) => assert_eq!(
err.to_string(), err.to_string(),
@ -331,7 +349,29 @@ mod session_data {
#[test] #[test]
fn get_missing() { fn get_missing() {
let dbs = Databases::new(); let dbs = Databases::new();
let output = dbs.get("name", "melvin").unwrap(); let output = dbs.get("database", "melvin").unwrap();
assert_eq!(output, Vec::<String>::new()); assert_eq!(output, Vec::<String>::new());
} }
#[test]
fn list_bad_keys() {
let dbs = Databases::new();
let key = "sdfgren";
let keys = [key];
match dbs.list(keys.to_vec()) {
Ok(_) => assert!(false, "Bad keys should error."),
Err(err) => assert_eq!(err.to_string(), format!("databases do not have a {}", key)),
}
}
#[test]
fn list_is_sorted() {
let mut dbs = Databases::new();
let mut data = ["fred", "barney", "wilma", "betty", "pebbles", "bambam"];
for db in data {
dbs.add("database", db, db).unwrap();
}
data.sort();
assert_eq!(dbs.list(["database"].to_vec()).unwrap(), data);
}
} }

View File

@ -27,7 +27,7 @@ trait FileData<F> {
trait SessionData { trait SessionData {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError>; fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError>;
fn get(&self, key: &str, value: &str) -> Result<Vec<String>, DBError>; fn get(&self, key: &str, value: &str) -> Result<Vec<String>, DBError>;
fn show(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>; fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
} }
#[derive(Clone)] #[derive(Clone)]