From 51fc2c89219a454a008223f74b707b7189aebe69 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 4 Feb 2023 23:06:29 -0500 Subject: [PATCH] Continued work to get items into a session. --- src/morethantext/databases.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/morethantext/databases.rs b/src/morethantext/databases.rs index 1f4f722..cdd0a8c 100644 --- a/src/morethantext/databases.rs +++ b/src/morethantext/databases.rs @@ -105,6 +105,10 @@ impl SessionData for Databases { Ok(_) => (), Err(err) => return Err(err), } + match self.db_map.get(value) { + Some(_) => return Err(DBError::new(format!("database {} already exists", value))), + None => (), + } self.db_map.insert(value.to_string(), data.to_string()); let mut output = Vec::new(); output.push(data.to_string()); @@ -117,8 +121,10 @@ impl SessionData for Databases { Err(err) => return Err(err), } let mut output = Vec::new(); - let data = self.db_map.get(value).unwrap(); - output.push(data.to_string()); + match self.db_map.get(value) { + Some(data) => output.push(data.to_string()), + None => (), + } Ok(output) } @@ -307,4 +313,25 @@ mod session_data { Err(_) => (), } } + + #[test] + fn unique_names() { + let mut dbs = Databases::new(); + let value = "wilma"; + dbs.add("name", value, "something").unwrap(); + match dbs.add("name", value, "overwrite") { + Ok(_) => assert!(false, "Duplicate names should produce an error."), + Err(err) => assert_eq!( + err.to_string(), + format!("database {} already exists", value) + ), + } + } + + #[test] + fn get_missing() { + let dbs = Databases::new(); + let output = dbs.get("name", "melvin").unwrap(); + assert_eq!(output, Vec::::new()); + } }