use secrecy::Secret;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use uuid::Uuid;
pub struct TestApp {
pub address: String,
pub port: u16,
pub db_pool: PgPool,
}
pub async fn spawn_app() -> TestApp {
let configuration = {
let mut c = eventify_http_server::Settings {
application: eventify_http_server::ApplicationSettings {
host: String::from("localhost"),
port: 0,
worker_threads: 1,
},
database: eventify_primitives::DatabaseSettings {
host: String::from("localhost"),
port: 5432,
username: String::from("postgres"),
password: Secret::new(String::from("password")),
database_name: String::from(""),
require_ssl: false,
},
};
c.database.database_name = Uuid::new_v4().to_string();
c.application.port = 0;
c
};
configure_database(&configuration.database).await;
let application = eventify_http_server::startup::Application::build(configuration.clone())
.await
.expect("Failed to build application.");
let application_port = application.port();
let _ = tokio::spawn(application.run_until_stopped());
TestApp {
address: format!("http://localhost:{}", application_port),
port: application_port,
db_pool: eventify_http_server::startup::get_connection_pool(&configuration.database),
}
}
async fn configure_database(config: &eventify_primitives::DatabaseSettings) -> PgPool {
let mut connection = PgConnection::connect_with(&config.without_db())
.await
.expect("Failed to connect to Postgres");
connection
.execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
.await
.expect("Failed to create database.");
let connection_pool = PgPool::connect_with(config.with_db())
.await
.expect("Failed to connect to Postgres.");
sqlx::migrate!("../../migrations/rdms/postgres/")
.run(&connection_pool)
.await
.expect("Failed to migrate the database");
connection_pool
}