diff --git a/tests/test.rs b/tests/test.rs index 6bb0b09..5683360 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -43,6 +43,29 @@ mod db_mgr { } } + pub async fn get_database_ready() -> ( + sqlx::Pool, + String, + sqlx::Pool, + ) { + let tm_pool = get_pool().await.unwrap(); + let db_name = generate_db_name().await; + + match create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + + let pool = connect_to_db(&db_name).await.unwrap(); + migrations(&pool).await; + // db::migrations(&pool).await; + (tm_pool, db_name, pool) + } + // Function to drop a database pub async fn drop_database( template_pool: &sqlx::PgPool, @@ -73,16 +96,9 @@ mod db_mgr { } } - /* pub async fn migrations(pool: &sqlx::PgPool) { - // Run migrations using the sqlx::migrate! macro - // Assumes your test migrations are in a ./test_migrations folder relative to Cargo.toml - sqlx::migrate!("./migrations") - .run(pool) - .await - .expect("Failed to run migrations"); + super::db::migrations(pool).await; } - */ } mod init { @@ -519,21 +535,7 @@ async fn test_update_contact_names() { #[tokio::test] async fn test_create_message() { - let tm_pool = db_mgr::get_pool().await.unwrap(); - let db_name = db_mgr::generate_db_name().await; - - match db_mgr::create_database(&tm_pool, &db_name).await { - Ok(_) => { - println!("Success"); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - - let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); - db::migrations(&pool).await; - + let (tm_pool, db_name, pool) = db_mgr::get_database_ready().await; let app = init::app(pool).await; // Send request @@ -560,20 +562,7 @@ async fn test_create_message() { #[tokio::test] async fn test_get_message() { - let tm_pool = db_mgr::get_pool().await.unwrap(); - let db_name = db_mgr::generate_db_name().await; - - match db_mgr::create_database(&tm_pool, &db_name).await { - Ok(_) => { - println!("Success"); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } - } - - let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); - db::migrations(&pool).await; + let (tm_pool, db_name, pool) = db_mgr::get_database_ready().await; let app = init::app(pool).await; let mut message_result: Option = None; @@ -617,3 +606,6 @@ async fn test_get_message() { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + +#[tokio::test] +async fn test_schedule_message() {}