Public Access
30 lines
997 B
Rust
30 lines
997 B
Rust
use charybdis::database::{EntityRepository, ensure_schema};
|
|
use sqlx::PgPool;
|
|
use testcontainers::runners::AsyncRunner;
|
|
use testcontainers_modules::postgres::Postgres;
|
|
|
|
pub struct TestDb {
|
|
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);
|
|
|
|
Self {
|
|
repository,
|
|
_container: container,
|
|
}
|
|
}
|
|
}
|