Added initial from bytes to databases.

This commit is contained in:
Jeff Baskin 2023-01-16 09:31:13 -05:00
parent 43036bec84
commit fa9cbe6a25

View File

@ -29,7 +29,7 @@ impl CacheType {
output.push(0); output.push(0);
match self { match self {
CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()), CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()),
CacheType::DBMap(_) => todo!(), CacheType::DBMap(_) => (),
} }
return output; return output;
} }
@ -58,6 +58,7 @@ impl CacheType {
} }
Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string())) Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string()))
} }
"DBMap" => Ok(CacheType::DBMap(Databases::new())),
_ => Err(DBError::new("data corruption")), _ => Err(DBError::new("data corruption")),
} }
} }
@ -77,51 +78,15 @@ mod databases {
use super::*; use super::*;
#[test] #[test]
fn get_databases() { fn get_bytes_for_new() {
Databases::new(); Databases::new();
} }
} }
#[cfg(test)]
mod raw {
use super::*;
#[test]
fn get_bytes() {
let data = "addams";
let holder = CacheType::Raw(data.to_string());
let mut expected = holder.entry_type().into_bytes();
expected.push(0);
expected.append(&mut data.as_bytes().to_vec());
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_bytes() {
let holder = CacheType::Raw("stored item".to_string());
let data = holder.to_bytes();
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.to_string(), holder.to_string());
}
}
#[cfg(test)] #[cfg(test)]
mod enum_ctype { mod enum_ctype {
use super::*; use super::*;
#[test]
fn get_raw_type() {
let holder = CacheType::Raw("nothing important".to_string());
assert_eq!(holder.entry_type(), "Raw");
}
#[test]
fn get_dbmap_type() {
let holder = CacheType::DBMap(Databases::new());
assert_eq!(holder.entry_type(), "DBMap");
}
#[test] #[test]
fn bad_file_header() { fn bad_file_header() {
let mut data: Vec<u8> = Vec::new(); let mut data: Vec<u8> = Vec::new();
@ -155,4 +120,52 @@ mod enum_ctype {
Err(err) => assert_eq!(err.to_string(), "empty file"), Err(err) => assert_eq!(err.to_string(), "empty file"),
} }
} }
#[test]
fn get_raw_type() {
let holder = CacheType::Raw("nothing important".to_string());
assert_eq!(holder.entry_type(), "Raw");
}
#[test]
fn get_raw_bytes() {
let data = "addams";
let holder = CacheType::Raw(data.to_string());
let mut expected = holder.entry_type().into_bytes();
expected.push(0);
expected.append(&mut data.as_bytes().to_vec());
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_raw_bytes() {
let holder = CacheType::Raw("stored item".to_string());
let data = holder.to_bytes();
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.to_string(), holder.to_string());
}
#[test]
fn get_dbmap_type() {
let holder = CacheType::DBMap(Databases::new());
assert_eq!(holder.entry_type(), "DBMap");
}
#[test]
fn get_new_databases_bytes() {
let holder = CacheType::DBMap(Databases::new());
let mut expected = "DBMap".as_bytes().to_vec();
expected.push(0);
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_new_databases_bytes() {
let mut data = "DBMap".as_bytes().to_vec();
data.push(0);
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.entry_type(), "DBMap");
}
} }