mirror of
https://github.com/kristoferssolo/zero2prod.git
synced 2025-10-21 20:10:40 +00:00
94 lines
2.5 KiB
Rust
94 lines
2.5 KiB
Rust
use chrono::format;
|
|
use once_cell::sync::Lazy;
|
|
use reqwest::{Client, Response};
|
|
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
|
use uuid::Uuid;
|
|
use zero2prod::{
|
|
config::{get_config, DatabaseSettings},
|
|
startup::{get_connection_pool, Application},
|
|
telemetry::{get_subscriber, init_subscriber},
|
|
};
|
|
|
|
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,
|
|
}
|
|
|
|
impl TestApp {
|
|
pub async fn post_subscription(&self, body: String) -> Response {
|
|
Client::new()
|
|
.post(&format!("{}/subscriptions", &self.address))
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
.body(body)
|
|
.send()
|
|
.await
|
|
.expect("Failed to execute request.")
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|