Skip to content

Commit

Permalink
fix fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Nov 1, 2021
1 parent f3b77e1 commit aaaf35b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion server/src/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ impl DBClient {
}

pub async fn delete_db(&self) {
self.database.drop(None).await.expect("couldn't drop db")
self.database.drop(None).await.expect("couldn't drop db");
}
}
2 changes: 1 addition & 1 deletion server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub mod handlers;
pub mod models;
pub mod repository;

pub mod test_util;
pub mod test_util;
1 change: 0 additions & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,3 @@ fn random_password() -> String {
}
pw
}

36 changes: 18 additions & 18 deletions server/src/test_util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::db_connection::DBClient;
use uuid::Uuid;
use tokio::runtime::Runtime;
use std::env;
use std::future::Future;
use std::panic::resume_unwind;
use tokio::runtime::Runtime;
use uuid::Uuid;

/// Use this function to temporarily crate a mongodb database
/// given the env var `TEST_DB_URI` or `DB_URI` (preferring `TEST_DB_URI`).
Expand All @@ -13,18 +13,16 @@ use std::panic::resume_unwind;
/// To ensure `should_panic` tests pass when no database URI is provided, call [`test_db_panic`]
#[allow(unused)]
pub fn test_db<F, Res>(f: F) -> bool
where
F: 'static + Send + FnOnce(DBClient) -> Res,
Res: Future<Output=()> + Send,
where
F: 'static + Send + FnOnce(DBClient) -> Res,
Res: Future<Output = ()> + Send,
{
let _ignored = dotenv::dotenv();

let name = format!("test_db_{}", Uuid::new_v4());

let client_uri = if let Ok(client_uri) = env::var("TEST_DB_URI")
.or_else(|_| {
env::var("DB_URI")
}) {
let client_uri = if let Ok(client_uri) = env::var("TEST_DB_URI").or_else(|_| env::var("DB_URI"))
{
client_uri
} else {
eprintln!("WARNING: NOT RUNNING TEST DUE TO LACK OF DATABASE URI");
Expand All @@ -33,14 +31,15 @@ pub fn test_db<F, Res>(f: F) -> bool

let rt = Runtime::new().expect("create runtime");
rt.block_on(async move {
let client = DBClient::new(client_uri, name).await
let client = DBClient::new(client_uri, name)
.await
.expect("start mongodb client");


let local_client = client.clone();
let res = tokio::spawn(async move {
f(local_client).await
}).await;
f(local_client).await;
})
.await;

client.delete_db().await;

Expand All @@ -56,12 +55,13 @@ pub fn test_db<F, Res>(f: F) -> bool
}

/// Panics when tests could not be run due to the lack of db uri. This is to
/// make sure should_panic tests stay working without a uri.
/// make sure `should_panic` tests stay working without a uri.
#[allow(unused)]
#[allow(clippy::missing_panics_doc)]
pub fn test_db_panic<F, Res>(f: F)
where
F: 'static + Send + FnOnce(DBClient) -> Res,
Res: Future<Output=()> + Send,
where
F: 'static + Send + FnOnce(DBClient) -> Res,
Res: Future<Output = ()> + Send,
{
if !test_db(f) {
panic!("no db found but test should panic");
Expand All @@ -79,4 +79,4 @@ mod tests {
panic!("test");
});
}
}
}

0 comments on commit aaaf35b

Please sign in to comment.