From 1350bfb822bbdf28d6a9e09f0690643f063b9181 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 11 Jan 2020 01:00:13 -0800 Subject: [PATCH] Add test for issue #40 --- tests/mysql.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/tests/mysql.rs b/tests/mysql.rs index a53526a7..b13741bd 100644 --- a/tests/mysql.rs +++ b/tests/mysql.rs @@ -1,5 +1,5 @@ use futures::TryStreamExt; -use sqlx::{mysql::MySqlConnection, Connection as _, Executor as _, Row as _}; +use sqlx::{Connection as _, Executor as _, MySqlConnection, MySqlPool, Row as _}; #[async_std::test] async fn it_connects() -> anyhow::Result<()> { @@ -48,6 +48,28 @@ CREATE TEMPORARY TABLE users (id INTEGER PRIMARY KEY) Ok(()) } +#[async_std::test] +async fn pool_immediately_fails_with_db_error() -> anyhow::Result<()> { + // Malform the database url by changing the password + let url = url()?.replace("password", "not-the-password"); + + let pool = MySqlPool::new(&url).await?; + + let res = pool.acquire().await; + + match res { + Err(sqlx::Error::Database(err)) if err.message().contains("Access denied") => { + // Access was properly denied + } + + Err(e) => panic!("unexpected error: {:?}", e), + + Ok(_) => panic!("unexpected ok"), + } + + Ok(()) +} + #[cfg(feature = "macros")] #[async_std::test] async fn macro_select_from_cte() -> anyhow::Result<()> { @@ -65,6 +87,10 @@ async fn macro_select_from_cte() -> anyhow::Result<()> { Ok(()) } -async fn connect() -> anyhow::Result { - Ok(MySqlConnection::open(dotenv::var("DATABASE_URL")?).await?) +fn url() -> anyhow::Result { + Ok(dotenv::var("DATABASE_URL")?) +} + +async fn connect() -> anyhow::Result { + Ok(MySqlConnection::open(url()?).await?) }