-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
64 lines (54 loc) · 1.58 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use sqlx::postgres::PgPoolOptions;
use sqlx::{PgPool, Pool, Postgres};
use std::future::Future;
use std::net::{SocketAddr, TcpListener};
use zero2prod::{config, startup};
async fn init_solo_pool() -> Pool<Postgres> {
let pool = PgPoolOptions::new()
.max_connections(1)
.connect(&config::get_conn_string())
.await
.expect("Could not init pg pool");
pool
}
async fn init_solo_pool_and_tx_start() -> Pool<Postgres> {
let pool = init_solo_pool().await;
sqlx::query("BEGIN")
.execute(&pool)
.await
.expect("BEGIN tx failed");
pool
}
async fn startup(with_tx: bool) -> (PgPool, SocketAddr) {
let pool = if with_tx {
init_solo_pool_and_tx_start().await
} else {
init_solo_pool().await
};
let listener = TcpListener::bind("localhost:0").expect("Failed to create listener");
let socket = listener.local_addr().unwrap();
let server = startup::run(listener, pool.clone()).expect("Could not start server");
tokio::spawn(server);
(pool, socket)
}
async fn rollback(pool: &PgPool) {
sqlx::query("ROLLBACK")
.execute(pool)
.await
.expect("ROLLBACK tx failed");
}
pub async fn with_tx<F>(test_body: fn(PgPool, SocketAddr) -> F)
where
F: Future<Output = ()>,
{
let (pool, socket) = startup(true).await;
test_body(pool.clone(), socket).await;
rollback(&pool).await;
}
pub async fn no_tx<F>(test_body: fn(PgPool, SocketAddr) -> F)
where
F: Future<Output = ()>,
{
let (pool, socket) = startup(true).await;
test_body(pool.clone(), socket).await;
}