feat(test): spin up new logical database tests

- Tests will use new database every run
- Added chrono and uuid dependencies.
- Updated documentation
This commit is contained in:
minhtrannhat 2024-05-04 15:27:47 -04:00
parent 1c317e3f34
commit 5e6e9c2efe
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
10 changed files with 106 additions and 26 deletions

5
Cargo.lock generated
View File

@ -611,11 +611,13 @@ name = "email_newsletter_api"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"chrono",
"config", "config",
"reqwest", "reqwest",
"serde", "serde",
"sqlx", "sqlx",
"tokio", "tokio",
"uuid",
] ]
[[package]] [[package]]
@ -2609,6 +2611,9 @@ name = "uuid"
version = "1.8.0" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"

View File

@ -20,6 +20,8 @@ reqwest = "0.12.2"
serde = { version = "1.0.197", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] }
tokio = { version = "1.36.0", features = ["full"] } tokio = { version = "1.36.0", features = ["full"] }
config = "0.13" config = "0.13"
uuid = { version = "1.8.0", features = ["v4"] }
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
[dependencies.sqlx] [dependencies.sqlx]
version = "0.7" version = "0.7"

View File

@ -8,3 +8,5 @@ When we set up our web app, we can attach a resource to the app with `app_data`.
In our app, we want to inject a `db_conn` to the route handlers, so that these routes can handle PostgreSQL read/write. In our app, we want to inject a `db_conn` to the route handlers, so that these routes can handle PostgreSQL read/write.
Since the database connection is a TCP connection that is NOT `Clone`-able. We use Rust's `Arc` (Atomic Reference Counter) as a wrapper around this connection. Each instance of our web app, instead of getting a raw TCP connection to the PostgreSQL database, will be given the pointer to the memory region of `db_conn`.

View File

@ -31,4 +31,11 @@ impl DatabaseSettings {
self.username, self.password, self.host, self.port, self.database_name self.username, self.password, self.host, self.port, self.database_name
) )
} }
pub fn connection_string_without_db(&self) -> String {
format!(
"postgres://{}:{}@{}:{}",
self.username, self.password, self.host, self.port
)
}
} }

View File

