use std::future::Future;
use testcontainers::runners::AsyncRunner;
use testcontainers_modules::postgres::Postgres;
pub async fn with_testcontainer_postgres<F, Fut, T>(f: F) -> T
where
F: FnOnce(String) -> Fut,
Fut: Future<Output = T>,
{
let container = Postgres::default()
.start()
.await
.expect("start postgres container");
let host = container.get_host().await.expect("container host");
let port = container
.get_host_port_ipv4(5432)
.await
.expect("container port");
let url = format!("postgres://postgres:postgres@{host}:{port}/postgres");
f(url).await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
#[ignore = "needs Docker (testcontainers)"]
async fn testcontainer_connects_via_purwa_orm() {
with_testcontainer_postgres(|url| async move {
let pool = purwa_orm::connect_pool(&url).await.expect("connect_pool");
let (n,): (i64,) = sqlx::query_as("SELECT 1::bigint")
.fetch_one(&pool)
.await
.expect("SELECT 1");
assert_eq!(n, 1);
})
.await;
}
}