-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first time me surpressing a warning... I find it okay. It makes it more readable
- Loading branch information
1 parent
9c08294
commit 4e5c393
Showing
2 changed files
with
60 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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() | ||
|
@@ -119,6 +118,8 @@ impl Contact { | |
} | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::errors::AppError; | ||
|
||
use super::{Builder, Contact}; | ||
|
||
#[test] | ||
|
@@ -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())); | ||
|
@@ -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())); | ||
|
@@ -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") | ||
); | ||
} | ||
} |