sqlx/tests/mysql-types.rs

95 lines
2.8 KiB
Rust
Raw Normal View History

use sqlx::{mysql::MySqlConnection, Connection, Row};
async fn connect() -> anyhow::Result<MySqlConnection> {
Ok(MySqlConnection::open(dotenv::var("DATABASE_URL")?).await?)
}
macro_rules! test {
($name:ident: $ty:ty: $($text:literal == $value:expr),+) => {
2020-01-15 09:17:33 +00:00
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn $name () -> anyhow::Result<()> {
let mut conn = connect().await?;
$(
2019-12-28 01:31:01 +00:00
let row = sqlx::query(&format!("SELECT {} = ?, ? as _1", $text))
.bind($value)
.bind($value)
.fetch_one(&mut conn)
.await?;
2019-12-28 01:31:01 +00:00
let value = row.get::<$ty, _>("_1");
2020-01-30 07:37:15 +00:00
assert_eq!(row.get::<i32, _>(0), 1, "value returned from server: {:?}", value);
assert_eq!($value, value);
)+
Ok(())
}
}
}
test!(mysql_bool: bool: "false" == false, "true" == true);
2019-12-06 05:36:09 +00:00
test!(mysql_tiny_unsigned: u8: "253" == 253_u8);
test!(mysql_tiny: i8: "5" == 5_i8);
2019-12-06 05:36:09 +00:00
test!(mysql_medium_unsigned: u16: "21415" == 21415_u16);
test!(mysql_short: i16: "21415" == 21415_i16);
2019-12-06 05:36:09 +00:00
test!(mysql_long_unsigned: u32: "2141512" == 2141512_u32);
test!(mysql_long: i32: "2141512" == 2141512_i32);
2019-12-06 05:36:09 +00:00
test!(mysql_longlong_unsigned: u64: "2141512" == 2141512_u64);
test!(mysql_longlong: i64: "2141512" == 2141512_i64);
2020-01-30 07:37:15 +00:00
// `DOUBLE` can be compared with decimal literals just fine but the same can't be said for `FLOAT`
test!(mysql_double: f64: "3.14159265" == 3.14159265f64);
2019-12-06 05:36:09 +00:00
test!(mysql_string: String: "'helloworld'" == "helloworld");
2020-01-15 09:17:33 +00:00
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn mysql_bytes() -> anyhow::Result<()> {
let mut conn = connect().await?;
let value = &b"Hello, World"[..];
let rec = sqlx::query!(
"SELECT (X'48656c6c6f2c20576f726c64' = ?) as _1, CAST(? as BINARY) as _2",
value,
value
)
.fetch_one(&mut conn)
.await?;
assert!(rec._1 != 0);
let output: Vec<u8> = rec._2;
assert_eq!(&value[..], &*output);
Ok(())
}
2020-01-30 07:37:15 +00:00
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn mysql_float() -> anyhow::Result<()> {
let mut conn = connect().await?;
let value = 10.2f32;
let row = sqlx::query("SELECT ? as _1")
.bind(value)
.fetch_one(&mut conn)
.await?;
// comparison between FLOAT and literal doesn't work as expected
// we get implicit widening to DOUBLE which gives a slightly different value
// however, round-trip does work as expected
let ret = row.get::<f32, _>("_1");
assert_eq!(value, ret);
Ok(())
}