refactor: changes

This commit is contained in:
Sandro Eiler 2024-01-21 22:00:10 +01:00
parent 476eed4559
commit f4deaceb27
7 changed files with 137 additions and 12 deletions

View file

@ -1,6 +1,7 @@
use std::time::Duration;
use learn_axum::configuration::get_configuration;
use sqlx::{Connection, PgConnection};
use std::net::SocketAddr;
use sqlx::{Connection, PgConnection, postgres::PgPoolOptions};
use tokio::net::TcpListener;
struct TestApp {
@ -33,7 +34,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
let connection_string = configuration.database.connection_string();
// The `Connection` trait MUST be in scope for us to invoke
// `PgConnection::connect` - it is not an inherent method of the struct!
let connection = PgConnection::connect(&connection_string)
let mut connection = PgConnection::connect(&connection_string)
.await
.expect("Failed to connect to Postgres.");
let client = reqwest::Client::new();
@ -50,6 +51,14 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
// Assert
assert_eq!(200, response.status().as_u16());
let saved = sqlx::query!("SELECT email, name FROM subscriptions",)
.fetch_one(&mut connection)
.await
.expect("Failed to fetch saved subscription.");
assert_eq!(saved.email, "ursula_le_guin@gmail.com");
assert_eq!(saved.name, "le guin");
}
#[tokio::test]
@ -87,8 +96,16 @@ async fn spawn_app() -> TestApp {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = format!("http://{}", listener.local_addr().unwrap());
let configuration = get_configuration().expect("Failed to read configuration.");
let pool = PgPoolOptions::new()
.max_connections(5)
.acquire_timeout(Duration::from_secs(3))
.connect(&configuration.database.connection_string())
.await
.expect("can't connect to database");
tokio::spawn(async move {
axum::serve(listener, learn_axum::startup::app())
axum::serve(listener, learn_axum::startup::app(pool))
.await
.unwrap();
});