diff --git a/src/starter/src/lib.rs b/src/starter/src/lib.rs index 7ff5d40..9a0c92c 100644 --- a/src/starter/src/lib.rs +++ b/src/starter/src/lib.rs @@ -6,13 +6,15 @@ use deadpool_postgres::tokio_postgres::NoTls; mod settings; pub async fn run() -> Result { - let _env_logger = env_logger::try_init_from_env(env_logger::Env::new().default_filter_or("info")); + let _env_logger = + env_logger::try_init_from_env(env_logger::Env::new().default_filter_or("info")); let settings = Settings::new().unwrap(); run_internal(&settings).await } pub async fn run_with_config(path: &str) -> Result { - let _env_logger = env_logger::try_init_from_env(env_logger::Env::new().default_filter_or("info")); + let _env_logger = + env_logger::try_init_from_env(env_logger::Env::new().default_filter_or("info")); let settings = Settings::with_path(path).unwrap(); run_internal(&settings).await } diff --git a/tests/integration/tests/integration.rs b/tests/integration/tests/integration.rs index 769a952..435c388 100644 --- a/tests/integration/tests/integration.rs +++ b/tests/integration/tests/integration.rs @@ -3,21 +3,22 @@ extern crate starter; #[cfg(test)] mod tests { - use std::collections::HashMap; - use serial_test::serial; + use serial_test::serial; + use std::collections::HashMap; use testcontainers::{clients, RunnableImage}; use testcontainers_modules::postgres; use uuid::Uuid; const CONFIG_FILE_PATH: &str = "./../../"; + #[macro_export] macro_rules! prepare_test_container { - () => ( + () => { let docker = clients::Cli::default(); let image = RunnableImage::from(postgres::Postgres::default()) - .with_tag("15-alpine") - .with_mapped_port((5432, 5432)); + .with_tag("15-alpine") + .with_mapped_port((5432, 5432)); let _node = docker.run(image); @@ -38,12 +39,7 @@ mod tests { .query("CREATE DATABASE rust_template_db;", &[]) .await .unwrap(); - - let server = starter::run_with_config(&CONFIG_FILE_PATH) - .await - .expect("Failed to bind address"); - let _ = tokio::spawn(server); - ) + }; } #[serial] @@ -51,6 +47,11 @@ mod tests { async fn test_get_all() { prepare_test_container!(); + let server = starter::run_with_config(&CONFIG_FILE_PATH) + .await + .expect("Failed to bind address"); + let _server_task = tokio::spawn(server); + let client = reqwest::Client::new(); // Act @@ -62,13 +63,20 @@ mod tests { // Assert assert!(response.status().is_success()); + + _server_task.abort(); } #[serial] #[tokio::test] - async fn test_create() { + async fn test_create() { prepare_test_container!(); + let server = starter::run_with_config(&CONFIG_FILE_PATH) + .await + .expect("Failed to bind address"); + let _server_task = tokio::spawn(server); + let client = reqwest::Client::new(); let mut map = HashMap::new(); map.insert("title", "title1"); @@ -84,6 +92,8 @@ mod tests { // Assert assert!(response.status().is_success()); + + _server_task.abort(); } #[serial] @@ -91,6 +101,11 @@ mod tests { async fn test_update() { prepare_test_container!(); + let server = starter::run_with_config(&CONFIG_FILE_PATH) + .await + .expect("Failed to bind address"); + let _server_task = tokio::spawn(server); + let client = reqwest::Client::new(); let mut map_create = HashMap::new(); map_create.insert("title", "title1"); @@ -121,6 +136,8 @@ mod tests { // Assert assert!(response.status().is_success()); + + _server_task.abort(); } #[serial] @@ -128,6 +145,11 @@ mod tests { async fn test_get_by_id() { prepare_test_container!(); + let server = starter::run_with_config(&CONFIG_FILE_PATH) + .await + .expect("Failed to bind address"); + let _server_task = tokio::spawn(server); + let client = reqwest::Client::new(); let mut map_create = HashMap::new(); map_create.insert("title", "title1"); @@ -152,6 +174,8 @@ mod tests { // Assert assert!(response.status().is_success()); + + _server_task.abort(); } #[serial] @@ -159,6 +183,11 @@ mod tests { async fn test_delete() { prepare_test_container!(); + let server = starter::run_with_config(&CONFIG_FILE_PATH) + .await + .expect("Failed to bind address"); + let _server_task = tokio::spawn(server); + let client = reqwest::Client::new(); let mut map_create = HashMap::new(); map_create.insert("title", "title1"); @@ -175,7 +204,6 @@ mod tests { .await .expect("Failed to deserialize response."); - let response = client .delete(format!("http://127.0.0.1:8181/to-do-items/{id}", id = id)) .send() @@ -184,5 +212,7 @@ mod tests { // Assert assert!(response.status().is_success()); + + _server_task.abort(); } }