2019-12-21 01:51:06 +00:00
|
|
|
#[async_std::test]
|
|
|
|
async fn test_query() -> sqlx::Result<()> {
|
2019-12-28 06:47:25 +00:00
|
|
|
let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
2019-12-28 06:47:25 +00:00
|
|
|
let account = sqlx::query!(
|
|
|
|
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
|
|
|
|
1i32
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
|
|
|
println!("account ID: {:?}", account.id);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn test_query_file() -> sqlx::Result<()> {
|
2019-12-28 06:47:25 +00:00
|
|
|
let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
|
|
|
let account = sqlx::query_file!("tests/test-query.sql")
|
2019-12-28 06:47:25 +00:00
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
|
|
|
println!("{:?}", account);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Account {
|
|
|
|
id: i32,
|
|
|
|
name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn test_query_as() -> sqlx::Result<()> {
|
2019-12-28 06:47:25 +00:00
|
|
|
let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
2019-12-28 06:47:25 +00:00
|
|
|
let account = sqlx::query_as!(
|
|
|
|
Account,
|
|
|
|
"SELECT * from (VALUES (1, null)) accounts(id, name)"
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
|
|
|
assert_eq!(None, account.name);
|
|
|
|
|
|
|
|
println!("{:?}", account);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn test_query_file_as() -> sqlx::Result<()> {
|
2019-12-28 06:47:25 +00:00
|
|
|
let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
|
|
|
let account = sqlx::query_file_as!(Account, "tests/test-query.sql")
|
2019-12-28 06:47:25 +00:00
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
|
|
|
println!("{:?}", account);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-12-28 07:20:01 +00:00
|
|
|
#[async_std::test]
|
|
|
|
async fn query_by_string() -> sqlx::Result<()> {
|
|
|
|
let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?;
|
|
|
|
|
|
|
|
let string = "Hello, world!".to_string();
|
|
|
|
|
|
|
|
let result = sqlx::query!(
|
2019-12-28 11:45:47 +00:00
|
|
|
"SELECT * from (VALUES('Hello, world!')) strings(string)\
|
|
|
|
where string = $1 or string = $2",
|
|
|
|
string,
|
|
|
|
string[..]
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await?;
|
2019-12-28 07:20:01 +00:00
|
|
|
|
|
|
|
assert_eq!(result.string, string);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-12-21 01:51:06 +00:00
|
|
|
#[async_std::test]
|
|
|
|
async fn test_nullable_err() -> sqlx::Result<()> {
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Account {
|
|
|
|
id: i32,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
2019-12-28 06:47:25 +00:00
|
|
|
let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?;
|
2019-12-21 01:51:06 +00:00
|
|
|
|
2019-12-28 06:47:25 +00:00
|
|
|
let err = sqlx::query_as!(
|
|
|
|
Account,
|
|
|
|
"SELECT * from (VALUES (1, null::text)) accounts(id, name)"
|
|
|
|
)
|
|
|
|
.fetch_one(&mut conn)
|
|
|
|
.await
|
|
|
|
.unwrap_err();
|
2019-12-21 01:51:06 +00:00
|
|
|
|
|
|
|
if let sqlx::Error::Decode(sqlx::decode::DecodeError::UnexpectedNull) = err {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
panic!("expected `UnexpectedNull`, got {}", err)
|
|
|
|
}
|
|
|
|
}
|