mirror of
https://github.com/isar/rusqlite.git
synced 2025-01-20 00:40:50 +08:00
Test FromSql & ToSql for std::num::NonZero types
This commit is contained in:
parent
ce7cdfb5bc
commit
2e5db7b2a0
@ -298,4 +298,48 @@ mod test {
|
||||
check_ranges::<u32>(&db, &[-2, -1, 4_294_967_296], &[0, 1, 4_294_967_295]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nonzero_ranges() -> Result<()> {
|
||||
let db = Connection::open_in_memory()?;
|
||||
|
||||
macro_rules! check_ranges {
|
||||
($nz:ty, $out_of_range:expr, $in_range:expr) => {
|
||||
for &n in $out_of_range {
|
||||
assert_eq!(
|
||||
db.query_row("SELECT ?1", [n], |r| r.get::<_, $nz>(0)),
|
||||
Err(Error::IntegralValueOutOfRange(0, n))
|
||||
);
|
||||
}
|
||||
for &n in $in_range {
|
||||
let non_zero = <$nz>::new(n).unwrap();
|
||||
assert_eq!(
|
||||
Ok(non_zero),
|
||||
db.query_row("SELECT ?1", [non_zero], |r| r.get::<_, $nz>(0))
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
check_ranges!(std::num::NonZeroI8, &[0, -129, 128], &[-128, 1, 127]);
|
||||
check_ranges!(
|
||||
std::num::NonZeroI16,
|
||||
&[0, -32769, 32768],
|
||||
&[-32768, -1, 1, 32767]
|
||||
);
|
||||
check_ranges!(
|
||||
std::num::NonZeroI32,
|
||||
&[0, -2_147_483_649, 2_147_483_648],
|
||||
&[-2_147_483_648, -1, 1, 2_147_483_647]
|
||||
);
|
||||
check_ranges!(std::num::NonZeroU8, &[0, -2, -1, 256], &[1, 255]);
|
||||
check_ranges!(std::num::NonZeroU16, &[0, -2, -1, 65536], &[1, 65535]);
|
||||
check_ranges!(
|
||||
std::num::NonZeroU32,
|
||||
&[0, -2, -1, 4_294_967_296],
|
||||
&[1, 4_294_967_295]
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -462,6 +462,54 @@ mod test {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "i128_blob")]
|
||||
#[test]
|
||||
fn test_non_zero_i128() -> crate::Result<()> {
|
||||
use std::num::NonZeroI128;
|
||||
macro_rules! nz {
|
||||
($x:expr) => {
|
||||
NonZeroI128::new($x).unwrap()
|
||||
};
|
||||
}
|
||||
|
||||
let db = crate::Connection::open_in_memory()?;
|
||||
db.execute_batch("CREATE TABLE foo (i128 BLOB, desc TEXT)")?;
|
||||
db.execute(
|
||||
"INSERT INTO foo(i128, desc) VALUES
|
||||
(?1, 'neg one'), (?2, 'neg two'),
|
||||
(?3, 'pos one'), (?4, 'pos two'),
|
||||
(?5, 'min'), (?6, 'max')",
|
||||
[
|
||||
nz!(-1),
|
||||
nz!(-2),
|
||||
nz!(1),
|
||||
nz!(2),
|
||||
nz!(i128::MIN),
|
||||
nz!(i128::MAX),
|
||||
],
|
||||
)?;
|
||||
let mut stmt = db.prepare("SELECT i128, desc FROM foo ORDER BY i128 ASC")?;
|
||||
|
||||
let res = stmt
|
||||
.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?
|
||||
.collect::<Result<Vec<(NonZeroI128, String)>, _>>()?;
|
||||
|
||||
assert_eq!(
|
||||
res,
|
||||
&[
|
||||
(nz!(i128::MIN), "min".to_owned()),
|
||||
(nz!(-2), "neg two".to_owned()),
|
||||
(nz!(-1), "neg one".to_owned()),
|
||||
(nz!(1), "pos one".to_owned()),
|
||||
(nz!(2), "pos two".to_owned()),
|
||||
(nz!(i128::MAX), "max".to_owned()),
|
||||
]
|
||||
);
|
||||
let err = db.query_row("SELECT ?1", [0i128], |row| row.get::<_, NonZeroI128>(0));
|
||||
assert_eq!(err, Err(crate::Error::IntegralValueOutOfRange(0, 0)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "uuid")]
|
||||
#[test]
|
||||
fn test_uuid() -> crate::Result<()> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user