mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-14 00:07:05 +00:00
0b2844bf39
closes #464
99 lines
2.1 KiB
Rust
99 lines
2.1 KiB
Rust
use sqlx::any::AnyRow;
|
|
use sqlx::{Any, Connection, Executor, Row};
|
|
use sqlx_test::new;
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_connects() -> anyhow::Result<()> {
|
|
let mut conn = new::<Any>().await?;
|
|
|
|
let value = sqlx::query("select 1 + 5")
|
|
.try_map(|row: AnyRow| row.try_get::<i32, _>(0))
|
|
.fetch_one(&mut conn)
|
|
.await?;
|
|
|
|
assert_eq!(6i32, value);
|
|
|
|
conn.close().await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_pings() -> anyhow::Result<()> {
|
|
let mut conn = new::<Any>().await?;
|
|
|
|
conn.ping().await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_executes_with_pool() -> anyhow::Result<()> {
|
|
let pool = sqlx_test::pool::<Any>().await?;
|
|
|
|
let rows = pool.fetch_all("SELECT 1; SElECT 2").await?;
|
|
|
|
assert_eq!(rows.len(), 2);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_gets_by_name() -> anyhow::Result<()> {
|
|
let mut conn = new::<Any>().await?;
|
|
|
|
let row = conn.fetch_one("SELECT 1 as _1").await?;
|
|
let val: i32 = row.get("_1");
|
|
|
|
assert_eq!(val, 1);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_can_fail_and_recover() -> anyhow::Result<()> {
|
|
let mut conn = new::<Any>().await?;
|
|
|
|
for i in 0..10 {
|
|
// make a query that will fail
|
|
let res = conn
|
|
.execute("INSERT INTO not_found (column) VALUES (10)")
|
|
.await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
// now try and use the connection
|
|
let val: i32 = conn
|
|
.fetch_one(&*format!("SELECT {}", i))
|
|
.await?
|
|
.get_unchecked(0);
|
|
|
|
assert_eq!(val, i);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[sqlx_macros::test]
|
|
async fn it_can_fail_and_recover_with_pool() -> anyhow::Result<()> {
|
|
let pool = sqlx_test::pool::<Any>().await?;
|
|
|
|
for i in 0..10 {
|
|
// make a query that will fail
|
|
let res = pool
|
|
.execute("INSERT INTO not_found (column) VALUES (10)")
|
|
.await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
// now try and use the connection
|
|
let val: i32 = pool
|
|
.fetch_one(&*format!("SELECT {}", i))
|
|
.await?
|
|
.get_unchecked(0);
|
|
|
|
assert_eq!(val, i);
|
|
}
|
|
|
|
Ok(())
|
|
}
|