diff --git a/.env b/.env new file mode 100644 index 0000000..88cfb53 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter" diff --git a/src/configuration.rs b/src/configuration.rs index db47c69..0ad4a22 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -23,3 +23,12 @@ pub fn get_configuration() -> Result { settings.try_deserialize::() } + +impl DatabaseSettings { + pub fn connection_string(&self) -> String { + format!( + "postgres://{}:{}@{}:{}/{}", + self.username, self.password, self.host, self.port, self.database_name + ) + } +} diff --git a/tests/health_check.rs b/tests/health_check.rs index 461230a..c937148 100644 --- a/tests/health_check.rs +++ b/tests/health_check.rs @@ -1,5 +1,8 @@ use std::net::TcpListener; +use sqlx::{Connection, PgConnection}; +use zero2prod::configuration::get_configuration; + #[tokio::test] async fn health_check_works() { // Arrange @@ -22,6 +25,11 @@ async fn health_check_works() { async fn subscribe_returns_a_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(); + let mut connection = PgConnection::connect(&connection_string) + .await + .expect("Failed to connect to Postgres."); let client = reqwest::Client::new(); // Act @@ -36,6 +44,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]