Add one more filter "integration" test

This commit is contained in:
Loïc Lecrenier 2022-12-07 14:11:20 +01:00
parent e688581c36
commit d38cc73630

View File

@ -398,10 +398,12 @@ impl<'a> From<FilterCondition<'a>> for Filter<'a> {
#[cfg(test)]
mod tests {
use std::fmt::Write;
use std::iter::FromIterator;
use big_s::S;
use either::Either;
use maplit::hashset;
use roaring::RoaringBitmap;
use crate::index::tests::TempIndex;
use crate::Filter;
@ -752,4 +754,85 @@ mod tests {
Err(crate::Error::UserError(crate::error::UserError::InvalidFilter(_)))
));
}
#[test]
fn filter_number() {
let index = TempIndex::new();
index
.update_settings(|settings| {
settings.set_primary_key("id".to_owned());
settings.set_filterable_fields(hashset! { S("id"), S("one"), S("two") });
})
.unwrap();
let mut docs = vec![];
for i in 0..100 {
docs.push(serde_json::json!({ "id": i, "two": i % 10 }));
}
index.add_documents(documents!(docs)).unwrap();
let rtxn = index.read_txn().unwrap();
for i in 0..100 {
let filter_str = format!("id = {i}");
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::from_iter([i]));
}
for i in 0..100 {
let filter_str = format!("id > {i}");
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::from_iter((i + 1)..100));
}
for i in 0..100 {
let filter_str = format!("id < {i}");
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::from_iter(0..i));
}
for i in 0..100 {
let filter_str = format!("id <= {i}");
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::from_iter(0..=i));
}
for i in 0..100 {
let filter_str = format!("id >= {i}");
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::from_iter(i..100));
}
for i in 0..100 {
for j in i..100 {
let filter_str = format!("id {i} TO {j}");
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::from_iter(i..=j));
}
}
let filter = Filter::from_str("one >= 0 OR one <= 0").unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::default());
let filter = Filter::from_str("one = 0").unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::default());
for i in 0..10 {
for j in i..10 {
let filter_str = format!("two {i} TO {j}");
let filter = Filter::from_str(&filter_str).unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(
result,
RoaringBitmap::from_iter((0..100).filter(|x| (i..=j).contains(&(x % 10))))
);
}
}
let filter = Filter::from_str("two != 0").unwrap().unwrap();
let result = filter.evaluate(&rtxn, &index).unwrap();
assert_eq!(result, RoaringBitmap::from_iter((0..100).filter(|x| x % 10 != 0)));
}
}