insert subscription, db test isolation
This commit is contained in:
@ -1,26 +1,55 @@
|
||||
use sqlx::{postgres::PgPoolOptions, query, Connection, PgConnection, Pool, Postgres};
|
||||
use sqlx::{postgres::PgPoolOptions, query, Executor, Pool, Postgres};
|
||||
use uuid::Uuid;
|
||||
use std::net::TcpListener;
|
||||
|
||||
use zero2prod::configuration::get_configuration;
|
||||
use zero2prod::configuration::{get_configuration, DatabaseSettings};
|
||||
|
||||
fn spawn_app(connection: Pool<Postgres>) -> String {
|
||||
pub struct TestApp {
|
||||
pub address: String,
|
||||
pub connection_pool: Pool<Postgres>,
|
||||
}
|
||||
|
||||
async fn spawn_app() -> TestApp {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let address = format!("http://127.0.0.1:{}", port);
|
||||
|
||||
let server = zero2prod::startup::run(listener, connection).expect("Failed to bind address");
|
||||
tokio::spawn(server);
|
||||
let mut config = get_configuration().expect("Failed to read configuration");
|
||||
config.database.database_name = Uuid::new_v4().to_string();
|
||||
let connection_pool = configure_database(&config.database).await;
|
||||
|
||||
format!("http://127.0.0.1:{}", port)
|
||||
let server = zero2prod::startup::run(listener, connection_pool.clone()).expect("Failed to bind address");
|
||||
let _ = tokio::spawn(server);
|
||||
|
||||
TestApp {
|
||||
address,
|
||||
connection_pool,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn configure_database(config: &DatabaseSettings) -> Pool<Postgres> {
|
||||
let connection_pool = PgPoolOptions::new()
|
||||
.max_connections(10)
|
||||
.connect(&config.connection_string_without_db())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
|
||||
connection_pool.execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_str())
|
||||
.await
|
||||
.expect("Failed to create database.");
|
||||
|
||||
sqlx::migrate!("./migrations")
|
||||
.run(&connection_pool)
|
||||
.await
|
||||
.expect("Failed to run migrations.");
|
||||
|
||||
connection_pool
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_check_works() {
|
||||
let config = get_configuration().expect("Failed to read configuration");
|
||||
let connection = PgPoolOptions::new()
|
||||
.max_connections(10).connect(&config.database.connection_string()).await.expect("Failed to connect to Postgres.");
|
||||
|
||||
let address = spawn_app(connection);
|
||||
let health_check_endpoint = format!("{}/health_check", address);
|
||||
let app = spawn_app().await;
|
||||
let health_check_endpoint = format!("{}/health_check", app.address);
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get(health_check_endpoint)
|
||||
@ -34,19 +63,12 @@ async fn health_check_works() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
let config = get_configuration().expect("Failed to read configuration");
|
||||
let connection = PgPoolOptions::new()
|
||||
.max_connections(10).connect(&config.database.connection_string()).await.expect("Failed to connect to Postgres.");
|
||||
|
||||
let app_address = spawn_app(connection);
|
||||
|
||||
let mut connection = PgConnection::connect(&config.database.connection_string()).await.expect("Failed to connect to Postgres.");
|
||||
|
||||
let app = spawn_app().await;
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
||||
let response = client
|
||||
.post(&format!("{}/subscriptions", &app_address))
|
||||
.post(&format!("{}/subscriptions", &app.address))
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(body)
|
||||
.send()
|
||||
@ -56,7 +78,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
assert_eq!(200, response.status().as_u16());
|
||||
|
||||
let saved = query!("SELECT email, name FROM subscriptions",)
|
||||
.fetch_one(&mut connection)
|
||||
.fetch_one(&app.connection_pool)
|
||||
.await
|
||||
.expect("Failed to fetch saved subscription.");
|
||||
|
||||
@ -66,11 +88,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn subscribe_returns_a_400_when_data_is_missing() {
|
||||
let config = get_configuration().expect("Failed to read configuration");
|
||||
let connection = PgPoolOptions::new()
|
||||
.max_connections(10).connect(&config.database.connection_string()).await.expect("Failed to connect to Postgres.");
|
||||
|
||||
let app_address = spawn_app(connection);
|
||||
let app = spawn_app().await;
|
||||
let client = reqwest::Client::new();
|
||||
let test_cases = vec![
|
||||
("name=le%20guin", "missing the email"),
|
||||
@ -80,7 +98,7 @@ async fn subscribe_returns_a_400_when_data_is_missing() {
|
||||
|
||||
for (invalid_body, error_message) in test_cases {
|
||||
let response = client
|
||||
.post(&format!("{}/subscriptions", &app_address))
|
||||
.post(&format!("{}/subscriptions", &app.address))
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(invalid_body)
|
||||
.send()
|
||||
|
||||
Reference in New Issue
Block a user