mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-01-06 16:51:40 +00:00
Re-arrange tests for postgres connection
This commit is contained in:
parent
1b855f581b
commit
5b6c4ad8fa
@ -93,3 +93,81 @@ impl RawConnection for PostgresRawConnection {
|
||||
Ok(row)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::query::QueryParameters;
|
||||
use std::env;
|
||||
|
||||
fn database_url() -> String {
|
||||
env::var("POSTGRES_DATABASE_URL")
|
||||
.or_else(|_| env::var("DATABASE_URL"))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn it_establishes() -> crate::Result<()> {
|
||||
let mut conn = PostgresRawConnection::establish(&database_url()).await?;
|
||||
|
||||
// After establish, run PING to ensure that it was established correctly
|
||||
conn.ping().await?;
|
||||
|
||||
// Then explicitly close the connection
|
||||
conn.close().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn it_executes() -> crate::Result<()> {
|
||||
let mut conn = PostgresRawConnection::establish(&database_url()).await?;
|
||||
|
||||
let affected_rows_from_begin =
|
||||
RawConnection::execute(&mut conn, "BEGIN", PostgresQueryParameters::new()).await?;
|
||||
|
||||
assert_eq!(affected_rows_from_begin, 0);
|
||||
|
||||
let affected_rows_from_create_table = RawConnection::execute(
|
||||
&mut conn,
|
||||
r#"
|
||||
CREATE TEMP TABLE sqlx_test_it_executes (
|
||||
id BIGSERIAL PRIMARY KEY
|
||||
)
|
||||
"#,
|
||||
PostgresQueryParameters::new(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(affected_rows_from_create_table, 0);
|
||||
|
||||
for _ in 0..5_i32 {
|
||||
let affected_rows_from_insert = RawConnection::execute(
|
||||
&mut conn,
|
||||
"INSERT INTO sqlx_test_it_executes DEFAULT VALUES",
|
||||
PostgresQueryParameters::new(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(affected_rows_from_insert, 1);
|
||||
}
|
||||
|
||||
let affected_rows_from_delete = RawConnection::execute(
|
||||
&mut conn,
|
||||
"DELETE FROM sqlx_test_it_executes",
|
||||
PostgresQueryParameters::new(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(affected_rows_from_delete, 5);
|
||||
|
||||
let affected_rows_from_rollback =
|
||||
RawConnection::execute(&mut conn, "ROLLBACK", PostgresQueryParameters::new()).await?;
|
||||
|
||||
assert_eq!(affected_rows_from_rollback, 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,124 +11,3 @@ pub use self::{
|
||||
backend::Postgres, error::PostgresDatabaseError, query::PostgresQueryParameters,
|
||||
raw::PostgresRawConnection, row::PostgresRow,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Postgres;
|
||||
use crate::connection::Connection;
|
||||
use futures_util::TryStreamExt;
|
||||
|
||||
const DATABASE_URL: &str = "postgres://postgres@127.0.0.1:5432/";
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_connects() {
|
||||
let _conn = Connection::<Postgres>::establish(DATABASE_URL)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_pings() {
|
||||
let conn = Connection::<Postgres>::establish(DATABASE_URL)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
conn.ping().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_fails_on_connect_with_an_unknown_user() {
|
||||
let res = Connection::<Postgres>::establish("postgres://not_a_user@127.0.0.1:5432/").await;
|
||||
|
||||
match res {
|
||||
Err(crate::Error::Database(err)) => {
|
||||
assert_eq!(err.message(), "role \"not_a_user\" does not exist");
|
||||
}
|
||||
|
||||
_ => panic!("unexpected result"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_fails_on_connect_with_an_unknown_database() {
|
||||
let res =
|
||||
Connection::<Postgres>::establish("postgres://postgres@127.0.0.1:5432/fdggsdfgsdaf")
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Err(crate::Error::Database(err)) => {
|
||||
assert_eq!(err.message(), "database \"fdggsdfgsdaf\" does not exist");
|
||||
}
|
||||
|
||||
_ => panic!("unexpected result"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_fetches_tuples_from_system_roles() {
|
||||
let conn = Connection::<Postgres>::establish(DATABASE_URL)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let roles: Vec<(String, bool)> = crate::query("SELECT rolname, rolsuper FROM pg_roles")
|
||||
.fetch(&conn)
|
||||
.try_collect()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Sanity check to be sure we did indeed fetch tuples
|
||||
assert!(roles.binary_search(&("postgres".to_string(), true)).is_ok());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_fetches_nothing_for_no_rows_from_system_roles() {
|
||||
let conn = Connection::<Postgres>::establish(DATABASE_URL)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let res: Option<(String, bool)> =
|
||||
crate::query("SELECT rolname, rolsuper FROM pg_roles WHERE rolname = 'not-a-user'")
|
||||
.fetch_optional(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(res.is_none());
|
||||
|
||||
let res: crate::Result<(String, bool)> =
|
||||
crate::query("SELECT rolname, rolsuper FROM pg_roles WHERE rolname = 'not-a-user'")
|
||||
.fetch_one(&conn)
|
||||
.await;
|
||||
|
||||
matches::assert_matches!(res, Err(crate::Error::NotFound));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_errors_on_fetching_more_than_one_row_from_system_roles() {
|
||||
let conn = Connection::<Postgres>::establish(DATABASE_URL)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let res: crate::Result<(String, bool)> =
|
||||
crate::query("SELECT rolname, rolsuper FROM pg_roles")
|
||||
.fetch_one(&conn)
|
||||
.await;
|
||||
|
||||
matches::assert_matches!(res, Err(crate::Error::FoundMoreThanOne));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_fetches_one_row_from_system_roles() {
|
||||
let conn = Connection::<Postgres>::establish(DATABASE_URL)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let res: (String, bool) =
|
||||
crate::query("SELECT rolname, rolsuper FROM pg_roles WHERE rolname = 'postgres'")
|
||||
.fetch_one(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(res.0, "postgres");
|
||||
assert!(res.1);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user