Makefile.toml
TestContainers and diesel test database
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
use diesel::{AsChangeset, Insertable, Queryable, Selectable};
|
||||
use diesel_async::{AsyncConnection, AsyncPgConnection};
|
||||
use dotenvy_macro::dotenv;
|
||||
use lib::diesel_crud_derive::{
|
||||
DieselCrudCreate, DieselCrudDelete, DieselCrudList, DieselCrudRead, DieselCrudUpdate,
|
||||
};
|
||||
use lib::diesel_crud_trait::DieselCrudCreate;
|
||||
use test_containers::create_test_containers_pool;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test_containers;
|
||||
|
||||
diesel::table! {
|
||||
user (email) {
|
||||
@ -14,6 +16,8 @@ diesel::table! {
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Queryable,
|
||||
Selectable,
|
||||
Insertable,
|
||||
@ -39,14 +43,19 @@ struct InsertUser {
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_insert_user() {
|
||||
let database_url = dotenv!("DATABASE_URL");
|
||||
let mut conn = AsyncPgConnection::establish(database_url).await.unwrap();
|
||||
conn.begin_test_transaction().await.unwrap();
|
||||
let _user = User::insert(
|
||||
let container = create_test_containers_pool().await.unwrap();
|
||||
let mut conn = container.pool.get().await.unwrap();
|
||||
let user = User::insert(
|
||||
InsertUser {
|
||||
email: "test".to_string(),
|
||||
},
|
||||
&mut conn,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
user,
|
||||
Ok(User {
|
||||
email: "test".to_string()
|
||||
})
|
||||
);
|
||||
}
|
||||
|
48
crates/tests/tests/test_containers.rs
Normal file
48
crates/tests/tests/test_containers.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use derive_more::{Constructor, From};
|
||||
use diesel_async::pooled_connection::deadpool::{BuildError, PoolError};
|
||||
use diesel_async::AsyncPgConnection;
|
||||
use diesel_async_migrations::EmbeddedMigrations;
|
||||
use lib::diesel::pool::{create_pool_from_url, PgPool};
|
||||
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.
|
||||
/// # Panics
|
||||
/// If destructed and the container field is dropped, the container will be removed, and using the pool will cause panic.
|
||||
#[derive(Constructor)]
|
||||
pub struct TestContainer {
|
||||
pub container: ContainerAsync<Postgres>,
|
||||
pub pool: PgPool,
|
||||
}
|
||||
|
||||
#[derive(Debug, From)]
|
||||
pub enum ContainerError {
|
||||
TestContainers(TestcontainersError),
|
||||
BuildError(BuildError),
|
||||
PoolError(PoolError),
|
||||
DieselError(diesel::result::Error),
|
||||
}
|
||||
|
||||
pub async fn create_test_containers_pool<'a>() -> Result<TestContainer, ContainerError> {
|
||||
let container = create_postgres_container().await?;
|
||||
let connection_string = format!(
|
||||
"postgres://postgres:postgres@localhost:{}/postgres",
|
||||
container.get_host_port_ipv4(5432).await?
|
||||
);
|
||||
let pool = create_pool_from_url(connection_string)?;
|
||||
run_migrations(pool.get().await?.as_mut()).await?;
|
||||
Ok(TestContainer::new(container, pool))
|
||||
}
|
||||
|
||||
pub(crate) async fn run_migrations(
|
||||
conn: &mut AsyncPgConnection,
|
||||
) -> Result<(), diesel::result::Error> {
|
||||
static EMBEDDED_MIGRATIONS: EmbeddedMigrations =
|
||||
diesel_async_migrations::embed_migrations!("./migrations");
|
||||
EMBEDDED_MIGRATIONS.run_pending_migrations(conn).await
|
||||
}
|
||||
|
||||
pub async fn create_postgres_container() -> Result<ContainerAsync<Postgres>, TestcontainersError> {
|
||||
Postgres::default().start().await
|
||||
}
|
Reference in New Issue
Block a user