From fa9cbe6a25d8a83cc28839d61342c4d0c237cd98 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Mon, 16 Jan 2023 09:31:13 -0500 Subject: [PATCH] Added initial from bytes to databases. --- src/morethantext/cachetype.rs | 89 ++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/src/morethantext/cachetype.rs b/src/morethantext/cachetype.rs index 1229571..a11b084 100644 --- a/src/morethantext/cachetype.rs +++ b/src/morethantext/cachetype.rs @@ -29,7 +29,7 @@ impl CacheType { output.push(0); match self { CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()), - CacheType::DBMap(_) => todo!(), + CacheType::DBMap(_) => (), } return output; } @@ -58,6 +58,7 @@ impl CacheType { } Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string())) } + "DBMap" => Ok(CacheType::DBMap(Databases::new())), _ => Err(DBError::new("data corruption")), } } @@ -77,51 +78,15 @@ mod databases { use super::*; #[test] - fn get_databases() { + fn get_bytes_for_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)] mod enum_ctype { 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] fn bad_file_header() { let mut data: Vec = Vec::new(); @@ -155,4 +120,52 @@ mod enum_ctype { 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"); + } }