sqlx/tests/postgres.rs
Daniel Akhterov db230e2ce0 Resolve Comments
- Remove `hex` from root `Cargo.toml`
    - Make `hmac` crate optional
    - Clean up checking mechanisms for "SCRAM-SHA-256"
    - Use `str::from_utf8` instead of `String::from_utf8_lossyf
    - Update `Sasl*Response` structs be tuple structs
    - Factor out `len` in `SaslInitialResponse.encode()`
    - Use `protocol_err` instead of `expect` when constructing `Hmacf
      instances
    - Remove `it_connects_to_database_user` test as it was too fragile
    - Move `sasl_auth` function into `postgres/connection` as it more
      related to `Connection` rather than `protocl`
    - Return an error when decoding base64 salt rather than panicing
      in `Authentication::SaslContinue`
2020-01-11 03:33:38 -08:00

71 lines
1.7 KiB
Rust

use futures::TryStreamExt;
use sqlx::{postgres::PgConnection, Connection as _, Executor as _, Row as _};
#[async_std::test]
async fn it_connects() -> anyhow::Result<()> {
let mut conn = connect().await?;
let row = sqlx::query("select 1 + 1").fetch_one(&mut conn).await?;
assert_eq!(2, row.get(0));
conn.close().await?;
Ok(())
}
#[async_std::test]
async fn it_executes() -> anyhow::Result<()> {
let mut conn = connect().await?;
let _ = conn
.send(
r#"
CREATE TEMPORARY TABLE users (id INTEGER PRIMARY KEY);
"#,
)
.await?;
for index in 1..=10_i32 {
let cnt = sqlx::query("INSERT INTO users (id) VALUES ($1)")
.bind(index)
.execute(&mut conn)
.await?;
assert_eq!(cnt, 1);
}
let sum: i32 = sqlx::query("SELECT id FROM users")
.fetch(&mut conn)
.try_fold(
0_i32,
|acc, x| async move { Ok(acc + x.get::<i32, _>("id")) },
)
.await?;
assert_eq!(sum, 55);
Ok(())
}
#[async_std::test]
async fn it_remains_stable_issue_30() -> anyhow::Result<()> {
let mut conn = connect().await?;
// This tests the internal buffer wrapping around at the end
// Specifically: https://github.com/launchbadge/sqlx/issues/30
let rows = sqlx::query("SELECT i, random()::text FROM generate_series(1, 1000) as i")
.fetch_all(&mut conn)
.await?;
assert_eq!(rows.len(), 1000);
assert_eq!(rows[rows.len() - 1].get::<i32, _>(0), 1000);
Ok(())
}
async fn connect() -> anyhow::Result<PgConnection> {
Ok(PgConnection::open(dotenv::var("DATABASE_URL")?).await?)
}