From 17feadd6517591c59b121d82afbd5a1993e78ea1 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 13 Aug 2025 11:17:51 -0400 Subject: [PATCH] Added query errors. --- src/message.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/src/message.rs b/src/message.rs index ca06001..a066888 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1254,8 +1254,24 @@ impl Query { self.specifiers.push(spec); } - fn run(&self, docs: &DocumentFile) -> Reply { + fn run(&self, docs: &DocumentFile) -> Result { 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() { let mut output = true; for specifier in self.specifiers.iter() { @@ -1268,7 +1284,7 @@ impl Query { 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 { self.docs.iter() } @@ -1513,7 +1533,10 @@ impl DocumentFile { } 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), } } + + #[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)]