From 029ba24cada3e023400b4d24a0c32dded5a70a76 Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Thu, 11 Jun 2020 19:39:20 -0700 Subject: [PATCH] chore(macros): add tests for bind parameter overrides --- tests/mysql/macros.rs | 2 ++ tests/postgres/macros.rs | 46 ++++++++++++++++++++++++++++++++++++++++ tests/sqlite/macros.rs | 2 ++ 3 files changed, 50 insertions(+) diff --git a/tests/mysql/macros.rs b/tests/mysql/macros.rs index 1b2abfed..135b7056 100644 --- a/tests/mysql/macros.rs +++ b/tests/mysql/macros.rs @@ -139,3 +139,5 @@ async fn test_column_override_exact() -> anyhow::Result<()> { Ok(()) } + +// we don't emit bind parameter typechecks for MySQL so testing the overrides is redundant diff --git a/tests/postgres/macros.rs b/tests/postgres/macros.rs index c927bec3..4d9c90e2 100644 --- a/tests/postgres/macros.rs +++ b/tests/postgres/macros.rs @@ -301,3 +301,49 @@ async fn test_column_override_exact() -> anyhow::Result<()> { Ok(()) } + +#[sqlx_macros::test] +async fn test_bind_arg_override_exact() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let my_int = MyInt4(1); + + // this query should require a bind parameter override as we would otherwise expect the bind + // to be the same type + let record = sqlx::query!( + "select * from (select 1::int4) records(id) where id = $1", + my_int as MyInt4 + ) + .fetch_one(&mut conn) + .await?; + + assert_eq!(record.id, Some(1i32)); + + // test that we're actually emitting the typecast by requiring the bound type to be the same + let record = sqlx::query!("select $1::int8 as id", 1i32 as i64) + .fetch_one(&mut conn) + .await?; + + assert_eq!(record.id, Some(1i64)); + + Ok(()) +} + +// we can't test this yet but will want to when 1.45 drops and we can strip casts to `_` +// #[sqlx_macros::test] +// async fn test_bind_arg_override_wildcard() -> anyhow::Result<()> { +// let mut conn = new::().await?; +// +// let my_int = MyInt4(1); +// +// let record = sqlx::query!( +// "select * from (select 1::int4) records(id) where id = $1", +// my_int as _ +// ) +// .fetch_one(&mut conn) +// .await?; +// +// assert_eq!(record.id, 1i32); +// +// Ok(()) +// } diff --git a/tests/sqlite/macros.rs b/tests/sqlite/macros.rs index 60ab4bf9..338b1609 100644 --- a/tests/sqlite/macros.rs +++ b/tests/sqlite/macros.rs @@ -132,3 +132,5 @@ async fn test_column_override_exact() -> anyhow::Result<()> { Ok(()) } + +// we don't emit bind parameter typechecks for SQLite so testing the overrides is redundant