use diesel::migration::Migration; use diesel::{Connection, IntoSql}; use diesel_async::async_connection_wrapper::AsyncConnectionWrapper; #[test] fn test_sync_wrapper() { use diesel::RunQueryDsl; // The runtime is required for the `sqlite` implementation to be able to use // `spawn_blocking()`. This is not required for `postgres` or `mysql`. #[cfg(feature = "sqlite")] let rt = tokio::runtime::Builder::new_current_thread() .enable_io() .build() .unwrap(); #[cfg(feature = "sqlite")] let _guard = rt.enter(); let db_url = std::env::var("DATABASE_URL").unwrap(); let mut conn = AsyncConnectionWrapper::::establish(&db_url).unwrap(); let res = diesel::select(1.into_sql::()).get_result::(&mut conn); assert_eq!(Ok(1), res); } #[tokio::test] async fn test_sync_wrapper_async_query() { use diesel_async::{AsyncConnection, RunQueryDsl}; let db_url = std::env::var("DATABASE_URL").unwrap(); let conn = crate::TestConnection::establish(&db_url).await.unwrap(); let mut conn = AsyncConnectionWrapper::<_>::from(conn); let res = diesel::select(1.into_sql::()) .get_result::(&mut conn) .await; assert_eq!(Ok(1), res); } #[tokio::test] async fn test_sync_wrapper_under_runtime() { use diesel::RunQueryDsl; let db_url = std::env::var("DATABASE_URL").unwrap(); tokio::task::spawn_blocking(move || { let mut conn = AsyncConnectionWrapper::::establish(&db_url).unwrap(); let res = diesel::select(1.into_sql::()).get_result::(&mut conn); assert_eq!(Ok(1), res); }) .await .unwrap(); } #[test] fn check_run_migration() { use diesel_migrations::MigrationHarness; let db_url = std::env::var("DATABASE_URL").unwrap(); let migrations: Vec>> = Vec::new(); let mut conn = AsyncConnectionWrapper::::establish(&db_url).unwrap(); // just use `run_migrations` here because that's the easiest one without additional setup conn.run_migrations(&migrations).unwrap(); }