40 lines
1.4 KiB
Rust
40 lines
1.4 KiB
Rust
use crate::diesel::pool::{create_pool_from_url, PgPool};
|
|
use deadpool_diesel::postgres::BuildError;
|
|
use derive_more::{Constructor, From};
|
|
use diesel_async::pooled_connection::deadpool::PoolError;
|
|
use lib::diesel::DieselError;
|
|
use testcontainers_modules::postgres::Postgres;
|
|
use testcontainers_modules::testcontainers::runners::AsyncRunner;
|
|
use testcontainers_modules::testcontainers::{ContainerAsync, TestcontainersError};
|
|
|
|
/// When the TestContainer is dropped, the container will be removed.
|
|
/// # Errors
|
|
/// If destructed and the container field is dropped, the container will be dropped, and using the pool will cause an error.
|
|
#[derive(Constructor)]
|
|
pub struct TestContainer {
|
|
pub container: ContainerAsync<Postgres>,
|
|
pub pool: PgPool,
|
|
}
|
|
|
|
pub async fn create_test_containers_pool<'a>() -> Result<TestContainer, ContainerError> {
|
|
let container = create_postgres_container().await?;
|
|
let connection_string = format!(
|
|
"postgres://postgres:postgres@127.0.0.1:{}/postgres",
|
|
container.get_host_port_ipv4(5432).await?
|
|
);
|
|
let pool = create_pool_from_url(connection_string)?;
|
|
Ok(TestContainer::new(container, pool))
|
|
}
|
|
|
|
pub async fn create_postgres_container() -> Result<ContainerAsync<Postgres>, TestcontainersError> {
|
|
Postgres::default().start().await
|
|
}
|
|
|
|
#[derive(Debug, From)]
|
|
pub enum ContainerError {
|
|
TestContainers(TestcontainersError),
|
|
BuildError(BuildError),
|
|
PoolError(PoolError),
|
|
DieselError(DieselError),
|
|
}
|