-
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.
Do I want a CLI interface? Not for the end user, but definitely as a path towards testability
- Loading branch information
1 parent
9136f3f
commit d151a67
Showing
3 changed files
with
133 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -15,36 +15,13 @@ async fn main() -> anyhow::Result<()> { | |
|
||
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?; | ||
|
||
println!("Welcome. You must run a postgres container to have this work"); | ||
|
||
let contact_repo = PostgresContactRepo::new(pool); | ||
|
||
let contact = parse_arguments()?; | ||
|
||
let id = contact_repo.save(contact).await?; | ||
|
||
println!("{id}"); | ||
|
||
let all_contact = contact_repo.get_all().await?; | ||
|
||
let most_recent_contact = &all_contact[all_contact.len() - 1..]; | ||
|
||
println!("{most_recent_contact:?}"); | ||
|
||
let edits = models::ContactBuilder::new(id) | ||
.set_first_name("New Name") | ||
.set_last_name("Yep") | ||
.set_email("[email protected]") | ||
.set_phone_number("1233211233") | ||
.set_display_name("Nickname") | ||
.build() | ||
.unwrap(); | ||
|
||
let _ = contact_repo.update(edits).await; | ||
let _ = contact_repo.save(contact).await?; | ||
|
||
let my_first_contact = contact_repo.get_by_id(1).await.unwrap(); | ||
|
||
println!("My first contact is {my_first_contact:?}"); | ||
println!("Successfully saved contact."); | ||
|
||
Ok(()) | ||
} | ||
|
@@ -60,3 +37,62 @@ fn parse_arguments() -> Result<models::Contact, AppError> { | |
|
||
models::Contact::new(&args[1], &args[2], &args[3], &args[4]) | ||
} | ||
|
||
#[cfg(test)] | ||
|
||
mod tests { | ||
use assert_cmd::Command; | ||
|
||
#[test] | ||
fn test_connect_works() { | ||
let mut cmd = Command::cargo_bin("connect").unwrap(); | ||
|
||
cmd.arg("First") | ||
.arg("Last") | ||
.arg("[email protected]") | ||
.arg("123-321-1233"); | ||
|
||
cmd.assert() | ||
.success() | ||
.stdout(predicates::str::contains("Successfully saved contact.")); | ||
} | ||
|
||
#[test] | ||
fn test_connect_invalid_email() { | ||
let mut cmd = Command::cargo_bin("connect").unwrap(); | ||
|
||
cmd.arg("First") | ||
.arg("Last") | ||
.arg("[email protected]") | ||
.arg("123-321-1233"); | ||
|
||
cmd.assert() | ||
.failure() | ||
.stderr(predicates::str::contains("Error: [email protected] is invalid.\n")); | ||
} | ||
|
||
#[test] | ||
fn test_connect_invalid_phone() { | ||
let mut cmd = Command::cargo_bin("connect").unwrap(); | ||
|
||
cmd.arg("First") | ||
.arg("Last") | ||
.arg("[email protected]") | ||
.arg("32321123"); | ||
|
||
cmd.assert() | ||
.failure() | ||
.stderr(predicates::str::contains("Error: 32321123 is invalid.\n")); | ||
} | ||
|
||
#[test] | ||
fn test_connect_invalid_args() { | ||
let mut cmd = Command::cargo_bin("connect").unwrap(); | ||
|
||
cmd.arg("First").arg("Last").arg("32321123"); | ||
|
||
cmd.assert() | ||
.failure() | ||
.stderr(predicates::str::contains("Error: Invalid argument\n")); | ||
} | ||
} |