Test Decimal conversions in my and pg

This commit is contained in:
Julius de Bruijn 2020-07-03 14:34:46 +02:00 committed by Austin Bonander
parent 95ac38caed
commit 245d53e484
3 changed files with 35 additions and 2 deletions

View file

@ -39,6 +39,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 {

View file

@ -1,5 +1,8 @@
extern crate time_ as time;
#[cfg(feature = "decimal")]
use std::str::FromStr;
use sqlx::mysql::MySql;
use sqlx::{Executor, Row};
use sqlx_test::test_type;
@ -185,7 +188,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(),
@ -196,6 +199,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::*;

View file

@ -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<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(),
@ -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);