lf9-pgg/crates/backend/tests/common/test_helpers.rs

48 lines
1.2 KiB
Rust

use backend::Database;
use lazy_static::lazy_static;
use testcontainers::ContainerAsync;
use testcontainers_modules::{postgres::Postgres, redis::Redis};
use super::setup;
struct TestState {
_postgres: ContainerAsync<Postgres>,
_redis: ContainerAsync<Redis>,
database: Database,
}
lazy_static! {
static ref TEST_STATE: tokio::sync::OnceCell<TestState> = tokio::sync::OnceCell::new();
}
pub async fn get_database() -> &'static Database {
let state = TEST_STATE
.get_or_init(|| async {
let (postgres, redis, database) = setup().await;
TestState {
_postgres: postgres,
_redis: redis,
database,
}
})
.await;
&state.database
}
#[macro_export]
macro_rules! create_test_app {
() => {{
let db = $crate::common::test_helpers::get_database().await;
actix_web::test::init_service(
actix_web::App::new()
.app_data(actix_web::web::Data::new(db.clone()))
.service(
actix_web::web::scope("/api/v1")
.configure(backend::controller::register_controllers),
),
)
.await
}};
}