Skip to content

Commit

Permalink
Test Decimal conversions in my and pg
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Jul 3, 2020
1 parent fbd0360 commit 136531b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions sqlx-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion tests/mysql/types.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -113,7 +116,7 @@ mod time_tests {
}

#[cfg(feature = "bigdecimal")]
test_type!(decimal<sqlx::types::BigDecimal>(
test_type!(bigdecimal<sqlx::types::BigDecimal>(
MySql,
"CAST(0 as DECIMAL(0, 0))" == "0".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(1 AS DECIMAL(1, 0))" == "1".parse::<sqlx::types::BigDecimal>().unwrap(),
Expand All @@ -124,6 +127,18 @@ test_type!(decimal<sqlx::types::BigDecimal>(
"CAST(12345.6789 AS DECIMAL(9, 4))" == "12345.6789".parse::<sqlx::types::BigDecimal>().unwrap(),
));

#[cfg(feature = "decimal")]
test_type!(decimal<sqlx::types::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::*;
Expand Down
16 changes: 15 additions & 1 deletion tests/postgres/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -324,7 +326,7 @@ mod json {
}

#[cfg(feature = "bigdecimal")]
test_type!(decimal<sqlx::types::BigDecimal>(Postgres,
test_type!(bigdecimal<sqlx::types::BigDecimal>(Postgres,

// https://github.com/launchbadge/sqlx/issues/283
"0::numeric" == "0".parse::<sqlx::types::BigDecimal>().unwrap(),
Expand All @@ -337,6 +339,18 @@ test_type!(decimal<sqlx::types::BigDecimal>(Postgres,
"12345.6789::numeric" == "12345.6789".parse::<sqlx::types::BigDecimal>().unwrap(),
));

#[cfg(feature = "decimal")]
test_type!(decimal<sqlx::types::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<i32> = Bound::Excluded(2);
const EXC3: Bound<i32> = Bound::Excluded(3);
const INC1: Bound<i32> = Bound::Included(1);
Expand Down

0 comments on commit 136531b

Please sign in to comment.