Added errors for missing or bad data.
This commit is contained in:
parent
7dbbe2fdce
commit
50481c18ad
@ -18,7 +18,9 @@ mod support_test {
|
||||
#[derive(Clone, Debug)]
|
||||
enum MTTError {
|
||||
DocumentAlreadyExists(String),
|
||||
DocumentFieldMissing(String),
|
||||
DocumentFieldNotFound(String),
|
||||
DocumentFieldWrongDataType(FieldType, FieldType),
|
||||
DocumentNotFound(String),
|
||||
}
|
||||
|
||||
@ -215,7 +217,10 @@ impl Message {
|
||||
&self.action
|
||||
}
|
||||
|
||||
fn response<A>(&self, action: A) -> Self where A: Into<MsgAction> {
|
||||
fn response<A>(&self, action: A) -> Self
|
||||
where
|
||||
A: Into<MsgAction>,
|
||||
{
|
||||
Self {
|
||||
msg_id: self.msg_id.clone(),
|
||||
document_id: self.document_id.clone(),
|
||||
@ -1126,6 +1131,8 @@ mod additions {
|
||||
}
|
||||
}
|
||||
|
||||
type DocDefMap = HashMap<String, FieldSetting>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct DocDef {
|
||||
fields: HashMap<String, FieldSetting>,
|
||||
@ -1150,6 +1157,15 @@ impl DocDef {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a DocDef {
|
||||
type Item = <&'a DocDefMap as IntoIterator>::Item;
|
||||
type IntoIter = <&'a DocDefMap as IntoIterator>::IntoIter;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
(&self.fields).into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod docdefs {
|
||||
use super::*;
|
||||
@ -1417,16 +1433,33 @@ impl DocumentFile {
|
||||
}
|
||||
|
||||
fn add_document(&mut self, addition: &Addition) -> MsgAction {
|
||||
let mut reply = Reply::new();
|
||||
let mut holder = Document::new();
|
||||
let doc = addition.get_document();
|
||||
for (key, value) in doc.into_iter() {
|
||||
match self.docdef.get_field(&key) {
|
||||
Err(err) => return err.into(),
|
||||
Ok(_) => {},
|
||||
Ok(field_info) => {
|
||||
if field_info.get_type() == &value.get_type() {
|
||||
holder.add_field(key.clone(), value.clone());
|
||||
} else {
|
||||
return MTTError::DocumentFieldWrongDataType(
|
||||
value.get_type(),
|
||||
field_info.get_type().clone(),
|
||||
)
|
||||
.into();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.docs.push(doc.clone());
|
||||
reply.add(doc);
|
||||
for (key, _) in self.docdef.into_iter() {
|
||||
match holder.get_field(key) {
|
||||
Some(_) => {}
|
||||
None => return MTTError::DocumentFieldMissing(key.clone()).into(),
|
||||
}
|
||||
}
|
||||
self.docs.push(holder.clone());
|
||||
let mut reply = Reply::new();
|
||||
reply.add(holder);
|
||||
reply.into()
|
||||
}
|
||||
|
||||
@ -1671,7 +1704,49 @@ mod document_files {
|
||||
MsgAction::Error(err) => match err {
|
||||
MTTError::DocumentFieldNotFound(data) => assert_eq!(data, &field_name),
|
||||
_ => unreachable!("got {:?}: should have been document field not found.", err),
|
||||
}
|
||||
},
|
||||
_ => unreachable!("got {:?}: should have been an error", result.get_action()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_on_wrong_field_type() {
|
||||
let doc_name = "mismatch";
|
||||
let (queue, rx) = test_doc(doc_name, create_docdef(1), standard_routes());
|
||||
let mut addition = Addition::new();
|
||||
addition.add_field("field0".to_string(), "astring");
|
||||
let msg = Message::new(doc_name, addition);
|
||||
queue.send(msg).unwrap();
|
||||
let result = rx.recv_timeout(TIMEOUT).unwrap();
|
||||
match result.get_action() {
|
||||
MsgAction::Error(err) => match err {
|
||||
MTTError::DocumentFieldWrongDataType(found, expected) => {
|
||||
assert_eq!(found, &FieldType::StaticString);
|
||||
assert_eq!(expected, &FieldType::Uuid);
|
||||
}
|
||||
_ => unreachable!(
|
||||
"got {:?}: should have been document field data mismatch.",
|
||||
err
|
||||
),
|
||||
},
|
||||
_ => unreachable!("got {:?}: should have been an error", result.get_action()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_on_missing_fields() {
|
||||
let doc_name = "missing";
|
||||
let (queue, rx) = test_doc(doc_name, create_docdef(2), standard_routes());
|
||||
let mut addition = Addition::new();
|
||||
addition.add_field("field0".to_string(), Uuid::nil());
|
||||
let msg = Message::new(doc_name, addition);
|
||||
queue.send(msg).unwrap();
|
||||
let result = rx.recv_timeout(TIMEOUT).unwrap();
|
||||
match result.get_action() {
|
||||
MsgAction::Error(err) => match err {
|
||||
MTTError::DocumentFieldMissing(field) => assert_eq!(field, "field1"),
|
||||
_ => unreachable!("got {:?}: should have been document field missing", err),
|
||||
},
|
||||
_ => unreachable!("got {:?}: should have been an error", result.get_action()),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user