Skip to content

Commit 4951ba1

Browse files
author
bitfl0wer
committed
fix: trait not public, method cannot be used
1 parent e1c2ec4 commit 4951ba1

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

sqlx-pg-uint/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ pub trait UIntType: private::Sealed + Display {
9191
type Uint: private::Sealed + FromStr;
9292
}
9393

94-
pub(crate) trait OptionPgUint<T: UIntType> {
94+
/// Allows for converting an `Option<PgUInt>` to an `Option<[underlying integer type]>`
95+
pub trait OptionPgUint<T: UIntType> {
9596
/// Convert any `Option<PgUint>` to an `Option<[underlying integer type]>`
9697
fn to_option_uint(&self) -> Option<T::Uint>;
9798
}

sqlx-pg-uint/tests/mod.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#[cfg(test)]
2+
mod pg_u128_tests {
3+
use bigdecimal::num_bigint::BigInt;
4+
use bigdecimal::BigDecimal;
5+
use sqlx_pg_uint::{Error, OptionPgUint, PgU128};
6+
7+
use super::*;
8+
9+
#[test]
10+
fn test_to_u128() {
11+
let pg_u128 = PgU128::from(12678671u128);
12+
assert_eq!(pg_u128.to_uint(), 12678671u128);
13+
let pg_u128 = PgU128::from(0);
14+
assert_eq!(pg_u128.to_uint(), 0u128);
15+
let pg_u128 = PgU128::from(u128::MAX);
16+
assert_eq!(pg_u128.to_uint(), u128::MAX);
17+
}
18+
19+
#[test]
20+
fn test_add() {
21+
let pg_u128 = PgU128::from(12678671u128);
22+
let pg_u1282 = PgU128::from(12678671u128);
23+
assert_eq!((pg_u128 + pg_u1282).to_uint(), 25357342u128);
24+
25+
let pg_u128 = PgU128::from(0u128);
26+
let pg_u1282 = PgU128::from(0u128);
27+
assert_eq!((pg_u128 + pg_u1282).to_uint(), 0u128);
28+
}
29+
30+
#[test]
31+
#[should_panic]
32+
fn test_add_overflow() {
33+
let pg_u128 = PgU128::from(u128::MAX);
34+
let pg_u1282 = PgU128::from(1u128);
35+
let _ = pg_u128 + pg_u1282;
36+
}
37+
38+
#[test]
39+
#[should_panic]
40+
fn test_add_underflow() {
41+
let pg_u128 = PgU128::from(0u128);
42+
let pg_u1282 = PgU128::from(1u128);
43+
let _ = pg_u128 - pg_u1282;
44+
}
45+
46+
#[test]
47+
fn try_from_bigdecimal() {
48+
let pg_u128 = PgU128::try_from(BigDecimal::from(12678671u128)).unwrap();
49+
assert_eq!(pg_u128.to_uint(), 12678671u128);
50+
51+
let pg_u128 = PgU128::try_from(BigDecimal::from(0)).unwrap();
52+
assert_eq!(pg_u128.to_uint(), 0u128);
53+
54+
let pg_u128 = PgU128::try_from(BigDecimal::from(u128::MAX)).unwrap();
55+
assert_eq!(pg_u128.to_uint(), u128::MAX);
56+
57+
let pg_u128 = PgU128::try_from(BigDecimal::from(-1));
58+
assert!(pg_u128.is_err());
59+
let err = pg_u128.unwrap_err();
60+
assert_eq!(err, Error::InvalidValue(BigDecimal::from(-1)));
61+
62+
let fractional = BigDecimal::from(3) / BigDecimal::from(2);
63+
let pg_u128 = PgU128::try_from(fractional.clone());
64+
assert_eq!(pg_u128.unwrap_err(), Error::Fractional(fractional));
65+
66+
let big_decimal = BigDecimal::from(BigInt::from(2).pow(128));
67+
let pg_u128 = PgU128::try_from(big_decimal.clone());
68+
assert_eq!(pg_u128.unwrap_err(), Error::InvalidValue(big_decimal));
69+
}
70+
71+
#[test]
72+
fn test_option_conversion() {
73+
let somepguint = Some(PgU128::from(123u128));
74+
let someuint = somepguint.to_option_uint();
75+
assert_eq!(someuint, Some(123u128));
76+
77+
let pguint = PgU128::from(123);
78+
let someuint = pguint.to_option_uint();
79+
assert_eq!(someuint, Some(123u128));
80+
81+
let pguint: Option<PgU128> = None;
82+
let someuint = pguint.to_option_uint();
83+
assert_eq!(someuint, None::<u128>);
84+
}
85+
}

0 commit comments

Comments
 (0)