Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nkz-soft committed Apr 29, 2024
1 parent c7f3dea commit 21cd0a5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/starter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use deadpool_postgres::tokio_postgres::NoTls;
mod settings;

pub async fn run() -> Result<Server, std::io::Error> {
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<Server, std::io::Error> {
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
}
Expand Down
56 changes: 43 additions & 13 deletions tests/integration/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -38,19 +39,19 @@ 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]
#[tokio::test]
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
Expand All @@ -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");
Expand All @@ -84,13 +92,20 @@ mod tests {

// Assert
assert!(response.status().is_success());

_server_task.abort();
}

#[serial]
#[tokio::test]
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");
Expand Down Expand Up @@ -121,13 +136,20 @@ mod tests {

// Assert
assert!(response.status().is_success());

_server_task.abort();
}

#[serial]
#[tokio::test]
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");
Expand All @@ -152,13 +174,20 @@ mod tests {

// Assert
assert!(response.status().is_success());

_server_task.abort();
}

#[serial]
#[tokio::test]
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");
Expand All @@ -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()
Expand All @@ -184,5 +212,7 @@ mod tests {

// Assert
assert!(response.status().is_success());

_server_task.abort();
}
}

0 comments on commit 21cd0a5

Please sign in to comment.