mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-19 08:39:44 +00:00
feat: introduce docker-compose based testing for running locally against many database combinations
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use sqlx::{Connect, Database};
|
||||
use sqlx::{database::Database, Connect};
|
||||
use std::env;
|
||||
|
||||
fn setup_if_needed() {
|
||||
let _ = dotenv::dotenv();
|
||||
@@ -13,42 +14,81 @@ where
|
||||
{
|
||||
setup_if_needed();
|
||||
|
||||
Ok(DB::Connection::connect(dotenv::var("DATABASE_URL")?).await?)
|
||||
Ok(DB::Connection::connect(&env::var("DATABASE_URL")?).await?)
|
||||
}
|
||||
|
||||
// Test type encoding and decoding
|
||||
#[macro_export]
|
||||
macro_rules! test_type {
|
||||
($name:ident($db:ident, $ty:ty, $sql:literal, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::test_prepared_type!($name($db, $ty, $sql, $($text == $value),+));
|
||||
$crate::test_unprepared_type!($name($db, $ty, $($text == $value),+));
|
||||
($name:ident<$ty:ty>($db:ident, $sql:literal, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::__test_prepared_type!($name<$ty>($db, $sql, $($text == $value),+));
|
||||
$crate::test_unprepared_type!($name<$ty>($db, $($text == $value),+));
|
||||
};
|
||||
|
||||
($name:ident($db:ident, $ty:ty, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::test_prepared_type!($name($db, $ty, $($text == $value),+));
|
||||
$crate::test_unprepared_type!($name($db, $ty, $($text == $value),+));
|
||||
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
paste::item! {
|
||||
$crate::__test_prepared_type!($name<$ty>($db, $crate::[< $db _query_for_test_prepared_type >]!(), $($text == $value),+));
|
||||
$crate::test_unprepared_type!($name<$ty>($db, $($text == $value),+));
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::test_type!($name<$name>($db, $($text == $value),+));
|
||||
};
|
||||
}
|
||||
|
||||
// Test type decoding only
|
||||
#[macro_export]
|
||||
macro_rules! test_decode_type {
|
||||
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::__test_prepared_decode_type!($name<$ty>($db, $($text == $value),+));
|
||||
$crate::test_unprepared_type!($name<$ty>($db, $($text == $value),+));
|
||||
};
|
||||
|
||||
($name:ident($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::test_decode_type!($name<$name>($db, $($text == $value),+));
|
||||
};
|
||||
}
|
||||
|
||||
// Test type encoding and decoding
|
||||
#[macro_export]
|
||||
macro_rules! test_prepared_type {
|
||||
($name:ident<$ty:ty>($db:ident, $sql:literal, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::__test_prepared_type!($name<$ty>($db, $sql, $($text == $value),+));
|
||||
};
|
||||
|
||||
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
paste::item! {
|
||||
$crate::__test_prepared_type!($name<$ty>($db, $crate::[< $db _query_for_test_prepared_type >]!(), $($text == $value),+));
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
$crate::__test_prepared_type!($name<$name>($db, $($text == $value),+));
|
||||
};
|
||||
}
|
||||
|
||||
// Test type decoding for the simple (unprepared) query API
|
||||
#[macro_export]
|
||||
macro_rules! test_unprepared_type {
|
||||
($name:ident($db:ident, $ty:ty, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
paste::item! {
|
||||
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
|
||||
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
||||
#[sqlx_macros::test]
|
||||
async fn [< test_unprepared_type_ $name >] () -> anyhow::Result<()> {
|
||||
use sqlx::prelude::*;
|
||||
use futures::TryStreamExt;
|
||||
|
||||
let mut conn = sqlx_test::new::<$db>().await?;
|
||||
|
||||
$(
|
||||
let query = format!("SELECT {} as _1", $text);
|
||||
let mut cursor = conn.fetch(&*query);
|
||||
let row = cursor.next().await?.unwrap();
|
||||
let rec = row.try_get::<$ty, _>("_1")?;
|
||||
let query = format!("SELECT {}", $text);
|
||||
let mut s = conn.fetch(&*query);
|
||||
let row = s.try_next().await?.unwrap();
|
||||
let rec = row.try_get::<$ty, _>(0)?;
|
||||
|
||||
assert!($value == rec);
|
||||
|
||||
drop(s);
|
||||
)+
|
||||
|
||||
Ok(())
|
||||
@@ -57,95 +97,76 @@ macro_rules! test_unprepared_type {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This macro is cursed. Needs a good re-factor.
|
||||
// Test type encoding and decoding for the prepared query API
|
||||
// Test type decoding only for the prepared query API
|
||||
#[macro_export]
|
||||
macro_rules! test_prepared_type {
|
||||
($name:ident($db:ident, $ty:ty, $sql:literal, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
macro_rules! __test_prepared_decode_type {
|
||||
($name:ident<$ty:ty>($db:ident, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
paste::item! {
|
||||
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
|
||||
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
||||
async fn [< test_prepared_type_ $name >] () -> anyhow::Result<()> {
|
||||
use sqlx::prelude::*;
|
||||
#[sqlx_macros::test]
|
||||
async fn [< test_prepared_decode_type_ $name >] () -> anyhow::Result<()> {
|
||||
use sqlx::Row;
|
||||
|
||||
let mut conn = sqlx_test::new::<$db>().await?;
|
||||
|
||||
$(
|
||||
let query = format!($sql, $text);
|
||||
let query = format!("SELECT {}", $text);
|
||||
|
||||
let rec: (bool, Option<String>, $ty, $ty) = sqlx::query_as(&query)
|
||||
.bind($value)
|
||||
.bind($value)
|
||||
.bind($value)
|
||||
let row = sqlx::query(&query)
|
||||
.fetch_one(&mut conn)
|
||||
.await?;
|
||||
|
||||
assert!(rec.0,
|
||||
"[1] DB value mismatch; given value: {:?}\n\
|
||||
as received: {:?}\n\
|
||||
as returned: {:?}\n\
|
||||
round-trip: {:?}",
|
||||
$value, rec.1, rec.2, rec.3);
|
||||
let rec: $ty = row.try_get(0)?;
|
||||
|
||||
assert_eq!($value, rec.2,
|
||||
"[2] DB value mismatch; given value: {:?}\n\
|
||||
as received: {:?}\n\
|
||||
as returned: {:?}\n\
|
||||
round-trip: {:?}",
|
||||
$value, rec.1, rec.2, rec.3);
|
||||
|
||||
assert_eq!($value, rec.3,
|
||||
"[3] DB value mismatch; given value: {:?}\n\
|
||||
as received: {:?}\n\
|
||||
as returned: {:?}\n\
|
||||
round-trip: {:?}",
|
||||
$value, rec.1, rec.2, rec.3);
|
||||
assert!($value == rec);
|
||||
)+
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
($name:ident($db:ident, $ty:ty, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
// Test type encoding and decoding for the prepared query API
|
||||
#[macro_export]
|
||||
macro_rules! __test_prepared_type {
|
||||
($name:ident<$ty:ty>($db:ident, $sql:expr, $($text:literal == $value:expr),+ $(,)?)) => {
|
||||
paste::item! {
|
||||
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
|
||||
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
|
||||
#[sqlx_macros::test]
|
||||
async fn [< test_prepared_type_ $name >] () -> anyhow::Result<()> {
|
||||
use sqlx::prelude::*;
|
||||
use sqlx::Row;
|
||||
|
||||
let mut conn = sqlx_test::new::<$db>().await?;
|
||||
|
||||
$(
|
||||
let query = format!($crate::[< $db _query_for_test_prepared_type >]!(), $text);
|
||||
let query = format!($sql, $text);
|
||||
|
||||
let rec: (bool, Option<String>, $ty, $ty) = sqlx::query_as(&query)
|
||||
.bind($value)
|
||||
let row = sqlx::query(&query)
|
||||
.bind($value)
|
||||
.bind($value)
|
||||
.fetch_one(&mut conn)
|
||||
.await?;
|
||||
|
||||
assert!(rec.0,
|
||||
let matches: bool = row.try_get(0)?;
|
||||
let returned: $ty = row.try_get(1)?;
|
||||
let round_trip: $ty = row.try_get(2)?;
|
||||
|
||||
assert!(matches,
|
||||
"[1] DB value mismatch; given value: {:?}\n\
|
||||
as received: {:?}\n\
|
||||
as returned: {:?}\n\
|
||||
round-trip: {:?}",
|
||||
$value, rec.1, rec.2, rec.3);
|
||||
$value, returned, round_trip);
|
||||
|
||||
assert_eq!($value, rec.2,
|
||||
assert_eq!($value, returned,
|
||||
"[2] DB value mismatch; given value: {:?}\n\
|
||||
as received: {:?}\n\
|
||||
as returned: {:?}\n\
|
||||
round-trip: {:?}",
|
||||
$value, rec.1, rec.2, rec.3);
|
||||
$value, returned, round_trip);
|
||||
|
||||
assert_eq!($value, rec.3,
|
||||
assert_eq!($value, round_trip,
|
||||
"[3] DB value mismatch; given value: {:?}\n\
|
||||
as received: {:?}\n\
|
||||
as returned: {:?}\n\
|
||||
round-trip: {:?}",
|
||||
$value, rec.1, rec.2, rec.3);
|
||||
$value, returned, round_trip);
|
||||
)+
|
||||
|
||||
Ok(())
|
||||
@@ -157,20 +178,20 @@ macro_rules! test_prepared_type {
|
||||
#[macro_export]
|
||||
macro_rules! MySql_query_for_test_prepared_type {
|
||||
() => {
|
||||
"SELECT {0} <=> ?, '<UNKNOWN>' as _1, ? as _2, ? as _3"
|
||||
"SELECT {0} <=> ?, {0}, ?"
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! Sqlite_query_for_test_prepared_type {
|
||||
() => {
|
||||
"SELECT {0} is ?, cast(? as text) as _1, {0} as _2, ? as _3"
|
||||
"SELECT {0} is ?, {0}, ?"
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! Postgres_query_for_test_prepared_type {
|
||||
() => {
|
||||
"SELECT {0} is not distinct from $1, $2::text as _1, {0} as _2, $3 as _3"
|
||||
"SELECT {0} is not distinct from $1, {0}, $2"
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user