From 6ac8675c6dec19936716405f370acdec71a9a0cd Mon Sep 17 00:00:00 2001 From: ad hoc Date: Thu, 2 Jun 2022 12:15:36 +0200 Subject: [PATCH] add IndexResolver BatchHandler tests --- meilisearch-lib/src/index_resolver/mod.rs | 31 +++------- .../tasks/handlers/index_resolver_handler.rs | 62 ++++++++++++++++++- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/meilisearch-lib/src/index_resolver/mod.rs b/meilisearch-lib/src/index_resolver/mod.rs index 3d76f3b6c..32970fc37 100644 --- a/meilisearch-lib/src/index_resolver/mod.rs +++ b/meilisearch-lib/src/index_resolver/mod.rs @@ -438,24 +438,9 @@ mod real { #[cfg(test)] mod test { - // use std::{collections::BTreeMap, vec::IntoIter}; - // - // use super::*; - // - // use futures::future::ok; - // use milli::update::{DocumentAdditionResult, IndexDocumentsMethod}; - // use nelson::Mocker; - // use proptest::prelude::*; - // - // use crate::{ - // index::{ - // error::{IndexError, Result as IndexResult}, - // Checked, IndexMeta, IndexStats, Settings, - // }, - // tasks::{batch::Batch, BatchHandler}, - // }; - // use index_store::MockIndexStore; - // use meta_store::MockIndexMetaStore; + use super::*; + + use nelson::Mocker; pub enum MockIndexResolver { Real(super::real::IndexResolver), @@ -494,14 +479,16 @@ mod test { pub async fn process_document_addition_batch(&self, tasks: Vec) -> Vec { match self { IndexResolver::Real(r) => r.process_document_addition_batch(tasks).await, - IndexResolver::Mock(_) => todo!(), + IndexResolver::Mock(m) => unsafe { + m.get("process_document_addition_batch").call(tasks) + }, } } pub async fn process_task(&self, task: &Task) -> Result { match self { IndexResolver::Real(r) => r.process_task(task).await, - IndexResolver::Mock(_) => todo!(), + IndexResolver::Mock(m) => unsafe { m.get("process_task").call(task) }, } } @@ -551,7 +538,9 @@ mod test { pub async fn delete_content_file(&self, content_uuid: Uuid) -> Result<()> { match self { IndexResolver::Real(r) => r.delete_content_file(content_uuid).await, - IndexResolver::Mock(_) => todo!(), + IndexResolver::Mock(m) => unsafe { + m.get("delete_content_file").call(content_uuid) + }, } } } diff --git a/meilisearch-lib/src/tasks/handlers/index_resolver_handler.rs b/meilisearch-lib/src/tasks/handlers/index_resolver_handler.rs index d313ea33d..de624106c 100644 --- a/meilisearch-lib/src/tasks/handlers/index_resolver_handler.rs +++ b/meilisearch-lib/src/tasks/handlers/index_resolver_handler.rs @@ -49,7 +49,12 @@ where #[cfg(test)] mod test { - use crate::index_resolver::{index_store::MockIndexStore, meta_store::MockIndexMetaStore}; + use crate::index_resolver::index_store::MapIndexStore; + use crate::index_resolver::meta_store::HeedMetaStore; + use crate::index_resolver::{ + error::Result as IndexResult, index_store::MockIndexStore, meta_store::MockIndexMetaStore, + }; + use crate::tasks::task::TaskResult; use crate::tasks::{ handlers::test::task_to_batch, task::{Task, TaskContent}, @@ -142,5 +147,58 @@ mod test { index_resolver.process_batch(batch).await; } - // TODO: test perform_batch. We need a Mocker for IndexResolver. + proptest! { + #[test] + fn index_document_task_deletes_update_file( + task in any::(), + ) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let handle = rt.spawn(async { + let mocker = Mocker::default(); + + if let TaskContent::DocumentAddition{ .. } = task.content { + mocker.when::>("delete_content_file").then(|_| Ok(())); + } + + let index_resolver: IndexResolver = IndexResolver::mock(mocker); + + let batch = task_to_batch(task); + + index_resolver.finish(&batch).await; + }); + + rt.block_on(handle).unwrap(); + } + + #[test] + fn test_handle_batch(task in any::()) { + let rt = tokio::runtime::Runtime::new().unwrap(); + let handle = rt.spawn(async { + let mocker = Mocker::default(); + match task.content { + TaskContent::DocumentAddition { .. } => { + mocker.when::, Vec>("process_document_addition_batch").then(|tasks| tasks); + } + TaskContent::Dump { .. } => (), + _ => { + mocker.when::<&Task, IndexResult>("process_task").then(|_| Ok(TaskResult::Other)); + } + } + let index_resolver: IndexResolver = IndexResolver::mock(mocker); + + + let batch = task_to_batch(task); + + if index_resolver.accept(&batch) { + index_resolver.process_batch(batch).await; + } + }); + + if let Err(e) = rt.block_on(handle) { + if e.is_panic() { + std::panic::resume_unwind(e.into_panic()); + } + } + } + } }