2020-03-17 03:55:19 +00:00
|
|
|
use sqlx::Sqlite;
|
|
|
|
use sqlx_test::new;
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
|
|
|
|
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
2020-03-17 04:05:48 +00:00
|
|
|
async fn macro_select() -> anyhow::Result<()> {
|
2020-03-17 03:55:19 +00:00
|
|
|
let mut conn = new::<Sqlite>().await?;
|
2020-03-18 04:33:29 +00:00
|
|
|
|
|
|
|
let account = sqlx::query!("select id, name, is_active from accounts where id = 1")
|
2020-03-17 04:05:48 +00:00
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2020-03-17 03:55:19 +00:00
|
|
|
|
2020-03-18 04:33:29 +00:00
|
|
|
assert_eq!(1, account.id);
|
|
|
|
assert_eq!("Herp Derpinson", account.name);
|
2020-03-17 03:55:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
|
|
|
|
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
2020-03-17 04:05:48 +00:00
|
|
|
async fn macro_select_bind() -> anyhow::Result<()> {
|
2020-03-17 03:55:19 +00:00
|
|
|
let mut conn = new::<Sqlite>().await?;
|
2020-03-18 04:33:29 +00:00
|
|
|
|
2020-03-18 04:43:41 +00:00
|
|
|
let account = sqlx::query!(
|
|
|
|
"select id, name, is_active from accounts where id = ?",
|
|
|
|
1i32
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2020-03-17 03:55:19 +00:00
|
|
|
|
2020-03-18 04:33:29 +00:00
|
|
|
assert_eq!(1, account.id);
|
|
|
|
assert_eq!("Herp Derpinson", account.name);
|
2020-03-17 03:55:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct RawAccount {
|
2020-03-17 04:05:48 +00:00
|
|
|
id: i32,
|
|
|
|
name: String,
|
2020-03-18 04:33:29 +00:00
|
|
|
is_active: bool,
|
2020-03-17 03:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
|
|
|
|
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
|
|
|
async fn test_query_as_raw() -> anyhow::Result<()> {
|
|
|
|
let mut conn = new::<Sqlite>().await?;
|
|
|
|
|
2020-03-18 04:33:29 +00:00
|
|
|
let account = sqlx::query_as!(RawAccount, "SELECT id, name, is_active from accounts")
|
2020-03-17 04:05:48 +00:00
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2020-03-17 03:55:19 +00:00
|
|
|
|
2020-03-17 04:05:48 +00:00
|
|
|
assert_eq!(1, account.id);
|
|
|
|
assert_eq!("Herp Derpinson", account.name);
|
2020-03-17 03:55:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|