Fixes NOW() evaluation

pull/298/head
Tpt 2 years ago committed by Thomas Tanon
parent c2fd3920b5
commit cda6b09d79
  1. 2
      js/test/store.mjs
  2. 7
      lib/src/xsd/date_time.rs
  3. 20
      lib/src/xsd/decimal.rs
  4. 10
      lib/src/xsd/duration.rs

@ -79,7 +79,7 @@ describe('Store', function () {
it('SELECT with NOW()', function () { it('SELECT with NOW()', function () {
const store = new Store([dataModel.quad(ex, ex, ex)]) 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) assert.strictEqual(1, results.length)
}) })

@ -2285,4 +2285,11 @@ mod tests {
Time::from_str("22:10:00-05:00").unwrap() 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());
}
} }

@ -22,12 +22,13 @@ impl Decimal {
/// Constructs the decimal i / 10^n /// Constructs the decimal i / 10^n
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
pub fn new(i: i128, n: u32) -> Result<Self, DecimalOverflowError> { pub fn new(i: i128, n: u32) -> Result<Self, DecimalOverflowError> {
if n > DECIMAL_PART_DIGITS as u32 { let shift = (DECIMAL_PART_DIGITS as u32)
//TODO: check if end with zeros? .checked_sub(n)
return Err(DecimalOverflowError); .ok_or(DecimalOverflowError)?;
}
Ok(Self { 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<Decimal> for i64 {
mod tests { mod tests {
use super::*; 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] #[test]
fn from_str() { fn from_str() {
assert_eq!(Decimal::from_str("210").unwrap().to_string(), "210"); assert_eq!(Decimal::from_str("210").unwrap().to_string(), "210");

@ -629,6 +629,16 @@ mod tests {
assert_eq!(Duration::from_str(&min.to_string()).unwrap(), min); 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] #[test]
fn equals() { fn equals() {
assert_eq!( assert_eq!(

Loading…
Cancel
Save