mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-01-18 08:48:32 +08:00
Simplify unit tests in facet/filter.rs
This commit is contained in:
parent
acff17fb88
commit
58cb1c1bda
@ -494,28 +494,21 @@ mod tests {
|
|||||||
|
|
||||||
use big_s::S;
|
use big_s::S;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use heed::EnvOpenOptions;
|
|
||||||
use maplit::hashset;
|
use maplit::hashset;
|
||||||
|
|
||||||
use super::*;
|
use crate::index::tests::TempIndex;
|
||||||
use crate::update::{self, IndexDocuments, IndexDocumentsConfig, IndexerConfig, Settings};
|
use crate::Filter;
|
||||||
use crate::Index;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_db() {
|
fn empty_db() {
|
||||||
let path = tempfile::tempdir().unwrap();
|
let index = TempIndex::new();
|
||||||
let mut options = EnvOpenOptions::new();
|
|
||||||
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
||||||
let index = Index::new(options, &path).unwrap();
|
|
||||||
|
|
||||||
// Set the filterable fields to be the channel.
|
// Set the filterable fields to be the channel.
|
||||||
let config = IndexerConfig::default();
|
index
|
||||||
let mut wtxn = index.write_txn().unwrap();
|
.update_settings(|settings| {
|
||||||
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
settings.set_searchable_fields(vec![S("PrIcE")]); // to keep the fields order
|
||||||
builder.set_searchable_fields(vec![S("PrIcE")]); // to keep the fields order
|
settings.set_filterable_fields(hashset! { S("PrIcE") });
|
||||||
builder.set_filterable_fields(hashset! { S("PrIcE") });
|
})
|
||||||
builder.execute(|_| ()).unwrap();
|
.unwrap();
|
||||||
wtxn.commit().unwrap();
|
|
||||||
|
|
||||||
let rtxn = index.read_txn().unwrap();
|
let rtxn = index.read_txn().unwrap();
|
||||||
|
|
||||||
@ -592,10 +585,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn not_filterable() {
|
fn not_filterable() {
|
||||||
let path = tempfile::tempdir().unwrap();
|
let index = TempIndex::new();
|
||||||
let mut options = EnvOpenOptions::new();
|
|
||||||
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
||||||
let index = Index::new(options, &path).unwrap();
|
|
||||||
|
|
||||||
let rtxn = index.read_txn().unwrap();
|
let rtxn = index.read_txn().unwrap();
|
||||||
let filter = Filter::from_str("_geoRadius(42, 150, 10)").unwrap().unwrap();
|
let filter = Filter::from_str("_geoRadius(42, 150, 10)").unwrap().unwrap();
|
||||||
@ -611,14 +601,12 @@ mod tests {
|
|||||||
));
|
));
|
||||||
drop(rtxn);
|
drop(rtxn);
|
||||||
|
|
||||||
let config = IndexerConfig::default();
|
index
|
||||||
// Set the filterable fields to be the channel.
|
.update_settings(|settings| {
|
||||||
let mut wtxn = index.write_txn().unwrap();
|
settings.set_searchable_fields(vec![S("title")]);
|
||||||
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
settings.set_filterable_fields(hashset! { S("title") });
|
||||||
builder.set_searchable_fields(vec![S("title")]);
|
})
|
||||||
builder.set_filterable_fields(hashset! { S("title") });
|
.unwrap();
|
||||||
builder.execute(|_| ()).unwrap();
|
|
||||||
wtxn.commit().unwrap();
|
|
||||||
|
|
||||||
let rtxn = index.read_txn().unwrap();
|
let rtxn = index.read_txn().unwrap();
|
||||||
|
|
||||||
@ -637,92 +625,64 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn escaped_quote_in_filter_value_2380() {
|
fn escaped_quote_in_filter_value_2380() {
|
||||||
let path = tempfile::tempdir().unwrap();
|
let index = TempIndex::new();
|
||||||
let mut options = EnvOpenOptions::new();
|
|
||||||
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
||||||
let index = Index::new(options, &path).unwrap();
|
|
||||||
|
|
||||||
let mut wtxn = index.write_txn().unwrap();
|
index
|
||||||
let content = documents!([
|
.add_documents(documents!([
|
||||||
{
|
{
|
||||||
"id": "test_1",
|
"id": "test_1",
|
||||||
"monitor_diagonal": "27' to 30'"
|
"monitor_diagonal": "27' to 30'"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "test_2",
|
"id": "test_2",
|
||||||
"monitor_diagonal": "27\" to 30\""
|
"monitor_diagonal": "27\" to 30\""
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "test_3",
|
"id": "test_3",
|
||||||
"monitor_diagonal": "27\" to 30'"
|
"monitor_diagonal": "27\" to 30'"
|
||||||
},
|
},
|
||||||
]);
|
]))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let config = IndexerConfig::default();
|
index
|
||||||
let indexing_config = IndexDocumentsConfig::default();
|
.update_settings(|settings| {
|
||||||
let builder =
|
settings.set_filterable_fields(hashset!(S("monitor_diagonal")));
|
||||||
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ())
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let (builder, user_error) = builder.add_documents(content).unwrap();
|
|
||||||
user_error.unwrap();
|
|
||||||
builder.execute().unwrap();
|
|
||||||
|
|
||||||
wtxn.commit().unwrap();
|
|
||||||
|
|
||||||
let mut wtxn = index.write_txn().unwrap();
|
|
||||||
let mut builder = update::Settings::new(&mut wtxn, &index, &config);
|
|
||||||
|
|
||||||
builder.set_filterable_fields(hashset!(S("monitor_diagonal")));
|
|
||||||
builder.execute(|_| ()).unwrap();
|
|
||||||
wtxn.commit().unwrap();
|
|
||||||
|
|
||||||
let rtxn = index.read_txn().unwrap();
|
let rtxn = index.read_txn().unwrap();
|
||||||
|
|
||||||
let mut search = crate::Search::new(&rtxn, &index);
|
let mut search = crate::Search::new(&rtxn, &index);
|
||||||
// this filter is copy pasted from #2380 with the exact same espace sequence
|
// this filter is copy pasted from #2380 with the exact same espace sequence
|
||||||
search.filter(
|
search.filter(Filter::from_str("monitor_diagonal = '27\" to 30\\''").unwrap().unwrap());
|
||||||
crate::Filter::from_str("monitor_diagonal = '27\" to 30\\''").unwrap().unwrap(),
|
|
||||||
);
|
|
||||||
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
||||||
assert_eq!(documents_ids, vec![2]);
|
assert_eq!(documents_ids, vec![2]);
|
||||||
|
|
||||||
search.filter(
|
search.filter(Filter::from_str(r#"monitor_diagonal = "27' to 30'" "#).unwrap().unwrap());
|
||||||
crate::Filter::from_str(r#"monitor_diagonal = "27' to 30'" "#).unwrap().unwrap(),
|
|
||||||
);
|
|
||||||
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
||||||
assert_eq!(documents_ids, vec![0]);
|
assert_eq!(documents_ids, vec![0]);
|
||||||
|
|
||||||
search.filter(
|
search.filter(Filter::from_str(r#"monitor_diagonal = "27\" to 30\"" "#).unwrap().unwrap());
|
||||||
crate::Filter::from_str(r#"monitor_diagonal = "27\" to 30\"" "#).unwrap().unwrap(),
|
|
||||||
);
|
|
||||||
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
||||||
assert_eq!(documents_ids, vec![1]);
|
assert_eq!(documents_ids, vec![1]);
|
||||||
|
|
||||||
search.filter(
|
search.filter(Filter::from_str(r#"monitor_diagonal = "27\" to 30'" "#).unwrap().unwrap());
|
||||||
crate::Filter::from_str(r#"monitor_diagonal = "27\" to 30'" "#).unwrap().unwrap(),
|
|
||||||
);
|
|
||||||
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
||||||
assert_eq!(documents_ids, vec![2]);
|
assert_eq!(documents_ids, vec![2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn geo_radius_error() {
|
fn geo_radius_error() {
|
||||||
let path = tempfile::tempdir().unwrap();
|
let index = TempIndex::new();
|
||||||
let mut options = EnvOpenOptions::new();
|
|
||||||
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
||||||
let index = Index::new(options, &path).unwrap();
|
|
||||||
|
|
||||||
let config = IndexerConfig::default();
|
index
|
||||||
// Set the filterable fields to be the channel.
|
.update_settings(|settings| {
|
||||||
let mut wtxn = index.write_txn().unwrap();
|
settings.set_searchable_fields(vec![S("_geo"), S("price")]); // to keep the fields order
|
||||||
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
settings.set_filterable_fields(hashset! { S("_geo"), S("price") });
|
||||||
builder.set_searchable_fields(vec![S("_geo"), S("price")]); // to keep the fields order
|
})
|
||||||
builder.set_filterable_fields(hashset! { S("_geo"), S("price") });
|
.unwrap();
|
||||||
builder.execute(|_| ()).unwrap();
|
|
||||||
wtxn.commit().unwrap();
|
|
||||||
|
|
||||||
let rtxn = index.read_txn().unwrap();
|
let rtxn = index.read_txn().unwrap();
|
||||||
|
|
||||||
// georadius have a bad latitude
|
// georadius have a bad latitude
|
||||||
let filter = Filter::from_str("_geoRadius(-100, 150, 10)").unwrap().unwrap();
|
let filter = Filter::from_str("_geoRadius(-100, 150, 10)").unwrap().unwrap();
|
||||||
let error = filter.evaluate(&rtxn, &index).unwrap_err();
|
let error = filter.evaluate(&rtxn, &index).unwrap_err();
|
||||||
|
@ -1578,7 +1578,6 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn index_documents_check_exists_database() {
|
fn index_documents_check_exists_database() {
|
||||||
|
|
||||||
let content = || {
|
let content = || {
|
||||||
documents!([
|
documents!([
|
||||||
{
|
{
|
||||||
@ -1640,20 +1639,24 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(bitmap_colour_green.into_iter().collect::<Vec<_>>(), vec![6, 7]);
|
assert_eq!(bitmap_colour_green.into_iter().collect::<Vec<_>>(), vec![6, 7]);
|
||||||
};
|
};
|
||||||
|
|
||||||
let faceted_fields = hashset!(S("colour"));
|
let faceted_fields = hashset!(S("colour"));
|
||||||
|
|
||||||
let index = TempIndex::new();
|
let index = TempIndex::new();
|
||||||
index.add_documents(content()).unwrap();
|
index.add_documents(content()).unwrap();
|
||||||
index.update_settings(|settings| {
|
index
|
||||||
settings.set_filterable_fields(faceted_fields.clone());
|
.update_settings(|settings| {
|
||||||
}).unwrap();
|
settings.set_filterable_fields(faceted_fields.clone());
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
check_ok(&index);
|
check_ok(&index);
|
||||||
|
|
||||||
let index = TempIndex::new();
|
let index = TempIndex::new();
|
||||||
index.update_settings(|settings| {
|
index
|
||||||
settings.set_filterable_fields(faceted_fields.clone());
|
.update_settings(|settings| {
|
||||||
}).unwrap();
|
settings.set_filterable_fields(faceted_fields.clone());
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
index.add_documents(content()).unwrap();
|
index.add_documents(content()).unwrap();
|
||||||
check_ok(&index);
|
check_ok(&index);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user