2021-02-14 12:24:50 +00:00
|
|
|
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
|
|
|
use uuid::Uuid;
|
|
|
|
use zero2prod::configuration::{get_configuration, DatabaseSettings};
|
2021-02-14 16:41:45 +00:00
|
|
|
use zero2prod::startup::{get_connection_pool, Application};
|
2021-02-14 12:24:50 +00:00
|
|
|
use zero2prod::telemetry::{get_subscriber, init_subscriber};
|
|
|
|
|
|
|
|
// Ensure that the `tracing` stack is only initialised once using `lazy_static`
|
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref TRACING: () = {
|
|
|
|
let filter = if std::env::var("TEST_LOG").is_ok() { "debug" } else { "" };
|
|
|
|
let subscriber = get_subscriber("test".into(), filter.into());
|
|
|
|
init_subscriber(subscriber);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TestApp {
|
|
|
|
pub address: String,
|
|
|
|
pub db_pool: PgPool,
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:07:31 +00:00
|
|
|
impl TestApp {
|
|
|
|
pub async fn post_subscriptions(&self, body: String) -> reqwest::Response {
|
|
|
|
reqwest::Client::new()
|
|
|
|
.post(&format!("{}/subscriptions", &self.address))
|
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
.body(body)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("Failed to execute request.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 12:24:50 +00:00
|
|
|
pub async fn spawn_app() -> TestApp {
|
2021-02-14 16:27:04 +00:00
|
|
|
// Randomise configuration to ensure test isolation
|
|
|
|
let configuration = {
|
|
|
|
let mut c = get_configuration().expect("Failed to read configuration.");
|
|
|
|
// Use a different database for each test case
|
|
|
|
c.database.database_name = Uuid::new_v4().to_string();
|
|
|
|
// Use a random OS port
|
|
|
|
c.application.port = 0;
|
|
|
|
c
|
|
|
|
};
|
2021-02-14 12:24:50 +00:00
|
|
|
|
2021-02-14 16:27:04 +00:00
|
|
|
// Create and migrate the database
|
|
|
|
configure_database(&configuration.database).await;
|
2021-02-14 12:24:50 +00:00
|
|
|
|
2021-02-14 16:27:04 +00:00
|
|
|
// Launch the application as a background task
|
2021-02-14 16:41:45 +00:00
|
|
|
let application = Application::build(configuration.clone())
|
2021-02-14 16:27:04 +00:00
|
|
|
.await
|
|
|
|
.expect("Failed to build application.");
|
2021-02-15 21:26:43 +00:00
|
|
|
let address = format!("http://localhost:{}", application.port());
|
2021-02-14 16:41:45 +00:00
|
|
|
let _ = tokio::spawn(application.run_until_stopped());
|
|
|
|
|
2021-02-14 12:24:50 +00:00
|
|
|
TestApp {
|
2021-02-14 16:41:45 +00:00
|
|
|
address,
|
2021-02-14 16:27:04 +00:00
|
|
|
db_pool: get_connection_pool(&configuration.database)
|
|
|
|
.await
|
|
|
|
.expect("Failed to connect to the database"),
|
2021-02-14 12:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 15:26:43 +00:00
|
|
|
async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
2021-02-14 12:24:50 +00:00
|
|
|
// Create database
|
|
|
|
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.");
|
|
|
|
|
|
|
|
// Migrate database
|
|
|
|
let connection_pool = PgPool::connect_with(config.with_db())
|
|
|
|
.await
|
|
|
|
.expect("Failed to connect to Postgres.");
|
|
|
|
sqlx::migrate!("./migrations")
|
|
|
|
.run(&connection_pool)
|
|
|
|
.await
|
|
|
|
.expect("Failed to migrate the database");
|
|
|
|
|
|
|
|
connection_pool
|
|
|
|
}
|