Public Access
initial-commit
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user