From 136531b7893bdd92074725d4a27caea231d5346a Mon Sep 17 00:00:00 2001 From: Julius de Bruijn Date: Fri, 3 Jul 2020 14:34:46 +0200 Subject: [PATCH] Test Decimal conversions in my and pg --- sqlx-core/src/types/mod.rs | 4 ++++ tests/mysql/types.rs | 17 ++++++++++++++++- tests/postgres/types.rs | 16 +++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/types/mod.rs b/sqlx-core/src/types/mod.rs index 18e18423a1..1f1de0a78d 100644 --- a/sqlx-core/src/types/mod.rs +++ b/sqlx-core/src/types/mod.rs @@ -37,6 +37,10 @@ pub mod time { #[cfg_attr(docsrs, doc(cfg(feature = "bigdecimal")))] pub use bigdecimal::BigDecimal; +#[cfg(feature = "decimal")] +#[cfg_attr(docsrs, doc(cfg(feature = "decimal")))] +pub use rust_decimal::Decimal; + #[cfg(feature = "ipnetwork")] #[cfg_attr(docsrs, doc(cfg(feature = "ipnetwork")))] pub mod ipnetwork { diff --git a/tests/mysql/types.rs b/tests/mysql/types.rs index bebe6e8968..a20033dfd8 100644 --- a/tests/mysql/types.rs +++ b/tests/mysql/types.rs @@ -1,5 +1,8 @@ extern crate time_ as time; +#[cfg(feature = "decimal")] +use std::str::FromStr; + use sqlx::mysql::MySql; use sqlx_test::test_type; @@ -113,7 +116,7 @@ mod time_tests { } #[cfg(feature = "bigdecimal")] -test_type!(decimal( +test_type!(bigdecimal( MySql, "CAST(0 as DECIMAL(0, 0))" == "0".parse::().unwrap(), "CAST(1 AS DECIMAL(1, 0))" == "1".parse::().unwrap(), @@ -124,6 +127,18 @@ test_type!(decimal( "CAST(12345.6789 AS DECIMAL(9, 4))" == "12345.6789".parse::().unwrap(), )); +#[cfg(feature = "decimal")] +test_type!(decimal(MySql, + "CAST(0 as DECIMAL(0, 0))" == sqlx::types::Decimal::from_str("0").unwrap(), + "CAST(1 AS DECIMAL(1, 0))" == sqlx::types::Decimal::from_str("1").unwrap(), + // bug in rust_decimal: https://github.com/paupino/rust-decimal/issues/251 + //"CAST(10000 AS DECIMAL(5, 0))" == sqlx::types::Decimal::from_str("10000").unwrap(), + "CAST(0.1 AS DECIMAL(2, 1))" == sqlx::types::Decimal::from_str("0.1").unwrap(), + "CAST(0.01234 AS DECIMAL(6, 5))" == sqlx::types::Decimal::from_str("0.01234").unwrap(), + "CAST(12.34 AS DECIMAL(4, 2))" == sqlx::types::Decimal::from_str("12.34").unwrap(), + "CAST(12345.6789 AS DECIMAL(9, 4))" == sqlx::types::Decimal::from_str("12345.6789").unwrap(), +)); + #[cfg(feature = "json")] mod json_tests { use super::*; diff --git a/tests/postgres/types.rs b/tests/postgres/types.rs index c9bb49b949..f94ec971ea 100644 --- a/tests/postgres/types.rs +++ b/tests/postgres/types.rs @@ -1,6 +1,8 @@ extern crate time_ as time; use std::ops::Bound; +#[cfg(feature = "decimal")] +use std::str::FromStr; use sqlx::postgres::types::{PgInterval, PgMoney, PgRange}; use sqlx::postgres::Postgres; @@ -324,7 +326,7 @@ mod json { } #[cfg(feature = "bigdecimal")] -test_type!(decimal(Postgres, +test_type!(bigdecimal(Postgres, // https://github.com/launchbadge/sqlx/issues/283 "0::numeric" == "0".parse::().unwrap(), @@ -337,6 +339,18 @@ test_type!(decimal(Postgres, "12345.6789::numeric" == "12345.6789".parse::().unwrap(), )); +#[cfg(feature = "decimal")] +test_type!(decimal(Postgres, + "0::numeric" == sqlx::types::Decimal::from_str("0").unwrap(), + "1::numeric" == sqlx::types::Decimal::from_str("1").unwrap(), + // bug in rust_decimal: https://github.com/paupino/rust-decimal/issues/251 + //"10000::numeric" == sqlx::types::Decimal::from_str("10000").unwrap(), + "0.1::numeric" == sqlx::types::Decimal::from_str("0.1").unwrap(), + "0.01234::numeric" == sqlx::types::Decimal::from_str("0.01234").unwrap(), + "12.34::numeric" == sqlx::types::Decimal::from_str("12.34").unwrap(), + "12345.6789::numeric" == sqlx::types::Decimal::from_str("12345.6789").unwrap(), +)); + const EXC2: Bound = Bound::Excluded(2); const EXC3: Bound = Bound::Excluded(3); const INC1: Bound = Bound::Included(1);