[−][src]Function preroll::test_utils::create_client_and_postgres
pub async fn create_client_and_postgres<State, RoutesFn>(
state: State,
setup_routes_fn: RoutesFn
) -> TestResult<(Client, Arc<RwLock<ConnectionWrapInner<Postgres>>>)> where
State: Send + Sync + 'static,
RoutesFn: Fn(&mut Server<Arc<State>>),
This is supported on crate feature
postgres
only.Creates a test application with routes and mocks set up, and hands back a client which is already connected to the server.
This function also hands back a postgres transaction connection which is being used for the rest of the application, allowing easy rollback of everything.
Important!
The RwLockWriteGuard
returned from pg_conn.write().await
MUST be dropped before running
the test cases, or else there will be a writer conflict and the test will hang indefinitely.
Example:
// use preroll::test_utils::{self, TestResult}; #[async_std::test] async fn example_test_with_postgres() -> TestResult<()> { let (client, pg_conn) = test_utils::create_client_and_postgres().await.unwrap(); { let mut pg_conn = pg_conn.write().await; // ... (test setup) ... // The RwLockWriteGuard here MUST be dropped before running the test cases, // or else there is a writer conflict and the test hangs indefinitely. // // Note: this is done automatically at the end of the closure. // We are still explicitly dropping so as to avoid accidently messing this up in the future. std::mem::drop(pg_conn); } // ... (test cases) ... Ok(()) }