mirror of
				https://github.com/meilisearch/meilisearch.git
				synced 2025-10-30 23:46:28 +00:00 
			
		
		
		
	add IndexResolver BatchHandler tests
This commit is contained in:
		| @@ -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<U, I> { | ||||
|         Real(super::real::IndexResolver<U, I>), | ||||
| @@ -494,14 +479,16 @@ mod test { | ||||
|         pub async fn process_document_addition_batch(&self, tasks: Vec<Task>) -> Vec<Task> { | ||||
|             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<TaskResult> { | ||||
|             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) | ||||
|                 }, | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -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::<Task>(), | ||||
|         ) { | ||||
|             let rt = tokio::runtime::Runtime::new().unwrap(); | ||||
|             let handle = rt.spawn(async { | ||||
|                 let mocker = Mocker::default(); | ||||
|  | ||||
|                 if let TaskContent::DocumentAddition{ .. } = task.content { | ||||
|                     mocker.when::<Uuid, IndexResult<()>>("delete_content_file").then(|_| Ok(())); | ||||
|                 } | ||||
|  | ||||
|                 let index_resolver: IndexResolver<HeedMetaStore, MapIndexStore> = 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::<Task>()) { | ||||
|             let rt = tokio::runtime::Runtime::new().unwrap(); | ||||
|             let handle = rt.spawn(async { | ||||
|                 let mocker = Mocker::default(); | ||||
|                 match task.content { | ||||
|                     TaskContent::DocumentAddition { .. } => { | ||||
|                         mocker.when::<Vec<Task>, Vec<Task>>("process_document_addition_batch").then(|tasks| tasks); | ||||
|                     } | ||||
|                     TaskContent::Dump { .. } => (), | ||||
|                     _ => { | ||||
|                         mocker.when::<&Task, IndexResult<TaskResult>>("process_task").then(|_| Ok(TaskResult::Other)); | ||||
|                     } | ||||
|                 } | ||||
|                 let index_resolver: IndexResolver<HeedMetaStore, MapIndexStore> = 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()); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user