implement test changes regarding database
also added .env for database connection details (used by sqlx)
This commit is contained in:
parent
782796486e
commit
6db0a7bc86
1
.env
Normal file
1
.env
Normal file
@ -0,0 +1 @@
|
|||||||
|
DATABASE_URL="postgres://postgres:password@localhost:5432/newsletter"
|
@ -23,3 +23,12 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
|||||||
// Try convert into Settings type
|
// Try convert into Settings type
|
||||||
settings.try_into()
|
settings.try_into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DatabaseSettings {
|
||||||
|
pub fn connection_string(&self) -> String {
|
||||||
|
format!(
|
||||||
|
"postgres://{}:{}@{}:{}/{}",
|
||||||
|
self.username, self.password, self.host, self.port, self.database_name
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use mail_app::startup::run;
|
use mail_app::startup::run;
|
||||||
|
|
||||||
|
use sqlx::{PgConnection, Connection};
|
||||||
|
use mail_app::configuration::get_configuration;
|
||||||
|
|
||||||
// Create new instance of the application on a random port and return address [`http://localhost:XXXX`]
|
// Create new instance of the application on a random port and return address [`http://localhost:XXXX`]
|
||||||
fn spawn_app() -> String {
|
fn spawn_app() -> String {
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
|
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
|
||||||
@ -36,6 +39,12 @@ async fn health_check_works() {
|
|||||||
async fn subscribe_returns_200_for_valid_form_data() {
|
async fn subscribe_returns_200_for_valid_form_data() {
|
||||||
// Arrange
|
// Arrange
|
||||||
let app_address = spawn_app();
|
let app_address = spawn_app();
|
||||||
|
let configuration = get_configuration().expect("Failed to read configuration.");
|
||||||
|
let connection_string = configuration.database.connection_string();
|
||||||
|
// Connection trait MUST be in scope to invoke.
|
||||||
|
let mut connection = PgConnection::connect(&connection_string)
|
||||||
|
.await
|
||||||
|
.expect("Failed to connect to Postgres.");
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let body = "name=le%20guin&email=usrula_le_guin%40gmail.com";
|
let body = "name=le%20guin&email=usrula_le_guin%40gmail.com";
|
||||||
|
|
||||||
@ -50,6 +59,14 @@ async fn subscribe_returns_200_for_valid_form_data() {
|
|||||||
|
|
||||||
// Assert test
|
// Assert test
|
||||||
assert_eq!(200, response.status().as_u16());
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
|
Loading…
Reference in New Issue
Block a user