Got backend using errors.

This commit is contained in:
2025-04-25 14:02:40 -04:00
parent cb9bac9d8a
commit 55ffa538e8
4 changed files with 166 additions and 14 deletions

View File

@ -13,11 +13,94 @@ use queue::{Message, MsgType, Queue};
use session::Session;
use uuid::Uuid;
pub enum ActionType {
Get,
Add,
Update,
}
#[derive(Clone, Debug)]
pub enum ErrorType {
DocumentNotFound,
}
pub struct MTTReply {
document: String,
error_type: Option<ErrorType>,
}
impl MTTReply {
fn new(msg: Message) -> Self {
Self {
document: match msg.get_data("doc") {
Some(doc) => doc.to_string(),
None => "".to_string(),
},
error_type: match msg.get_data("error_type") {
Some(err) => Some(err.to_error_type().unwrap()),
None => None,
},
}
}
pub fn get_document(&self) -> String {
self.document.clone()
}
pub fn get_error(&self) -> Option<ErrorType> {
self.error_type.clone()
}
}
#[cfg(test)]
mod mtt_replies {
use super::*;
#[test]
fn create_reply_with_no_error() {
let mut msg = Message::new(MsgType::Document);
let content = format!("content-{}", Uuid::new_v4());
msg.add_data("doc", content.to_string());
let reply = MTTReply::new(msg);
assert!(reply.get_error().is_none());
assert_eq!(reply.get_document(), content);
}
#[test]
fn create_reply_with_error() {
let mut msg = Message::new(MsgType::Error);
msg.add_data("error_type", ErrorType::DocumentNotFound);
let reply = MTTReply::new(msg);
match reply.get_error() {
Some(err) => match err {
ErrorType::DocumentNotFound => {},
},
None => unreachable!("should return an error type"),
}
assert_eq!(reply.get_document(), "");
}
#[test]
fn no_error() {
let msg = Message::new(MsgType::Document);
let reply = MTTReply::new(msg);
assert!(reply.get_error().is_none());
}
#[test]
fn some_error() {
let mut msg = Message::new(MsgType::Error);
msg.add_data("error_type", ErrorType::DocumentNotFound);
let reply = MTTReply::new(msg);
match reply.get_error() {
Some(err) => match err {
ErrorType::DocumentNotFound => {},
},
None => unreachable!("should return an error type"),
}
}
}
#[derive(Clone)]
pub struct MoreThanText {
client_channel: ClientChannel,
@ -48,11 +131,55 @@ impl MoreThanText {
reply.get_data("sess_id").unwrap().to_uuid().unwrap()
}
pub fn get_document(&self, sess_id: Uuid) -> String {
pub fn get_document<S>(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply where S: Into<String> {
let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("sess_id", sess_id);
msg.add_data("name", doc_name.into());
let rx = self.client_channel.send(msg);
let reply = rx.recv().unwrap();
reply.get_data("doc").unwrap().to_string()
MTTReply::new(reply)
}
}
#[cfg(test)]
mod mtt {
use super::*;
#[test]
fn session_id_is_unique() {
let mut mtt = MoreThanText::new();
let input: Option<String> = None;
let mut ids: Vec<Uuid> = Vec::new();
for _ in 0..10 {
let id = mtt.validate_session(input.clone());
assert!(!ids.contains(&id));
ids.push(id);
}
}
#[test]
fn reuse_existing_session() {
let mut mtt = MoreThanText::new();
let initial: Option<String> = None;
let id = mtt.validate_session(initial);
let output = mtt.validate_session(Some(id.clone()));
assert_eq!(output, id);
}
#[test]
fn get_root_document_with_str() {
let mut mtt = MoreThanText::new();
let id = mtt.validate_session(Some(Uuid::new_v4()));
let output = mtt.get_document(id, ActionType::Get, "root");
assert!(output.get_error().is_none());
}
#[test]
fn get_root_document_with_string() {
let mut mtt = MoreThanText::new();
let id = mtt.validate_session(Some(Uuid::new_v4()));
let output = mtt.get_document(id, ActionType::Get, "root".to_string());
assert!(output.get_error().is_none());
}
}