From 46d1d110e1292cd49f4f78ea1bb40ebed2163265 Mon Sep 17 00:00:00 2001 From: appflowy Date: Mon, 6 Sep 2021 14:24:22 +0800 Subject: [PATCH] drop the test postgres databse after each test --- backend/tests/api/helper.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/backend/tests/api/helper.rs b/backend/tests/api/helper.rs index e91958347b..039daec9fa 100644 --- a/backend/tests/api/helper.rs +++ b/backend/tests/api/helper.rs @@ -129,9 +129,10 @@ impl TestApp { } pub async fn spawn_app() -> TestApp { + let database_name = format!("{}", Uuid::new_v4().to_string()); let configuration = { let mut c = get_configuration().expect("Failed to read configuration."); - c.database.database_name = Uuid::new_v4().to_string(); + c.database.database_name = database_name.clone(); // Use a random OS port c.application.port = 0; c @@ -143,7 +144,10 @@ pub async fn spawn_app() -> TestApp { .expect("Failed to build application."); let application_port = application.port(); - let _ = tokio::spawn(application.run_until_stopped()); + let _ = tokio::spawn(async { + let _ = application.run_until_stopped(); + drop_test_database(database_name).await; + }); TestApp { address: format!("http://localhost:{}", application_port), @@ -176,3 +180,21 @@ async fn configure_database(config: &DatabaseSettings) -> PgPool { connection_pool } + +async fn drop_test_database(database_name: String) { + let configuration = { + let mut c = get_configuration().expect("Failed to read configuration."); + c.database.database_name = "flowy".to_owned(); + c.application.port = 0; + c + }; + + let mut connection = PgConnection::connect_with(&configuration.database.without_db()) + .await + .expect("Failed to connect to Postgres"); + + connection + .execute(&*format!(r#"Drop DATABASE "{}";"#, database_name)) + .await + .expect("Failed to drop database."); +}