mirror of
https://github.com/kristoferssolo/axum-template.git
synced 2026-02-04 06:22:02 +00:00
feat: update dependencies
This commit is contained in:
17
tests/api/health_check.rs
Normal file
17
tests/api/health_check.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use crate::helpers::spawn_app;
|
||||
use reqwest::Client;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_check() {
|
||||
let app = spawn_app().await;
|
||||
let url = format!("{}/health_check", &app.address);
|
||||
let client = Client::new();
|
||||
let response = client
|
||||
.get(&url)
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request");
|
||||
|
||||
assert!(response.status().is_success());
|
||||
assert_eq!(Some(0), response.content_length());
|
||||
}
|
||||
79
tests/api/helpers.rs
Normal file
79
tests/api/helpers.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use {{crate_name}}::{
|
||||
config::{get_config, DatabaseSettings},
|
||||
middleware::telemetry::{get_subscriber, init_subscriber},
|
||||
startup::{get_connection_pool, Application},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||
use uuid::Uuid;
|
||||
|
||||
static TRACING: Lazy<()> = Lazy::new(|| {
|
||||
let default_filter_level = "trace";
|
||||
let subscriber_name = "test";
|
||||
if std::env::var("TEST_LOG").is_ok() {
|
||||
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::stdout);
|
||||
init_subscriber(subscriber);
|
||||
} else {
|
||||
let subscriber = get_subscriber(default_filter_level, subscriber_name, std::io::sink);
|
||||
init_subscriber(subscriber);
|
||||
}
|
||||
});
|
||||
|
||||
pub struct TestApp {
|
||||
pub address: String,
|
||||
pub pool: PgPool,
|
||||
}
|
||||
|
||||
pub async fn spawn_app() -> TestApp {
|
||||
Lazy::force(&TRACING);
|
||||
|
||||
let config = {
|
||||
let mut c = get_config().expect("Failed to read configuration.");
|
||||
c.database.database_name = Uuid::new_v4().to_string();
|
||||
c.application.port = 0;
|
||||
c
|
||||
};
|
||||
configure_database(&config.database).await;
|
||||
|
||||
let application = Application::build(config.clone())
|
||||
.await
|
||||
.expect("Failed to build application.");
|
||||
|
||||
let address = format!("http://127.0.0.1:{}", application.port());
|
||||
let _ = tokio::spawn(application.run_until_stopped());
|
||||
|
||||
TestApp {
|
||||
address,
|
||||
pool: get_connection_pool(&config.database),
|
||||
}
|
||||
}
|
||||
|
||||
async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
||||
let mut connection = PgConnection::connect_with(&config.without_db())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
|
||||
connection
|
||||
.execute(
|
||||
format!(
|
||||
r#"
|
||||
CREATE DATABASE "{}"
|
||||
"#,
|
||||
config.database_name
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.await
|
||||
.expect("Failed to create database.");
|
||||
|
||||
let pool = PgPool::connect_with(config.with_db())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
|
||||
sqlx::migrate!("./migrations")
|
||||
.run(&pool)
|
||||
.await
|
||||
.expect("Failed to migrate database");
|
||||
|
||||
pool
|
||||
}
|
||||
2
tests/api/main.rs
Normal file
2
tests/api/main.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod health_check;
|
||||
mod helpers;
|
||||
Reference in New Issue
Block a user