fix(macros): reintroduce and fix test for macros in SQLite

This commit is contained in:
Austin Bonander 2020-06-05 21:16:14 -07:00
parent 80b4e2fca6
commit 0652864315
No known key found for this signature in database
GPG key ID: 4E7DA63E66AFC37E
2 changed files with 73 additions and 0 deletions

73
tests/sqlite/macros.rs Normal file
View file

@ -0,0 +1,73 @@
use sqlx::Sqlite;
use sqlx_test::new;
#[sqlx_macros::test]
async fn macro_select() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query!("select id, name, is_active from accounts where id = 1")
.fetch_one(&mut conn)
.await?;
assert_eq!(1, account.id);
assert_eq!("Herp Derpinson", account.name);
assert_eq!(account.is_active, None);
Ok(())
}
#[sqlx_macros::test]
async fn macro_select_bind() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query!(
"select id, name, is_active from accounts where id = ?",
1i32
)
.fetch_one(&mut conn)
.await?;
assert_eq!(1, account.id);
assert_eq!("Herp Derpinson", account.name);
assert_eq!(account.is_active, None);
Ok(())
}
#[derive(Debug)]
struct RawAccount {
id: i32,
name: String,
is_active: Option<bool>,
}
#[sqlx_macros::test]
async fn test_query_as_raw() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query_as!(RawAccount, "SELECT id, name, is_active from accounts")
.fetch_one(&mut conn)
.await?;
assert_eq!(account.id, 1);
assert_eq!(account.name, "Herp Derpinson");
assert_eq!(account.is_active, None);
Ok(())
}
#[sqlx_macros::test]
async fn macro_select_from_view() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let account = sqlx::query!("SELECT id, name, is_active from accounts_view")
.fetch_one(&mut conn)
.await?;
// SQLite tells us the true origin of these columns even through the view
assert_eq!(account.id, 1);
assert_eq!(account.name, "Herp Derpinson");
assert_eq!(account.is_active, None);
Ok(())
}

Binary file not shown.