Skip to content

Commit

Permalink
Each running test, creates a new database that is used for inserting …
Browse files Browse the repository at this point in the history
…- ISSUE, not deleting databases
  • Loading branch information
ICavlek committed Sep 30, 2023
1 parent 153e467 commit c557373
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ impl DatabaseSettings {
self.username, self.password, self.host, self.port, self.database_name
)
}

pub fn connection_string_without_db(&self) -> String {
format!(
"postgres://{}:{}@{}:{}",
self.username, self.password, self.host, self.port
)
}
}

pub fn get_configuration() -> Result<Settings, config::ConfigError> {
Expand Down
5 changes: 3 additions & 2 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use actix_web::{web, HttpResponse};
use sqlx::PgPool;
use chrono::Utc;
use sqlx::PgPool;
use uuid::Uuid;

#[derive(serde::Deserialize)]
Expand All @@ -23,7 +23,8 @@ pub async fn subscribe(form: web::Form<FormData>, pool: web::Data<PgPool>) -> Ht
Utc::now()
)
.execute(pool.get_ref())
.await {
.await
{
Ok(_) => HttpResponse::Ok().finish(),
Err(e) => {
println!("Failed to execute query: {}", e);
Expand Down
33 changes: 27 additions & 6 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use sqlx::PgPool;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener;
use zero2prod::{configuration::get_configuration, startup::run};
use uuid::Uuid;
use zero2prod::{
configuration::{get_configuration, DatabaseSettings},
startup::run,
};

pub struct TestApp {
pub address: String,
Expand All @@ -11,10 +15,9 @@ async fn spawn_app() -> TestApp {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let address = format!("http://127.0.0.1:{}", port);
let configuration = get_configuration().expect("Failed to read configuration.");
let connection_pool = PgPool::connect(&configuration.database.connection_string())
.await
.expect("Failed to connect to Postgres.");
let mut configuration = get_configuration().expect("Failed to read configuration.");
configuration.database.database_name = Uuid::new_v4().to_string();
let connection_pool = configure_database(&configuration.database).await;
let server = run(listener, connection_pool.clone()).expect("Failed to bind address");
let _ = tokio::spawn(server);
TestApp {
Expand All @@ -23,6 +26,24 @@ async fn spawn_app() -> TestApp {
}
}

pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
let mut connection = PgConnection::connect(&config.connection_string_without_db())
.await
.expect("Failed to connect to Postgres");
connection
.execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_str())
.await
.expect("Failed to create database.");
let connection_pool = PgPool::connect(&config.connection_string())
.await
.expect("Failed to connect to Postgres.");
sqlx::migrate!("./migrations")
.run(&connection_pool)
.await
.expect("Failed to migrate the database");
connection_pool
}

#[tokio::test]
async fn health_check_works() {
let app = spawn_app().await;
Expand Down

0 comments on commit c557373

Please sign in to comment.