use sqlx::PgPool;
use testcontainers::ContainerAsync;
use testcontainers_modules::postgres::Postgres;
pub struct EphemeralPostgres {
_container: ContainerAsync<Postgres>,
connection_url: String,
}
impl EphemeralPostgres {
pub async fn start() -> Self {
use testcontainers::runners::AsyncRunner;
let container = Postgres::default()
.start()
.await
.expect("failed to start Postgres container");
let host_port = container
.get_host_port_ipv4(5432)
.await
.expect("failed to get mapped port");
let connection_url = format!("postgres://postgres:postgres@127.0.0.1:{host_port}/postgres");
Self {
_container: container,
connection_url,
}
}
pub fn connection_url(&self) -> &str {
&self.connection_url
}
pub async fn pool(&self) -> PgPool {
PgPool::connect(&self.connection_url)
.await
.expect("failed to connect to ephemeral Postgres")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
#[ignore]
async fn start_pool_select_one() {
let pg = EphemeralPostgres::start().await;
let pool = pg.pool().await;
let row: (i64,) = sqlx::query_as("SELECT 1").fetch_one(&pool).await.unwrap();
assert_eq!(row.0, 1);
}
}