From 5b291e35534e64e44a0613eadf051ba0dd52aa68 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 5 Feb 2023 11:29:00 -0500 Subject: [PATCH] Completed making the session trait for databases. --- src/morethantext/databases.rs | 72 +++++++++++++++++++++++++++-------- src/morethantext/mod.rs | 2 +- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/morethantext/databases.rs b/src/morethantext/databases.rs index cdd0a8c..57f327c 100644 --- a/src/morethantext/databases.rs +++ b/src/morethantext/databases.rs @@ -15,12 +15,13 @@ impl Databases { fn test_key(key: &str) -> Result<(), DBError> { match key { - "name" => (), + "database" => (), _ => return Err(DBError::new(format!("databases do not have a {}", key))), } Ok(()) } + /* fn add_database(&mut self, name: &str, id: &str) -> Result { if name.len() == 0 { return Err(DBError::new("database names cannot be empty")); @@ -41,6 +42,7 @@ impl Databases { names.sort(); names } + */ } impl FileData for Databases { @@ -73,9 +75,11 @@ impl FileData for Databases { } if letter == 0 { if get_id { - match output - .add_database(str::from_utf8(&name).unwrap(), str::from_utf8(&id).unwrap()) - { + match output.add( + "database", + str::from_utf8(&name).unwrap(), + str::from_utf8(&id).unwrap(), + ) { Ok(_) => (), Err(err) => { let mut error = DBError::new("file corruption"); @@ -128,11 +132,20 @@ impl SessionData for Databases { Ok(output) } - fn show(&self, keys: Vec<&str>) -> Result, DBError> { - Ok(Vec::new()) + fn list(&self, keys: Vec<&str>) -> Result, DBError> { + for key in keys { + match Self::test_key(key) { + Ok(_) => (), + Err(err) => return Err(err), + } + } + let mut names: Vec = self.db_map.clone().into_keys().collect(); + names.sort(); + Ok(names) } } +/* #[cfg(test)] mod functions { use super::*; @@ -191,6 +204,7 @@ mod functions { assert_eq!(output, expected); } } +*/ #[cfg(test)] mod file_data { @@ -210,7 +224,7 @@ mod file_data { let mut dbs = Databases::new(); let name = "something"; let id = "id"; - dbs.add_database(name, id).unwrap(); + dbs.add("database", name, id).unwrap(); let mut expected: Vec = Vec::new(); expected.append(&mut name.as_bytes().to_vec()); expected.push(0); @@ -223,9 +237,9 @@ mod file_data { #[test] fn from_bytes() { let mut dbs = Databases::new(); - dbs.add_database("one", "1").unwrap(); - dbs.add_database("two", "2").unwrap(); - dbs.add_database("three", "3").unwrap(); + dbs.add("database", "one", "1").unwrap(); + dbs.add("database", "two", "2").unwrap(); + dbs.add("database", "three", "3").unwrap(); let data = dbs.to_bytes(); let mut feed = data.iter(); let output = Databases::from_bytes(&mut feed).unwrap(); @@ -256,7 +270,8 @@ mod file_data { #[test] 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.append(&mut "first".as_bytes().to_vec()); data.push(0); @@ -273,7 +288,10 @@ mod file_data { err.source().is_some(), "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] fn add_new() { let mut dbs = Databases::new(); - let key = "name"; + let key = "database"; let value = "marvin"; let data = "123456"; assert_eq!(dbs.add(key, value, data).unwrap(), [data]); @@ -318,8 +336,8 @@ mod session_data { fn unique_names() { let mut dbs = Databases::new(); let value = "wilma"; - dbs.add("name", value, "something").unwrap(); - match dbs.add("name", value, "overwrite") { + dbs.add("database", value, "something").unwrap(); + match dbs.add("database", value, "overwrite") { Ok(_) => assert!(false, "Duplicate names should produce an error."), Err(err) => assert_eq!( err.to_string(), @@ -331,7 +349,29 @@ mod session_data { #[test] fn get_missing() { let dbs = Databases::new(); - let output = dbs.get("name", "melvin").unwrap(); + let output = dbs.get("database", "melvin").unwrap(); assert_eq!(output, Vec::::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); + } } diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 4206263..30c11c7 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -27,7 +27,7 @@ trait FileData { trait SessionData { fn add(&mut self, key: &str, value: &str, data: &str) -> Result, DBError>; fn get(&self, key: &str, value: &str) -> Result, DBError>; - fn show(&self, keys: Vec<&str>) -> Result, DBError>; + fn list(&self, keys: Vec<&str>) -> Result, DBError>; } #[derive(Clone)]