Skip to content

Commit

Permalink
WIP: refactor: no warnings some failed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonribble committed Jul 26, 2024
1 parent 2ebefeb commit 9c08294
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub enum AppError {
DatabaseError(sqlx::Error),
InvalidEmail(String),
InvalidPhoneNumber(String),
EmptyUpdate,
}

impl fmt::Display for AppError {
Expand All @@ -13,6 +14,7 @@ impl fmt::Display for AppError {
Self::DatabaseError(error) => write!(f, "Database error: {error}"),
Self::InvalidEmail(email) => write!(f, "{email} is invalid."),
Self::InvalidPhoneNumber(phone) => write!(f, "{phone} is invalid."),
Self::EmptyUpdate => write!(f, "Empty update"),
}
}
}
Expand Down
48 changes: 29 additions & 19 deletions src/models/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ pub struct Update {
pub phone_number: Option<String>,
}

impl Update {
pub const fn is_empty(&self) -> bool {
self.first_name.is_none()
&& self.last_name.is_none()
&& self.display_name.is_none()
&& self.email.is_none()
&& self.phone_number.is_none()
}
}

#[derive(Debug)]
pub struct Builder {
pub id: i64,
Expand All @@ -43,25 +53,31 @@ impl Builder {

let maybe_email = email.as_deref().unwrap_or("");

if utils::is_not_valid_email(maybe_email) || email != None {
if utils::is_not_valid_email(maybe_email) && email != None {
return Err(AppError::InvalidEmail(email.clone().unwrap_or_else(|| String::from(""))));
}

let maybe_phone = phone_number.as_deref().unwrap_or("");

if utils::is_not_valid_phone_number(maybe_phone) || phone_number != None {
if utils::is_not_valid_phone_number(maybe_phone) && phone_number != None {
return Err(AppError::InvalidPhoneNumber(phone_number.clone().unwrap_or_else(|| String::from(""))));
}

let update = Update {
first_name,
last_name,
display_name,
email,
phone_number,
};

if update.is_empty() {
return Err(AppError::EmptyUpdate);
}

Ok(Self {
id,
update: Update {
first_name,
last_name,
display_name,
email,
phone_number,
},
update,
errors: Vec::new(),
})
}
Expand Down Expand Up @@ -115,7 +131,7 @@ mod tests {

#[test]
fn test_contact_update_builder() {
let edits = Builder::new(1, None, None, Some("[email protected]".to_string()), None, Some("123-233-1221".to_string())).unwrap();
let edits = Builder::new(1, None, None, None, Some("123-233-1221".to_string()), Some("Nickname".to_string())).unwrap();

assert_eq!(edits.id, 1);
assert_eq!(edits.update.display_name, Some("Nickname".to_string()));
Expand All @@ -127,7 +143,7 @@ mod tests {

#[test]
fn test_contact_update_builder_2() {
let edits = Builder::new(2, Some("Mary".to_string()), Some("Smith".to_string()), None, Some("[email protected]".to_string()), None).unwrap();
let edits = Builder::new(2, Some("Mary".to_string()), Some("Smith".to_string()), Some("[email protected]".to_string()), None, None).unwrap();

assert_eq!(edits.id, 2);
assert_eq!(edits.update.first_name, Some("Mary".to_string()));
Expand All @@ -139,26 +155,20 @@ mod tests {

#[test]
fn test_is_empty() {
let contact = Builder::new(1, None, None, None, None, None).unwrap();
assert!(contact.is_empty());
}

#[test]
fn test_is_empty_error() {
let result = Builder::new(1, None, None, None, None, None);
assert!(result.is_err());
}


#[test]
fn test_invalid_email_builder() {
let result: Result<Builder, crate::errors::AppError> = Builder::new(1, None, None, None, Some("invalid@example".to_string()), None);
let result = Builder::new(1, None, None, None, Some("invalid@example".to_string()), None);
assert!(result.is_err());
}

#[test]
fn test_invalid_builder_phone_number() {
let result = Builder::new(1, None, None, None, None, Some("123-123-1234".to_string()));
let result = Builder::new(1, None, None, None, None, Some("123-123-12344".to_string()));
assert!(result.is_err());
}
}

0 comments on commit 9c08294

Please sign in to comment.