Update test suite to drop database automatically when test is successfull
This commit is contained in:
@@ -5,14 +5,14 @@ use argon2::{
|
||||
use fake::{Fake, faker::internet::en::SafeEmail};
|
||||
use linkify::{Link, LinkFinder};
|
||||
use once_cell::sync::Lazy;
|
||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
use wiremock::{
|
||||
Mock, MockBuilder, MockServer, ResponseTemplate,
|
||||
matchers::{method, path},
|
||||
};
|
||||
use zero2prod::{
|
||||
configuration::{DatabaseSettings, get_configuration},
|
||||
configuration::get_configuration,
|
||||
email_client::EmailClient,
|
||||
issue_delivery_worker::{ExecutionOutcome, try_execute_task},
|
||||
startup::Application,
|
||||
@@ -80,19 +80,22 @@ pub struct TestApp {
|
||||
}
|
||||
|
||||
impl TestApp {
|
||||
pub async fn spawn() -> Self {
|
||||
pub async fn spawn(connection_pool: PgPool) -> Self {
|
||||
Lazy::force(&TRACING);
|
||||
|
||||
let email_server = MockServer::start().await;
|
||||
let configuration = {
|
||||
let mut c = get_configuration().expect("Failed to read configuration");
|
||||
c.database.database_name = Uuid::new_v4().to_string();
|
||||
c.application.port = 0;
|
||||
c.email_client.base_url = email_server.uri();
|
||||
c.database.database_name = connection_pool
|
||||
.connect_options()
|
||||
.get_database()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
c
|
||||
};
|
||||
let local_addr = configuration.application.host.clone();
|
||||
let connection_pool = configure_database(&configuration.database).await;
|
||||
let email_client = EmailClient::build(configuration.email_client.clone()).unwrap();
|
||||
let application = Application::build(configuration)
|
||||
.await
|
||||
@@ -312,26 +315,6 @@ impl TestApp {
|
||||
}
|
||||
}
|
||||
|
||||
async fn configure_database(config: &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).as_ref())
|
||||
.await
|
||||
.expect("Failed to create the 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
|
||||
}
|
||||
|
||||
pub fn assert_is_redirect_to(response: &reqwest::Response, location: &str) {
|
||||
assert!(
|
||||
response.status().as_u16() == 303
|
||||
|
||||
Reference in New Issue
Block a user