feat(test): connecting to PostgreSQL

- rename the database name in the configuration.yaml
This commit is contained in:
minhtrannhat 2024-05-03 17:10:39 -04:00
parent 38f9646526
commit 46ac102adc
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
3 changed files with 21 additions and 1 deletions

View File

@ -4,4 +4,4 @@ database:
port: 5432
username: "postgres"
password: "password"
database_name: "email_newsletter_api"
database_name: "newsletter"

View File

@ -23,3 +23,12 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
settings.try_deserialize::<Settings>()
}
impl DatabaseSettings {
pub fn connection_string(&self) -> String {
format!(
"postgres://{}:{}@{}:{}/{}",
self.username, self.password, self.host, self.port, self.database_name
)
}
}

View File

@ -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";