Added query errors.

This commit is contained in:
Jeff Baskin 2025-08-13 11:17:51 -04:00
parent 692f2dceb7
commit 17feadd651

View File

@ -1254,8 +1254,24 @@ impl Query {
self.specifiers.push(spec); self.specifiers.push(spec);
} }
fn run(&self, docs: &DocumentFile) -> Reply { fn run(&self, docs: &DocumentFile) -> Result<Reply, MTTError> {
let mut reply = Reply::new(); let mut reply = Reply::new();
let docdef = docs.get_docdef();
for specifier in self.specifiers.iter() {
match docdef.get_field(&specifier.field_name) {
Ok(spec) => {
let value_type: FieldType = (&specifier.value).into();
let wanted_type = spec.get_type();
if &value_type != wanted_type {
return Err(MTTError::DocumentFieldWrongDataType(
wanted_type.clone(),
value_type.clone(),
));
}
}
Err(err) => return Err(err),
}
}
for doc in docs.get_documents() { for doc in docs.get_documents() {
let mut output = true; let mut output = true;
for specifier in self.specifiers.iter() { for specifier in self.specifiers.iter() {
@ -1268,7 +1284,7 @@ impl Query {
reply.add(doc.clone()); reply.add(doc.clone());
} }
} }
reply Ok(reply)
} }
} }
@ -1477,6 +1493,10 @@ impl DocumentFile {
} }
} }
fn get_docdef(&self) -> &DocDef {
&self.docdef
}
fn get_documents<'a>(&self) -> impl Iterator<Item = &Document> { fn get_documents<'a>(&self) -> impl Iterator<Item = &Document> {
self.docs.iter() self.docs.iter()
} }
@ -1513,7 +1533,10 @@ impl DocumentFile {
} }
fn query(&self, query: &Query) -> MsgAction { fn query(&self, query: &Query) -> MsgAction {
query.run(self).into() match query.run(self) {
Ok(reply) => reply.into(),
Err(err) => err.into(),
}
} }
} }
@ -1949,6 +1972,52 @@ mod document_files {
_ => unreachable!("got {:?}: should have been a reply", action), _ => unreachable!("got {:?}: should have been a reply", action),
} }
} }
#[test]
fn errors_on_bad_field_name() {
let doc_name = "testing";
let field_name = "wrong";
let (queue, rx) = test_doc(doc_name, create_docdef(Vec::new()), standard_routes());
let mut query = Query::new();
query.add_specifier(field_name.to_string(), Operand::Equal, Uuid::new_v4());
let msg = Message::new(doc_name, query);
queue.send(msg).unwrap();
let result = rx.recv_timeout(TIMEOUT).unwrap();
let action = result.get_action();
match action {
MsgAction::Error(data) => match data {
MTTError::DocumentFieldNotFound(output) => assert_eq!(output, field_name),
_ => unreachable!("got {:?}: should been field not found", data),
},
_ => unreachable!("got {:?}: should have been a error", action),
}
}
#[test]
fn errors_on_bad_field_type() {
let doc_name = "testing";
let (queue, rx) = test_doc(
doc_name,
create_docdef([FieldType::Uuid].to_vec()),
standard_routes(),
);
let mut query = Query::new();
query.add_specifier("field0".to_string(), Operand::Equal, "wrong");
let msg = Message::new(doc_name, query);
queue.send(msg).unwrap();
let result = rx.recv_timeout(TIMEOUT).unwrap();
let action = result.get_action();
match action {
MsgAction::Error(data) => match data {
MTTError::DocumentFieldWrongDataType(expected, got) => {
assert_eq!(expected, &FieldType::Uuid);
assert_eq!(got, &FieldType::StaticString);
}
_ => unreachable!("got {:?}: should been field not found", data),
},
_ => unreachable!("got {:?}: should have been a error", action),
}
}
} }
#[cfg(test)] #[cfg(test)]