diff --git a/Cargo.toml b/Cargo.toml index 0cca6a1a..535d1531 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,10 @@ criterion = "0.3.0" name = "sql-macro-test" required-features = ["postgres", "uuid", "macros"] +[[test]] +name = "postgres-types" +required-features = ["postgres"] + [[bench]] name = "postgres_protocol" required-features = ["postgres", "unstable"] diff --git a/tests/postgres-types.rs b/tests/postgres-types.rs new file mode 100644 index 00000000..95e53b71 --- /dev/null +++ b/tests/postgres-types.rs @@ -0,0 +1,35 @@ +use sqlx::{Connection, Postgres}; +use std::env; + +macro_rules! test { + ($name:ident: $ty:ty: $($text:literal == $value:expr),+) => { + #[async_std::test] + async fn $name () -> sqlx::Result<()> { + let mut conn = + Connection::::open(&env::var("DATABASE_URL").unwrap()).await?; + + $( + let row = sqlx::query(&format!("SELECT {} = $1, $1", $text)) + .bind($value) + .fetch_one(&mut conn) + .await?; + + assert!(row.get::(0)); + assert!($value == row.get::<$ty>(1)); + )+ + + Ok(()) + } + } +} + +test!(postgres_bool: bool: "false::boolean" == false, "true::boolean" == true); + +test!(postgres_smallint: i16: "821::smallint" == 821_i16); +test!(postgres_int: i32: "94101::int" == 94101_i32); +test!(postgres_bigint: i64: "9358295312::bigint" == 9358295312_i64); + +test!(postgres_real: f32: "9419.122::real" == 9419.122_f32); +test!(postgres_double: f64: "939399419.1225182::double precision" == 939399419.1225182_f64); + +test!(postgres_text: String: "'this is foo'" == "this is foo", "''" == "");