Skip to content

Commit

Permalink
Fixed (#2838)
Browse files Browse the repository at this point in the history
fmt
  • Loading branch information
jkleinknox authored Nov 7, 2023
1 parent ee3b94d commit ee73c84
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions sqlx-postgres/src/types/rust_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ impl TryFrom<PgNumeric> for Decimal {
type Error = BoxDynError;

fn try_from(numeric: PgNumeric) -> Result<Self, BoxDynError> {
let (digits, sign, mut weight) = match numeric {
let (digits, sign, mut weight, scale) = match numeric {
PgNumeric::Number {
digits,
sign,
weight,
..
} => (digits, sign, weight),
scale,
} => (digits, sign, weight, scale),

PgNumeric::NotANumber => {
return Err("Decimal does not support NaN values".into());
Expand Down Expand Up @@ -65,6 +65,8 @@ impl TryFrom<PgNumeric> for Decimal {
PgNumericSign::Negative => value.set_sign_negative(true),
}

value.rescale(scale as u32);

Ok(value)
}
}
Expand Down Expand Up @@ -311,29 +313,38 @@ mod decimal_to_pgnumeric {
#[test]
fn decimal_4() {
let decimal: Decimal = "12345.67890".parse().unwrap();
assert_eq!(
PgNumeric::try_from(&decimal).unwrap(),
PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 5,
weight: 1,
digits: vec![1, 2345, 6789]
}
);
let expected_numeric = PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 5,
weight: 1,
digits: vec![1, 2345, 6789],
};
assert_eq!(PgNumeric::try_from(&decimal).unwrap(), expected_numeric);

let actual_decimal = Decimal::try_from(expected_numeric).unwrap();
assert_eq!(actual_decimal, decimal);
assert_eq!(actual_decimal.mantissa(), 1234567890);
assert_eq!(actual_decimal.scale(), 5);
}

#[test]
fn one_digit_decimal() {
let one_digit_decimal: Decimal = "0.00001234".parse().unwrap();
let expected_numeric = PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 8,
weight: -2,
digits: vec![1234],
};
assert_eq!(
PgNumeric::try_from(&one_digit_decimal).unwrap(),
PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 8,
weight: -2,
digits: vec![1234]
}
expected_numeric
);

let actual_decimal = Decimal::try_from(expected_numeric).unwrap();
assert_eq!(actual_decimal, one_digit_decimal);
assert_eq!(actual_decimal.mantissa(), 1234);
assert_eq!(actual_decimal.scale(), 8);
}

#[test]
Expand Down Expand Up @@ -396,6 +407,24 @@ mod decimal_to_pgnumeric {
);
}

#[test]
fn issue_2247_trailing_zeros() {
// This is a regression test for https://github.com/launchbadge/sqlx/issues/2247
let one_hundred: Decimal = "100.00".parse().unwrap();
let expected_numeric = PgNumeric::Number {
sign: PgNumericSign::Positive,
scale: 2,
weight: 0,
digits: vec![100],
};
assert_eq!(PgNumeric::try_from(&one_hundred).unwrap(), expected_numeric);

let actual_decimal = Decimal::try_from(expected_numeric).unwrap();
assert_eq!(actual_decimal, one_hundred);
assert_eq!(actual_decimal.mantissa(), 10000);
assert_eq!(actual_decimal.scale(), 2);
}

#[test]
fn issue_666_trailing_zeroes_at_max_precision() {}
}

0 comments on commit ee73c84

Please sign in to comment.