diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-18 15:39:05 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-18 15:39:05 +0100 |
commit | 2dbb085008f68ed56675cf23ea6e1c89af632ea9 (patch) | |
tree | 44f025664d0188ed854b5edb35da285dc997980c | |
parent | 6aa72b3c8717f32e62c772aeed327d3cd9a6fa65 (diff) | |
download | planetwars.dev-2dbb085008f68ed56675cf23ea6e1c89af632ea9.tar.xz planetwars.dev-2dbb085008f68ed56675cf23ea6e1c89af632ea9.zip |
use async closures for tests
-rw-r--r-- | backend/tests/login.rs | 53 | ||||
-rw-r--r-- | backend/tests/util/mod.rs | 39 |
2 files changed, 50 insertions, 42 deletions
diff --git a/backend/tests/login.rs b/backend/tests/login.rs index 9c70af2..b4e07e3 100644 --- a/backend/tests/login.rs +++ b/backend/tests/login.rs @@ -1,41 +1,10 @@ +#![feature(async_closure)] extern crate mozaic4_backend; -use diesel; -use diesel::prelude::*; -use mozaic4_backend::DbConn; use rocket::http::{ContentType, Header, Status}; -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 -} - -macro_rules! run_test { - (|$client:ident, $conn:ident| $block:expr) => {{ - let _lock = DB_LOCK.lock(); - - rocket::async_test(async move { - let $client = Client::tracked(mozaic4_backend::rocket()) - .await - .expect("Rocket client"); - let db = mozaic4_backend::DbConn::get_one($client.rocket()).await; - let $conn = db.expect("failed to get database connection for testing"); - reset_db(&$conn).await; - - $block - }) - }}; -} +mod util; +use util::run_test; pub struct BearerAuth { token: String, @@ -53,9 +22,9 @@ impl<'a> Into<Header<'a>> for BearerAuth { } } -#[test] -fn test_registration() { - run_test!(|client, _conn| { +#[rocket::async_test] +async fn test_registration() { + run_test(async move |client, _conn| { let response = client .post("/register") .header(ContentType::JSON) @@ -87,12 +56,12 @@ fn test_registration() { let resp = response.into_string().await.unwrap(); let json: serde_json::Value = serde_json::from_str(&resp).unwrap(); assert_eq!(json["username"], "piepkonijn"); - }); + }).await } -#[test] -fn test_reject_invalid_credentials() { - run_test!(|client, _conn| { +#[rocket::async_test] +async fn test_reject_invalid_credentials() { + run_test(async move |client, _conn| { let response = client .post("/login") .header(ContentType::JSON) @@ -102,5 +71,5 @@ fn test_reject_invalid_credentials() { assert_eq!(response.status(), Status::Forbidden); // assert_eq!(response.content_type(), Some(ContentType::JSON)); - }); + }).await } diff --git a/backend/tests/util/mod.rs b/backend/tests/util/mod.rs new file mode 100644 index 0000000..3502ddb --- /dev/null +++ b/backend/tests/util/mod.rs @@ -0,0 +1,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; +} |