fix: isolate test db
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2024-06-10 15:36:00 +05:30
parent 02e3e3f371
commit 67fa2c7381
Signed by: realaravinth
GPG key ID: F8F50389936984FF

View file

@ -24,6 +24,7 @@ use actix_web::{
};
use mktemp::Temp;
use serde::Serialize;
use url::Url;
use crate::ctx::api::v1::auth::{Login, Register};
use crate::ctx::api::v1::pages::AddSite;
@ -40,6 +41,15 @@ pub async fn get_ctx() -> (Temp, Arc<Ctx>) {
// mktemp::Temp is returned because the temp directory created
// is removed once the variable goes out of scope
let mut settings = Settings::new().unwrap();
let mut db_url = Url::parse(&settings.database.url).unwrap();
db_url.set_path(&crate::utils::get_random(8));
settings.database.url = db_url.to_string();
use sqlx::migrate::MigrateDatabase;
// sqlx::Postgres::create_database(&settings.database.url).await.unwrap();
sqlx::postgres::Postgres::create_database(&settings.database.url)
.await
.unwrap();
let tmp_dir = Temp::new_dir().unwrap();
println!("[log] Test temp directory: {}", tmp_dir.to_str().unwrap());
@ -48,8 +58,10 @@ pub async fn get_ctx() -> (Temp, Arc<Ctx>) {
settings.init();
println!("[log] Initialzing settings again with test config");
settings.init();
let ctx = Ctx::new(settings).await;
ctx.db.migrate().await.unwrap();
(tmp_dir, Ctx::new(settings).await)
(tmp_dir, ctx)
}
#[allow(dead_code, clippy::upper_case_acronyms)]