blob: 3502ddb112605c8b05a4e1e7faf79b1de6d61155 (
plain)
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
|
use std::future::Future;
use diesel::RunQueryDsl;
use mozaic4_backend::DbConn;
use rocket::local::asynchronous::Client;
// We use a lock to synchronize between tests so DB operations don't collide.
// For now. In the future, we'll have a nice way to run each test in a DB
// transaction so we can regain concurrency.
static DB_LOCK: parking_lot::Mutex<()> = parking_lot::const_mutex(());
async fn reset_db(db: &DbConn) {
db.run(|conn| {
diesel::sql_query("TRUNCATE TABLE users, sessions")
.execute(conn)
.expect("drop all tables");
})
.await
}
pub async fn run_test<F, R>(test_closure: F)
where
F: FnOnce(Client, DbConn) -> R,
R: Future<Output = ()>,
{
let _lock = DB_LOCK.lock();
let client = Client::untracked(mozaic4_backend::rocket())
.await
.expect("failed to create test client");
let db = mozaic4_backend::DbConn::get_one(client.rocket())
.await
.expect("failed to get db connection");
// make sure we start with a clean DB
reset_db(&db).await;
test_closure(client, db).await;
}
|