Corrected Record::new().
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit

This commit is contained in:
Jeff Baskin 2024-12-08 07:40:33 -05:00
parent a26471088d
commit a86cbacf7b
3 changed files with 98 additions and 26 deletions

View File

@ -25,8 +25,18 @@ pub struct Record {
} }
impl Record { impl Record {
pub fn new(data: HashMap<String, Field>) -> Self { pub fn new() -> Self {
Self { data: data } Self {
data: HashMap::new(),
}
}
pub fn add<S, D>(&mut self, name: S, data: D) -> Option<Field>
where
String: From<S>,
Field: From<D>,
{
self.data.insert(name.into(), data.into())
} }
} }
@ -56,21 +66,49 @@ mod records {
#[test] #[test]
fn new_record() { fn new_record() {
let data: HashMap<String, Field> = HashMap::new(); let rec = Record::new();
let rec = Record::new(data);
assert_eq!(rec.len(), 0); assert_eq!(rec.len(), 0);
} }
#[test]
fn add_field() {
let mut rec = Record::new();
let name = "field_name";
let data = ID::random();
rec.add(name.to_string(), data);
match rec.get(name) {
Some(data) => match data {
Field::ID(result) => assert_eq!(result.to_string(), data.to_string()),
},
None => unreachable!("Should get data back"),
}
}
#[test]
fn add_alternate_field() {
let mut rec = Record::new();
let name = "alternate";
let data = ID::random();
rec.add(name, data.clone());
match rec.get(name) {
Some(data) => match data {
Field::ID(result) => assert_eq!(result.to_string(), data.to_string()),
},
None => unreachable!("Should get data back"),
}
}
#[test] #[test]
fn record_iter() { fn record_iter() {
let a = Field::from(ID::random()); let mut data: HashMap<String, ID> = HashMap::new();
let b = Field::from(ID::random()); data.insert("a".to_string(), ID::random());
let c = Field::from(ID::random()); data.insert("b".to_string(), ID::random());
let mut data: HashMap<String, Field> = HashMap::new(); data.insert("c".to_string(), ID::random());
data.insert("a".to_string(), a.clone()); let mut rec = Record::new();
data.insert("b".to_string(), b.clone()); for (key, value) in data.iter() {
data.insert("c".to_string(), c.clone()); rec.add(key, value.clone());
let rec = Record::new(data.clone()); }
assert_eq!(rec.len(), data.len());
for (key, value) in rec.iter() { for (key, value) in rec.iter() {
assert_eq!(value.to_string(), data.get(key).unwrap().to_string()); assert_eq!(value.to_string(), data.get(key).unwrap().to_string());
} }

View File

@ -11,6 +11,7 @@ use std::{collections::HashMap, fmt};
pub enum TBLError { pub enum TBLError {
DuplicateField(String), DuplicateField(String),
InvalidField(String), InvalidField(String),
MissingField(String),
} }
impl fmt::Display for TBLError { impl fmt::Display for TBLError {
@ -18,6 +19,7 @@ impl fmt::Display for TBLError {
match self { match self {
TBLError::DuplicateField(data) => write!(f, "'{}' already exists", data), TBLError::DuplicateField(data) => write!(f, "'{}' already exists", data),
TBLError::InvalidField(data) => write!(f, "'{}' invalid field name", data), TBLError::InvalidField(data) => write!(f, "'{}' invalid field name", data),
TBLError::MissingField(data) => write!(f, "'{}' field is missing", data),
} }
} }
} }
@ -39,6 +41,13 @@ mod errors {
let err = TBLError::InvalidField(data.to_string()); let err = TBLError::InvalidField(data.to_string());
assert_eq!(err.to_string(), format!("'{}' invalid field name", data)); assert_eq!(err.to_string(), format!("'{}' invalid field name", data));
} }
#[test]
fn missing_field() {
let data = "something";
let err = TBLError::MissingField(data.to_string());
assert_eq!(err.to_string(), format!("'{}' field is missing", data));
}
} }
enum FieldType { enum FieldType {
@ -86,15 +95,22 @@ impl Table {
} }
fn add_record(&mut self, rec: Record) -> Result<Record, MTTError> { fn add_record(&mut self, rec: Record) -> Result<Record, MTTError> {
for (key, _) in rec.iter() { let mut holder = rec.clone();
match self.fields.get(key) { for (key, _) in self.fields.iter() {
match holder.remove(key.into()) {
Some(_) => {} Some(_) => {}
None => { None => {
let err = TBLError::InvalidField(key.to_string()); let err = TBLError::MissingField(key.to_string());
return Err(err.into()); return Err(err.into());
} }
} }
} }
if holder.len() > 0 {
for (key, _) in holder.iter() {
let err = TBLError::InvalidField(key.to_string());
return Err(err.into());
}
}
Ok(rec) Ok(rec)
} }
} }
@ -134,10 +150,10 @@ mod table {
match tbl.add_field(name, FieldType::ID) { match tbl.add_field(name, FieldType::ID) {
Ok(_) => unreachable!(" Should not duplicates."), Ok(_) => unreachable!(" Should not duplicates."),
Err(err) => match err.get_code() { Err(err) => match err.get_code() {
ErrorType::TBLErr(data) => {match data { ErrorType::TBLErr(data) => match data {
TBLError::DuplicateField(fname) => assert_eq!(fname, name), TBLError::DuplicateField(fname) => assert_eq!(fname, name),
_ => unreachable!("Should have been a dupluicate field error"), _ => unreachable!("Should have been a dupluicate field error"),
}} },
_ => unreachable!("should produce a duplicate name error"), _ => unreachable!("should produce a duplicate name error"),
}, },
} }
@ -149,9 +165,8 @@ mod table {
let name = "id"; let name = "id";
let field_data = ID::random(); let field_data = ID::random();
tbl.add_field(name, FieldType::ID).unwrap(); tbl.add_field(name, FieldType::ID).unwrap();
let mut data: HashMap<String, Field> = HashMap::new(); let mut rec = Record::new();
data.insert(name.to_string(), field_data.clone().into()); rec.add(name, field_data.clone());
let rec = Record::new(data);
let result = tbl.add_record(rec).unwrap(); let result = tbl.add_record(rec).unwrap();
assert_eq!( assert_eq!(
result.get(name).unwrap().to_string(), result.get(name).unwrap().to_string(),
@ -164,9 +179,8 @@ mod table {
let mut tbl = Table::new(); let mut tbl = Table::new();
let name = "id"; let name = "id";
let field_data = ID::random(); let field_data = ID::random();
let mut data: HashMap<String, Field> = HashMap::new(); let mut rec = Record::new();
data.insert(name.to_string(), field_data.clone().into()); rec.add(name, field_data.clone());
let rec = Record::new(data);
match tbl.add_record(rec) { match tbl.add_record(rec) {
Ok(_) => unreachable!("should have produced an error"), Ok(_) => unreachable!("should have produced an error"),
Err(err) => match err.get_code() { Err(err) => match err.get_code() {
@ -175,7 +189,28 @@ mod table {
_ => unreachable!("should have been invalid field name"), _ => unreachable!("should have been invalid field name"),
}, },
_ => unreachable!("should have been a table error"), _ => unreachable!("should have been a table error"),
},
} }
} }
#[test]
fn add_record_missing_field() {
let mut tbl = Table::new();
let present = "here";
let missing = "not";
tbl.add_field(present, FieldType::ID);
tbl.add_field(missing, FieldType::ID);
let mut rec = Record::new();
rec.add(present, ID::random());
match tbl.add_record(rec) {
Ok(_) => unreachable!("should have produced an error"),
Err(err) => match err.get_code() {
ErrorType::TBLErr(error) => match error {
TBLError::MissingField(result) => assert_eq!(result, missing),
_ => unreachable!("should have been missing field"),
},
_ => unreachable!("should have been a table error"),
},
}
} }
} }

View File

@ -1,4 +1,4 @@
use crate::data::{id::IDError, DBError, table::TBLError}; use crate::data::{id::IDError, table::TBLError, DBError};
use std::{error::Error, fmt}; use std::{error::Error, fmt};
#[derive(Debug)] #[derive(Debug)]
@ -109,7 +109,6 @@ impl From<TBLError> for MTTError {
} }
} }
#[cfg(test)] #[cfg(test)]
mod errors { mod errors {
use super::*; use super::*;