Skip to content

Commit 64e6388

Browse files
author
Anders Blenstrup-Pedersen
committed
feat(test-transaction): Adding test transaction to the trait AsyncConnection
1 parent 91ce1d0 commit 64e6388

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@
6666
//! ```
6767
6868
#![warn(missing_docs)]
69+
6970
use diesel::backend::Backend;
7071
use diesel::query_builder::{AsQuery, QueryFragment, QueryId};
72+
use diesel::result::Error;
7173
use diesel::row::Row;
7274
use diesel::{ConnectionResult, QueryResult};
7375
use futures_util::{Future, Stream};
76+
use std::fmt::Debug;
7477

7578
pub use scoped_futures;
76-
use scoped_futures::ScopedBoxFuture;
79+
use scoped_futures::{ScopedBoxFuture, ScopedFutureExt};
7780

7881
#[cfg(feature = "mysql")]
7982
mod mysql;
@@ -254,6 +257,30 @@ pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send {
254257
Ok(())
255258
}
256259

260+
#[doc(hidden)]
261+
fn test_transaction<'a, R, E, F>(&'a mut self, f: F) -> ScopedBoxFuture<'a, 'a, R>
262+
where
263+
F: for<'r> FnOnce(&'r mut Self) -> ScopedBoxFuture<'a, 'r, Result<R, E>> + Send + 'a,
264+
E: Debug + Send + 'a,
265+
R: Send + 'a,
266+
Self: 'a,
267+
{
268+
async move {
269+
self.transaction::<R, _, _>(|c| {
270+
async move {
271+
match f(c).await {
272+
Ok(t) => Ok(t),
273+
Err(_) => Err(Error::RollbackTransaction),
274+
}
275+
}
276+
.scope_boxed()
277+
})
278+
.await
279+
.expect("Test Transaction did not succeed")
280+
}
281+
.scope_boxed()
282+
}
283+
257284
#[doc(hidden)]
258285
fn load<'conn, 'query, T>(&'conn mut self, source: T) -> Self::LoadFuture<'conn, 'query>
259286
where

0 commit comments

Comments
 (0)