Files
charybdis/tests/harness.rs
T
2026-05-12 17:06:43 +02:00

32 lines
1.0 KiB
Rust

use charybdis::database::{EntityRepository, ensure_schema};
use sqlx::PgPool;
use testcontainers::runners::AsyncRunner;
use testcontainers_modules::postgres::Postgres;
pub struct TestDb {
pub pool: PgPool,
pub repository: EntityRepository,
_container: testcontainers::ContainerAsync<Postgres>,
}
impl TestDb {
pub async fn new() -> Self {
// Testcontainers picks up DOCKER_HOST or TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE
// For Colima users: export DOCKER_HOST=unix:///Users/$USER/.colima/default/docker.sock
let container = Postgres::default().start().await.unwrap();
let port = container.get_host_port_ipv4(5432).await.unwrap();
let url = format!("postgresql://postgres:postgres@127.0.0.1:{}/postgres", port);
let pool = PgPool::connect(&url).await.unwrap();
ensure_schema(&pool).await.unwrap();
let repository = EntityRepository::new(pool.clone());
Self {
pool,
repository,
_container: container,
}
}
}