Преглед изворни кода

drop the test postgres databse after each test

appflowy пре 3 година
родитељ
комит
46d1d110e1
1 измењених фајлова са 24 додато и 2 уклоњено
  1. 24 2
      backend/tests/api/helper.rs

+ 24 - 2
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.");
+}