Skip to content

Commit

Permalink
Fix tests and configurations
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony M. Bonafide <anthony.bonafide@shipt.com>
  • Loading branch information
AnthonyMBonafide committed Mar 29, 2024
1 parent 78e4a51 commit 6c56be0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions scripts/init_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ DB_USER="${POSTGRES_USER:=postgres}"
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
DB_NAME="${POSTGRES_NAME:=newsletter}"
DB_PORT="${POSTGRES_PORT:=5432}"
DB_HOST="${POSTGRES_HOST:=localhost}"
DB_HOST="${POSTGRES_HOST:=127.0.0.1}"

if [[ -z "${SKIP_DOCKER}" ]]; then
docker run -e POSTGRES_USER=${DB_USER} -e POSTGRES_PASSWORD=${DB_PASSWORD} -e POSTGRES_DB=${DB_NAME} -p "${DB_PORT}":5432 -d postgres postgres -N 1000
podman run -e POSTGRES_USER=${DB_USER} -e POSTGRES_PASSWORD=${DB_PASSWORD} -e POSTGRES_DB=${DB_NAME} -p "${DB_PORT}":5432 -d postgres postgres -N 1000
fi

export PGPASSWORD="${DB_PASSWORD}"
Expand Down
17 changes: 10 additions & 7 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use zero2prod::{configuration::DatabaseSettings, get_configuration, run};

#[tokio::test]
async fn health_check_works() {
let address = spawn_app().await;
let (address, _) = spawn_app().await;

let client = reqwest::Client::new();

Expand All @@ -22,7 +22,7 @@ async fn health_check_works() {

#[tokio::test]
async fn subscribe_returns_200_for_valid_form_data() {
let address = spawn_app().await;
let (address, db_conn_string) = spawn_app().await;
let client = reqwest::Client::new();

let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
Expand All @@ -36,8 +36,7 @@ async fn subscribe_returns_200_for_valid_form_data() {

assert_eq!(200, response.status().as_u16());

let config = get_configuration().expect("Failed to get configuration");
let mut connection = PgConnection::connect(&config.database.connection_string())
let mut connection = PgConnection::connect(&db_conn_string)
.await
.expect("Failed to connect to database");

Expand All @@ -52,7 +51,7 @@ async fn subscribe_returns_200_for_valid_form_data() {

#[tokio::test]
async fn subscribe_returns_400_for_missing_form_data() {
let address = spawn_app().await;
let (address, _) = spawn_app().await;
let client = reqwest::Client::new();
let test_cases = vec![
("name=le%20guin", "missing email"),
Expand All @@ -77,7 +76,7 @@ async fn subscribe_returns_400_for_missing_form_data() {
)
}
}
async fn spawn_app() -> String {
async fn spawn_app() -> (String, String) {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind port");
let port = listener.local_addr().unwrap().port();
let mut config = get_configuration().expect("Failed to get configuration");
Expand All @@ -88,7 +87,11 @@ async fn spawn_app() -> String {

let _server_run = tokio::spawn(server);
std::mem::drop(_server_run);
format!("http://127.0.0.1:{}", port)

(
format!("http://127.0.0.1:{}", port),
config.database.connection_string(),
)
}

async fn configure_database(config: &DatabaseSettings) -> PgPool {
Expand Down

0 comments on commit 6c56be0

Please sign in to comment.