Skip to content

Commit

Permalink
Add .env file and check db in the /subscriptions happy path test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mulder90 committed Feb 12, 2024
1 parent 3e90601 commit 6c71f8b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter"
9 changes: 9 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
16 changes: 16 additions & 0 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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, "[email protected]");
assert_eq!(saved.name, "le guin");
}

#[tokio::test]
Expand Down

0 comments on commit 6c71f8b

Please sign in to comment.