Continued work to get items into a session.

This commit is contained in:
Jeff Baskin 2023-02-04 23:06:29 -05:00
parent 60dae69788
commit 51fc2c8921

View File

@ -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::<String>::new());
}
}