add IndexResolver BatchHandler tests

This commit is contained in:
ad hoc 2022-06-02 12:15:36 +02:00
parent df61ca9cae
commit 6ac8675c6d
No known key found for this signature in database
GPG Key ID: 4F00A782990CC643
2 changed files with 70 additions and 23 deletions

View File

@ -438,24 +438,9 @@ mod real {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
// use std::{collections::BTreeMap, vec::IntoIter}; use super::*;
//
// use super::*; use nelson::Mocker;
//
// 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;
pub enum MockIndexResolver<U, I> { pub enum MockIndexResolver<U, I> {
Real(super::real::IndexResolver<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> { pub async fn process_document_addition_batch(&self, tasks: Vec<Task>) -> Vec<Task> {
match self { match self {
IndexResolver::Real(r) => r.process_document_addition_batch(tasks).await, 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> { pub async fn process_task(&self, task: &Task) -> Result<TaskResult> {
match self { match self {
IndexResolver::Real(r) => r.process_task(task).await, 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<()> { pub async fn delete_content_file(&self, content_uuid: Uuid) -> Result<()> {
match self { match self {
IndexResolver::Real(r) => r.delete_content_file(content_uuid).await, 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)
},
} }
} }
} }

View File

@ -49,7 +49,12 @@ where
#[cfg(test)] #[cfg(test)]
mod 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::{ use crate::tasks::{
handlers::test::task_to_batch, handlers::test::task_to_batch,
task::{Task, TaskContent}, task::{Task, TaskContent},
@ -142,5 +147,58 @@ mod test {
index_resolver.process_batch(batch).await; 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());
}
}
}
}
} }