Patch errors if page is missing.

This commit is contained in:
2025-05-04 09:55:05 -04:00
parent cb7526dd45
commit 5502013b76
2 changed files with 123 additions and 14 deletions

View File

@@ -41,25 +41,41 @@ impl Document {
loop {
let msg = self.rx.recv().unwrap();
match msg.get_data("action") {
Some(action) => match action.to_action().unwrap() {
ActionType::Add => self.add(msg),
_ => self.get(msg),
},
Some(action_field) => {
let action = action_field.to_action().unwrap();
match action {
ActionType::Add | ActionType::Update => self.add(action, msg),
_ => self.get(msg),
}
}
None => self.get(msg),
}
}
}
fn add(&mut self, msg: Message) {
fn add(&mut self, action: ActionType, msg: Message) {
let name = msg.get_data("name").unwrap().to_string();
match self.data.get(&name) {
Some(_) => {
self.queue
.send(msg.reply_with_error(ErrorType::DocumentAlreadyExists))
.unwrap();
return;
}
None => {}
Some(_) => match action {
ActionType::Add => {
self.queue
.send(msg.reply_with_error(ErrorType::DocumentAlreadyExists))
.unwrap();
return;
}
ActionType::Update => {}
_ => unreachable!("listen should prevent anything else"),
},
None => match action {
ActionType::Add => {}
ActionType::Update => {
self.queue
.send(msg.reply_with_error(ErrorType::DocumentNotFound))
.unwrap();
return;
}
_ => unreachable!("listen should prevent anything else"),
},
}
let doc: Value = match serde_json::from_str(&msg.get_data("doc").unwrap().to_string()) {
Ok(value) => value,
@@ -251,8 +267,8 @@ pub mod documents {
#[test]
fn invalid_json() {
let inputs = ["Invalid json request.", "{}"];
let (queue, rx) = setup_document();
for input in inputs.into_iter() {
let (queue, rx) = setup_document();
let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("action", ActionType::Add);
msg.add_data("name", "doc");
@@ -282,4 +298,30 @@ pub mod documents {
}
}
}
#[test]
fn patch_nonexistant_page() {
let (queue, rx) = setup_document();
let input = json!({
"template": "Sothing here"
});
let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("action", ActionType::Update);
msg.add_data("name", "something");
msg.add_data("doc", input.to_string());
queue.send(msg.clone()).unwrap();
let reply = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply.get_id(), msg.get_id());
match reply.get_msg_type() {
MsgType::Error => {}
_ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()),
}
match reply.get_data("error_type") {
Some(err) => match err.to_error_type().unwrap() {
ErrorType::DocumentNotFound => {}
_ => unreachable!("got {:?}: should have been document not found'", err),
},
None => unreachable!("should contain error type"),
}
}
}