Implement changes to allow database connection for insertions
Also updated tests to reflect the new PgPool format being used. Tests now create a mock database on creation to determine if it works before removing it. Up to Chapter 4.
This commit is contained in:
@@ -1,31 +1,68 @@
|
||||
use std::net::TcpListener;
|
||||
use mail_app::startup::run;
|
||||
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
||||
use uuid::Uuid;
|
||||
|
||||
use sqlx::{PgConnection, Connection};
|
||||
use mail_app::configuration::get_configuration;
|
||||
use mail_app::startup::run;
|
||||
use mail_app::configuration::{get_configuration, DatabaseSettings};
|
||||
|
||||
pub struct TestApp {
|
||||
pub address: String,
|
||||
pub db_pool: PgPool,
|
||||
}
|
||||
|
||||
// Create new instance of the application on a random port and return address [`http://localhost:XXXX`]
|
||||
fn spawn_app() -> String {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
|
||||
async fn spawn_app() -> TestApp {
|
||||
let listener = TcpListener::bind("127.0.0.1:0")
|
||||
.expect("Failed to bind to random port");
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let address = format!("http://127.0.0.1:{}", port);
|
||||
|
||||
let server = run(listener).expect("Failed to bind address");
|
||||
let mut configuration = get_configuration()
|
||||
.expect("Failed to read configuration.");
|
||||
configuration.database.database_name = Uuid::new_v4().to_string(); // Adjust database string to be random!
|
||||
|
||||
let connection_pool = configure_database(&configuration.database).await;
|
||||
|
||||
let server = run(listener, connection_pool.clone())
|
||||
.expect("Failed to bind address");
|
||||
let _ = tokio::spawn(server);
|
||||
TestApp {
|
||||
address,
|
||||
db_pool: connection_pool,
|
||||
}
|
||||
}
|
||||
|
||||
format!("http://127.0.0.1:{}", port)
|
||||
pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
||||
// Create database
|
||||
let mut connection = PgConnection::connect(&config.connection_string_without_db())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres");
|
||||
connection
|
||||
.execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
|
||||
.await
|
||||
.expect("Failed to create database.");
|
||||
|
||||
// Migrate database
|
||||
let connection_pool = PgPool::connect(&config.connection_string())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
sqlx::migrate!("./migrations")
|
||||
.run(&connection_pool)
|
||||
.await
|
||||
.expect("Failed to migrate the database");
|
||||
|
||||
connection_pool
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn health_check_works() {
|
||||
// Arrange
|
||||
let address = spawn_app();
|
||||
let app = spawn_app().await;
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
// Perform a 'reqwest' against endpoint
|
||||
|
||||
// Act
|
||||
let response = client
|
||||
.get(&format!("{}/health_check", &address))
|
||||
.get(&format!("{}/health_check", &app.address))
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request.");
|
||||
@@ -38,19 +75,13 @@ async fn health_check_works() {
|
||||
#[actix_rt::test]
|
||||
async fn subscribe_returns_200_for_valid_form_data() {
|
||||
// Arrange
|
||||
let app_address = spawn_app();
|
||||
let configuration = get_configuration().expect("Failed to read configuration.");
|
||||
let connection_string = configuration.database.connection_string();
|
||||
// Connection trait MUST be in scope to invoke.
|
||||
let mut connection = PgConnection::connect(&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=usrula_le_guin%40gmail.com";
|
||||
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
||||
|
||||
// Act
|
||||
let response = client
|
||||
.post(&format!("{}/subscriptions", &app_address))
|
||||
.post(&format!("{}/subscriptions", &app.address))
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.body(body)
|
||||
.send()
|
||||
@@ -61,7 +92,7 @@ async fn subscribe_returns_200_for_valid_form_data() {
|
||||
assert_eq!(200, response.status().as_u16());
|
||||
|
||||
let saved = sqlx::query!("SELECT email, name FROM subscriptions",)
|
||||
.fetch_one(&mut connection)
|
||||
.fetch_one(&app.db_pool)
|
||||
.await
|
||||
.expect("Failed to fetch saved subscription.");
|
||||
|
||||
@@ -70,9 +101,9 @@ async fn subscribe_returns_200_for_valid_form_data() {
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn subscrib_returns_400_for_missing_form_data() {
|
||||
async fn subscribe_returns_400_for_missing_form_data() {
|
||||
//Arrange
|
||||
let app_address = spawn_app();
|
||||
let app = spawn_app().await;
|
||||
let client = reqwest::Client::new();
|
||||
let test_cases = vec![
|
||||
("name=le%20guin", "missing the email"),
|
||||
@@ -83,7 +114,7 @@ async fn subscrib_returns_400_for_missing_form_data() {
|
||||
for (invalid_body, error_message) in test_cases {
|
||||
// Act
|
||||
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