From cda6b09d79b91f349733a4e6b18ce443ea082556 Mon Sep 17 00:00:00 2001 From: Tpt Date: Wed, 23 Nov 2022 21:44:59 +0100 Subject: [PATCH] Fixes NOW() evaluation --- js/test/store.mjs | 2 +- lib/src/xsd/date_time.rs | 7 +++++++ lib/src/xsd/decimal.rs | 20 +++++++++++++++----- lib/src/xsd/duration.rs | 10 ++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/js/test/store.mjs b/js/test/store.mjs index 2b56203d..a6fa5627 100644 --- a/js/test/store.mjs +++ b/js/test/store.mjs @@ -79,7 +79,7 @@ describe('Store', function () { it('SELECT with NOW()', function () { const store = new Store([dataModel.quad(ex, ex, ex)]) - const results = store.query('SELECT (YEAR(NOW()) AS ?y) WHERE {}') + const results = store.query('SELECT * WHERE { FILTER(2022 <= YEAR(NOW()) && YEAR(NOW()) <= 2100) }') assert.strictEqual(1, results.length) }) diff --git a/lib/src/xsd/date_time.rs b/lib/src/xsd/date_time.rs index 1ca17002..3694bffe 100644 --- a/lib/src/xsd/date_time.rs +++ b/lib/src/xsd/date_time.rs @@ -2285,4 +2285,11 @@ mod tests { Time::from_str("22:10:00-05:00").unwrap() ); } + + #[test] + fn now() { + let now = DateTime::now().unwrap(); + assert!(DateTime::from_str("2022-01-01T00:00:00Z").unwrap() < now); + assert!(now < DateTime::from_str("2100-01-01T00:00:00Z").unwrap()); + } } diff --git a/lib/src/xsd/decimal.rs b/lib/src/xsd/decimal.rs index e9067d33..f4949ef0 100644 --- a/lib/src/xsd/decimal.rs +++ b/lib/src/xsd/decimal.rs @@ -22,12 +22,13 @@ impl Decimal { /// Constructs the decimal i / 10^n #[allow(clippy::cast_possible_truncation)] pub fn new(i: i128, n: u32) -> Result { - if n > DECIMAL_PART_DIGITS as u32 { - //TODO: check if end with zeros? - return Err(DecimalOverflowError); - } + let shift = (DECIMAL_PART_DIGITS as u32) + .checked_sub(n) + .ok_or(DecimalOverflowError)?; Ok(Self { - value: i.checked_div(10_i128.pow(n)).ok_or(DecimalOverflowError)?, + value: i + .checked_mul(10_i128.pow(shift)) + .ok_or(DecimalOverflowError)?, }) } @@ -549,6 +550,15 @@ impl TryFrom for i64 { mod tests { use super::*; + #[test] + fn new() { + assert_eq!(Decimal::new(1, 0).unwrap().to_string(), "1"); + assert_eq!(Decimal::new(1, 1).unwrap().to_string(), "0.1"); + assert_eq!(Decimal::new(10, 0).unwrap().to_string(), "10"); + assert_eq!(Decimal::new(10, 1).unwrap().to_string(), "1"); + assert_eq!(Decimal::new(10, 2).unwrap().to_string(), "0.1"); + } + #[test] fn from_str() { assert_eq!(Decimal::from_str("210").unwrap().to_string(), "210"); diff --git a/lib/src/xsd/duration.rs b/lib/src/xsd/duration.rs index 67f7fbfa..d5fbae3b 100644 --- a/lib/src/xsd/duration.rs +++ b/lib/src/xsd/duration.rs @@ -629,6 +629,16 @@ mod tests { assert_eq!(Duration::from_str(&min.to_string()).unwrap(), min); } + #[test] + fn from_std() { + assert_eq!( + Duration::try_from(StdDuration::new(10, 10)) + .unwrap() + .to_string(), + "PT10.00000001S" + ); + } + #[test] fn equals() { assert_eq!(