@ -1,11 +1,16 @@
use std::net::TcpListener; use std::net::TcpListener;
use email_newsletter_api::{configuration::get_configuration, startup}; use email_newsletter_api::{configuration::get_configuration, startup};
use sqlx::PgPool;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), std::io::Error> { async fn main() -> Result<(), std::io::Error> {
let configuration = get_configuration().expect("Failed to read configuration"); let configuration = get_configuration().expect("Failed to read configuration");
let db_conn = PgPool::connect(&configuration.database.connection_string())
.await
.expect("Failed to connect to PostgreSQL");
let port_number = configuration.application_port; let port_number = configuration.application_port;
let listener = TcpListener::bind(format!("127.0.0.1:{}", port_number)) let listener = TcpListener::bind(format!("127.0.0.1:{}", port_number))
@ -13,5 +18,5 @@ async fn main() -> Result<(), std::io::Error> {
// Move the error up the call stack // Move the error up the call stack
// otherwise await for the HttpServer // otherwise await for the HttpServer
startup::run(listener)?.await startup::run(listener, db_conn)?.await
} }

View File

@ -1,4 +1,7 @@
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use chrono::Utc;
use sqlx::PgPool;
use uuid::Uuid;
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
pub struct FormData { pub struct FormData {
@ -6,6 +9,27 @@ pub struct FormData {
name: String, name: String,
} }
pub async fn subscribe_route(_form: web::Form<FormData>) -> HttpResponse { pub async fn subscribe_route(
HttpResponse::Ok().finish() form: web::Form<FormData>,
db_conn_pool: web::Data<PgPool>,
) -> HttpResponse {
match sqlx::query!(
r#"
INSERT INTO subscriptions (id, email, name, subscribed_at)
VALUES ($1, $2, $3, $4)
"#,
Uuid::new_v4(),
form.email,
form.name,
Utc::now()
)
.execute(db_conn_pool.get_ref())
.await
{
Ok(_) => HttpResponse::Ok().finish(),
Err(e) => {
println!("Failed to execute query: {}", e);
HttpResponse::InternalServerError().finish()
}
}
} }

View File

@ -1,13 +1,19 @@
use crate::routes::{healthcheck_route, subscribe_route}; use crate::routes::{healthcheck_route, subscribe_route};
use actix_web::dev::Server; use actix_web::dev::Server;
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use sqlx::PgPool;
use std::net::TcpListener; use std::net::TcpListener;
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> { pub fn run(listener: TcpListener, db_conn_pool: PgPool) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| { // under the hood, web::Data::new will create an Arc
// to make the TCP connection to PostgreSQL clone-able
let db_conn_pool = web::Data::new(db_conn_pool);
let server = HttpServer::new(move || {
App::new() App::new()
.route("/health_check", web::get().to(healthcheck_route)) .route("/health_check", web::get().to(healthcheck_route))
.route("/subscribe", web::post().to(subscribe_route)) .route("/subscribe", web::post().to(subscribe_route))
.app_data(db_conn_pool.clone())
}) })
.listen(listener)? .listen(listener)?
.run(); .run();

View File

@ -4,12 +4,12 @@ use test_utils::spawn_app;
#[tokio::test] #[tokio::test]
async fn health_check_works() { async fn health_check_works() {
let server_address = spawn_app(); let test_app = spawn_app().await;
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let response = client let response = client
.get(&format!("{}/health_check", &server_address)) .get(&format!("{}/health_check", &test_app.address))
.send() .send()
.await .await
.expect("Failed to execute health_check request."); .expect("Failed to execute health_check request.");

View File

@ -1,27 +1,17 @@
mod test_utils; mod test_utils;
use email_newsletter_api::configuration::{self, get_configuration};
use sqlx::{Connection, PgConnection};
use test_utils::spawn_app; use test_utils::spawn_app;
#[tokio::test] #[tokio::test]
async fn subscribe_returns_a_200_for_valid_form_data() { async fn subscribe_returns_a_200_for_valid_form_data() {
let server_address = spawn_app(); let test_app = spawn_app().await;
let configuration = get_configuration().expect("Failed to read configuration");
let postgres_connection_string = configuration.database.connection_string();
let mut connection = PgConnection::connect(&postgres_connection_string)
.await
.expect("Failed to connect to Postgres");
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let body = "name=le%20test&email=le_test%40gmail.com"; let body = "name=le%20test&email=le_test%40gmail.com";
let response = client let response = client
.post(&format!("{}/subscribe", &server_address)) .post(&format!("{}/subscribe", &test_app.address))
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.body(body) .body(body)
.send() .send()
@ -32,7 +22,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
assert_eq!(Some(0), response.content_length()); assert_eq!(Some(0), response.content_length());
let saved = sqlx::query!("SELECT email, name FROM subscriptions") let saved = sqlx::query!("SELECT email, name FROM subscriptions")
.fetch_one(&mut connection) .fetch_one(&test_app.db_pool)
.await .await
.expect("Failed to fetch saved subscribtions"); .expect("Failed to fetch saved subscribtions");
@ -42,7 +32,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
#[tokio::test] #[tokio::test]
async fn subscribe_returns_a_400_when_data_is_missing() { async fn subscribe_returns_a_400_when_data_is_missing() {
let server_address = spawn_app(); let test_app = spawn_app().await;
let client = reqwest::Client::new(); let client = reqwest::Client::new();
@ -54,7 +44,7 @@ async fn subscribe_returns_a_400_when_data_is_missing() {
for (invalid_body, error_message) in test_cases { for (invalid_body, error_message) in test_cases {
let response = client let response = client
.post(&format!("{}/subscribe", &server_address)) .post(&format!("{}/subscribe", &test_app.address))
.header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body) .body(invalid_body)
.send() .send()

View File

@ -1,17 +1,31 @@
use email_newsletter_api::configuration::{get_configuration, DatabaseSettings};
use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener; use std::net::TcpListener;
use uuid::Uuid;
pub struct TestApp {
pub address: String,
pub db_pool: PgPool,
}
#[allow(dead_code)]
#[allow(clippy::let_underscore_future)] #[allow(clippy::let_underscore_future)]
pub fn spawn_app() -> String { pub async fn spawn_app() -> TestApp {
/* Spawn a app server with a TcpListener bound to localhost:<random port> /* Spawn a app server with a TcpListener bound to localhost:<random port>
* *
* Returns a valid IPv4 string (i.e localhost:8080) * Returns a valid IPv4 string (i.e localhost:8080)
*/ */
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to a random port"); let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to a random port");
let mut configuration = get_configuration().expect("Failed to read configuration");
configuration.database.database_name = Uuid::new_v4().to_string();
let db_conn_pool = configure_test_database(&configuration.database).await;
let port = listener.local_addr().unwrap().port(); let port = listener.local_addr().unwrap().port();
let server = email_newsletter_api::startup::run(listener).expect("Failed to bind address"); let server = email_newsletter_api::startup::run(listener, db_conn_pool.clone())
.expect("Failed to bind address");
/* `tokio::spawn(/*async task*/)` will spawn an async task to be run. /* `tokio::spawn(/*async task*/)` will spawn an async task to be run.
We can continue executing other code concurrently while `task` runs in the background. We can continue executing other code concurrently while `task` runs in the background.
@ -20,5 +34,30 @@ pub fn spawn_app() -> String {
(which `#[tokio::test]` will take care for us in the mean time).*/ (which `#[tokio::test]` will take care for us in the mean time).*/
let _ = tokio::spawn(server); let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port) TestApp {
address: format!("http://127.0.0.1:{}", port),
db_pool: db_conn_pool,
}
}
pub async fn configure_test_database(db_config: &DatabaseSettings) -> PgPool {
let mut connection = PgConnection::connect(&db_config.connection_string_without_db())
.await
.expect("Failed to connect to Postgres");
connection
.execute(format!(r#"CREATE DATABASE "{}";"#, db_config.database_name).as_str())
.await
.expect("Failed to create database");
let conn_pool = PgPool::connect(&db_config.connection_string())
.await
.expect("Failed to connect to PostgreSQL pool");
sqlx::migrate!("./migrations")
.run(&conn_pool)
.await
.expect("Failed to migrate the database");
conn_pool
} }