Skip to content

Commit

Permalink
fix(test): finish refactor
Browse files Browse the repository at this point in the history
first time me surpressing a warning...
I find it okay. It makes it more readable
  • Loading branch information
jasonribble committed Jul 29, 2024
1 parent 9c08294 commit 4e5c393
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
10 changes: 9 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,15 @@ mod tests {
.times(1)
.return_once(|_| Ok(()));

let edits = models::ContactBuilder::new(1, None, None, None, Some("[email protected]".to_string()), None).unwrap();
let edits = models::ContactBuilder::new(
1,
None,
None,
Some("[email protected]".to_string()),
None,
None,
)
.unwrap();

let result = mock_contact_repo.update(edits).await;

Expand Down
72 changes: 51 additions & 21 deletions src/models/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,30 @@ impl Update {
pub struct Builder {
pub id: i64,
pub update: Update,
errors: Vec<AppError>,
}
impl Builder {
pub fn new(
id: i64,
first_name:Option<String>,
last_name:Option<String>,
email:Option<String>,
phone_number:Option<String>,
first_name: Option<String>,
last_name: Option<String>,
email: Option<String>,
phone_number: Option<String>,
display_name: Option<String>,
) -> Result<Self, AppError> {

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

if utils::is_not_valid_email(maybe_email) && email != None {
return Err(AppError::InvalidEmail(email.clone().unwrap_or_else(|| String::from(""))));
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 {
return Err(AppError::InvalidPhoneNumber(phone_number.clone().unwrap_or_else(|| String::from(""))));
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 {
Expand All @@ -70,19 +72,16 @@ impl Builder {
email,
phone_number,
};

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

Ok(Self {
id,
update,
errors: Vec::new(),
})
Ok(Self { id, update })
}

pub const fn is_empty(&self) -> bool {
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.update.first_name.is_none()
&& self.update.last_name.is_none()
&& self.update.display_name.is_none()
Expand Down Expand Up @@ -119,6 +118,8 @@ impl Contact {
}
#[cfg(test)]
mod tests {
use crate::errors::AppError;

use super::{Builder, Contact};

#[test]
Expand All @@ -131,7 +132,15 @@ mod tests {

#[test]
fn test_contact_update_builder() {
let edits = Builder::new(1, None, None, None, Some("123-233-1221".to_string()), Some("Nickname".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 @@ -143,7 +152,15 @@ mod tests {

#[test]
fn test_contact_update_builder_2() {
let edits = Builder::new(2, Some("Mary".to_string()), Some("Smith".to_string()), Some("[email protected]".to_string()), None, 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 @@ -159,16 +176,29 @@ mod tests {
assert!(result.is_err());
}


#[test]
fn test_invalid_email_builder() {
let result = Builder::new(1, None, None, None, Some("invalid@example".to_string()), None);
let result = Builder::new(
1,
None,
None,
Some("invalid@example".to_string()),
None,
None,
);
assert!(result.is_err());
assert!(matches!(result, Err(AppError::InvalidEmail(email)) if email == "invalid@example"));
}

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

println!("{result:?}");
assert!(result.is_err());

assert!(
matches!(result, Err(AppError::InvalidPhoneNumber(phone_number)) if phone_number == "123-123-12345")
);
}
}

0 comments on commit 4e5c393

Please sign in to comment.