Skip to content

Commit

Permalink
test: write cli tests
Browse files Browse the repository at this point in the history
Do I want a CLI interface? Not for the end user, but definitely as a path towards testability
  • Loading branch information
jasonribble committed Jul 15, 2024
1 parent 9136f3f commit d151a67
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 26 deletions.
68 changes: 68 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ dotenvy = "0.15.0"
regex = "1.5.4"
sqlx = { version = "0.7.4", features = ["runtime-tokio-native-tls", "postgres"] }
tokio = { version = "1.28.0", features = ["full", "test-util"] }

[dev-dependencies]
mockall = "0.12.1"
mockall = "0.12.1"
assert_cmd = "2.0.14"
predicates = "3.1"
86 changes: 61 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand All @@ -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"));
}
}

0 comments on commit d151a67

Please sign in to comment.