Got add and query working for documents.

This commit is contained in:
Jeff Baskin 2025-08-07 13:51:22 -04:00
parent 2fa92904a9
commit 6d0876f9e9

View File

@ -1186,15 +1186,12 @@ impl Reply {
}
}
impl Iterator for Reply {
impl IntoIterator for Reply {
type Item = Document;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn next(&mut self) -> Option<Self::Item> {
if self.data.len() > 0 {
Some(self.data.remove(0))
} else {
None
}
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}
@ -1228,17 +1225,18 @@ mod replies {
let mut reply = Reply::new();
reply.add(doc1);
reply.add(doc2);
let result1 = reply.next().unwrap();
let mut reply_iter = reply.into_iter();
let mut result1 = reply_iter.next().unwrap();
match result1.get_field(&fieldname).unwrap() {
Field::StaticString(output) => assert_eq!(output, "one"),
_ => unreachable!("got {:?}: should have been static string", result1),
}
let result2 = reply.next().unwrap();
let result2 = reply_iter.next().unwrap();
match result2.get_field(&fieldname).unwrap() {
Field::StaticString(output) => assert_eq!(output, "two"),
_ => unreachable!("got {:?}: should have been static string", result2),
}
match reply.next() {
match reply_iter.next() {
None => {}
Some(_) => unreachable!("should be out of data"),
}
@ -1300,6 +1298,7 @@ mod documents {
}
struct DocumentFile {
docs: Vec<Document>,
queue: Queue,
rx: Receiver<Message>,
}
@ -1307,6 +1306,7 @@ struct DocumentFile {
impl DocumentFile {
fn new(queue: Queue, rx: Receiver<Message>) -> Self {
Self {
docs: Vec::new(),
queue: queue,
rx: rx,
}
@ -1353,15 +1353,26 @@ impl DocumentFile {
let msg = self.rx.recv().unwrap();
let reply = match msg.get_action() {
MsgAction::Addition(data) => self.add_document(data),
MsgAction::Query(query) => self.query(query),
_ => Reply::new(),
};
self.queue.send(msg.reply(reply)).unwrap();
}
}
fn add_document(&mut self, new_doc: &Addition) -> Reply {
fn add_document(&mut self, addition: &Addition) -> Reply {
let mut reply = Reply::new();
reply.add(new_doc.get_document());
let doc = addition.get_document();
self.docs.push(doc.clone());
reply.add(doc);
reply
}
fn query(&self, query: &Query) -> Reply {
let mut reply = Reply::new();
for doc in self.docs.iter() {
reply.add(doc.clone());
}
reply
}
}
@ -1479,6 +1490,7 @@ mod document_files {
fn can_document_be_added() {
let mut docdef = DocDef::new();
let name = "field";
let doc_name = "document";
let data = Uuid::new_v4();
docdef.add_field(name.to_string(), FieldType::Uuid);
let routes = [RouteRequest::new(
@ -1487,17 +1499,51 @@ mod document_files {
Include::Some(Action::Reply),
)]
.to_vec();
let (queue, rx) = test_doc(name, docdef, routes);
let msg = Message::new(name, Addition::new());
let (queue, rx) = test_doc(doc_name, docdef, routes);
let mut new_doc = Addition::new();
new_doc.add_field(name.to_string(), data.clone());
let msg = Message::new(doc_name, new_doc);
queue.send(msg.clone()).unwrap();
let result = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(result.get_message_id(), msg.get_message_id());
match result.get_action() {
MsgAction::Reply(output) => assert_eq!(output.len(), 1),
MsgAction::Reply(output) => {
assert_eq!(output.len(), 1);
let holder = output.clone().into_iter().next().unwrap();
match holder.get_field(name) {
Some(field) => match field {
Field::Uuid(store) => assert_eq!(store, &data),
_ => unreachable!(
"got {:?}: should have received uuid",
holder.get_field(name).unwrap()
),
},
None => unreachable!("{:?} did not contain field '{}'", holder, name),
}
}
_ => unreachable!("got {:?}: should have been a reply", result),
}
let msg = Message::new(doc_name, Query::new());
queue.send(msg.clone()).unwrap();
let result = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(result.get_message_id(), msg.get_message_id());
match result.get_action() {
MsgAction::Reply(output) => {
assert_eq!(output.len(), 1);
let holder = output.clone().into_iter().next().unwrap();
match holder.get_field(name) {
Some(field) => match field {
Field::Uuid(store) => assert_eq!(store, &data),
_ => unreachable!(
"got {:?}: should have received uuid",
holder.get_field(name).unwrap()
),
},
None => unreachable!("{:?} did not contain field '{}'", holder, name),
}
}
_ => unreachable!("got {:?}: should have been a reply", result),
}
// Finish the test.
// Need to add addition message.
}
}