use sqlx_test over connect fn in test

This commit is contained in:
Ryan Leckey 2020-03-29 14:40:02 -07:00
parent 7a59e383bf
commit 5fa11da353
2 changed files with 14 additions and 21 deletions

View file

@ -1,9 +1,10 @@
use sqlx::{Connect, PgConnection};
use sqlx::Postgres;
use sqlx_test::new;
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_query() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let account = sqlx::query!(
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
@ -20,7 +21,7 @@ async fn test_query() -> anyhow::Result<()> {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_no_result() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let _ = sqlx::query!("DELETE FROM pg_enum")
.execute(&mut conn)
@ -32,7 +33,7 @@ async fn test_no_result() -> anyhow::Result<()> {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_text_var_char_char_n() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
// TEXT
// we cannot infer nullability from an expression
@ -64,7 +65,7 @@ async fn test_text_var_char_char_n() -> anyhow::Result<()> {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn _file() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let account = sqlx::query_file!("tests/test-query.sql",)
.fetch_one(&mut conn)
@ -84,7 +85,7 @@ struct Account {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_query_as() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let name: Option<&str> = None;
let account = sqlx::query_as!(
@ -111,7 +112,7 @@ struct RawAccount {
#[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 = connect().await?;
let mut conn = new::<Postgres>().await?;
let account = sqlx::query_as!(
RawAccount,
@ -131,7 +132,7 @@ async fn test_query_as_raw() -> anyhow::Result<()> {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_query_file_as() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let account = sqlx::query_file_as!(Account, "tests/test-query.sql",)
.fetch_one(&mut conn)
@ -145,7 +146,7 @@ async fn test_query_file_as() -> anyhow::Result<()> {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn query_by_string() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let string = "Hello, world!".to_string();
let ref tuple = ("Hello, world!".to_string(),);
@ -178,7 +179,7 @@ async fn test_nullable_err() -> anyhow::Result<()> {
name: String,
}
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let err = sqlx::query_as!(
Account,
@ -200,7 +201,7 @@ async fn test_nullable_err() -> anyhow::Result<()> {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_many_args() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
// previous implementation would only have supported 10 bind parameters
// (this is really gross to test in MySQL)
@ -221,7 +222,7 @@ async fn test_many_args() -> anyhow::Result<()> {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_array_from_slice() -> anyhow::Result<()> {
let mut conn = connect().await?;
let mut conn = new::<Postgres>().await?;
let list: &[i32] = &[1, 2, 3, 4i32];
@ -243,10 +244,3 @@ async fn test_array_from_slice() -> anyhow::Result<()> {
Ok(())
}
async fn connect() -> anyhow::Result<PgConnection> {
let _ = dotenv::dotenv();
let _ = env_logger::try_init();
Ok(PgConnection::connect(dotenv::var("DATABASE_URL")?).await?)
}

View file

@ -11,6 +11,7 @@ use sqlx_test::{new, test_prepared_type, test_type};
macro_rules! array_macro_test {
($name:ident($type:ty, $($sql:literal == $value:expr),+ $(,)?)) => {
paste::item! {
#[allow(unused_imports)]
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn [< test_array_type_ $name >] () -> anyhow::Result<()> {
@ -688,8 +689,6 @@ mod json {
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_json_value_in_macro() -> anyhow::Result<()> {
use sqlx::prelude::*;
let mut conn = sqlx_test::new::<Postgres>().await?;
let v: serde_json::Value = json!({