From 46ac102adcd37db1bb3f822ea4a784a91acd6c85 Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Fri, 3 May 2024 17:10:39 -0400 Subject: [PATCH] feat(test): connecting to PostgreSQL - rename the database name in the configuration.yaml --- configuration.yaml | 2 +- src/configuration.rs | 9 +++++++++ tests/subscribe.rs | 11 +++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/configuration.yaml b/configuration.yaml index 38c68d4..85a79ac 100644 --- a/configuration.yaml +++ b/configuration.yaml @@ -4,4 +4,4 @@ database: port: 5432 username: "postgres" password: "password" - database_name: "email_newsletter_api" + database_name: "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/subscribe.rs b/tests/subscribe.rs index ecd8c1d..c57927b 100644 --- a/tests/subscribe.rs +++ b/tests/subscribe.rs @@ -2,10 +2,21 @@ mod test_utils; use test_utils::spawn_app; +use email_newsletter_api::configuration::{self, get_configuration}; +use sqlx::{Connection, PgConnection}; + #[tokio::test] async fn subscribe_returns_a_200_for_valid_form_data() { let server_address = spawn_app(); + let configuration = get_configuration().expect("Failed to read configuration"); + + let postgres_connection_string = configuration.database.connection_string(); + + let connection = PgConnection::connect(&postgres_connection_string) + .await + .expect("Failed to connect to Postgres"); + let client = reqwest::Client::new(); let body = "name=le%20test&email=le_test%40gmail.com";