Completed making the session trait for databases.
This commit is contained in:
parent
51fc2c8921
commit
5b291e3553
@ -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<String, DBError> {
|
||||
if name.len() == 0 {
|
||||
return Err(DBError::new("database names cannot be empty"));
|
||||
@ -41,6 +42,7 @@ impl Databases {
|
||||
names.sort();
|
||||
names
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl FileData<Self> for Databases {
|
||||
@ -73,9 +75,11 @@ impl FileData<Self> 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<Vec<String>, DBError> {
|
||||
Ok(Vec::new())
|
||||
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError> {
|
||||
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)]
|
||||
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<u8> = 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::<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);
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ trait FileData<F> {
|
||||
trait SessionData {
|
||||
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 show(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
|
||||
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
Loading…
Reference in New Issue
Block a user