From c78f351300fa4d82604f3e7d3effb143b99fbabf Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Thu, 15 Apr 2021 19:54:25 +0200 Subject: [PATCH] fix tests --- .../src/index_controller/index_actor/mod.rs | 66 +++++++++++++++++++ meilisearch-http/src/index_controller/mod.rs | 2 + .../src/index_controller/snapshot.rs | 21 ++++-- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/meilisearch-http/src/index_controller/index_actor/mod.rs b/meilisearch-http/src/index_controller/index_actor/mod.rs index e44891b5a..26f7a23db 100644 --- a/meilisearch-http/src/index_controller/index_actor/mod.rs +++ b/meilisearch-http/src/index_controller/index_actor/mod.rs @@ -1,3 +1,5 @@ +#[cfg(test)] +use std::sync::Arc; use std::path::PathBuf; use chrono::{DateTime, Utc}; @@ -70,6 +72,70 @@ pub enum IndexError { ExistingPrimaryKey, } +#[cfg(test)] +#[async_trait::async_trait] +impl IndexActorHandle for Arc { + async fn create_index(&self, uuid: Uuid, primary_key: Option) -> Result { + self.as_ref().create_index(uuid, primary_key).await + } + + async fn update( + &self, + uuid: Uuid, + meta: Processing, + data: std::fs::File, + ) -> anyhow::Result { + self.as_ref().update(uuid, meta, data).await + } + + async fn search(&self, uuid: Uuid, query: SearchQuery) -> Result { + self.as_ref().search(uuid, query).await + } + + async fn settings(&self, uuid: Uuid) -> Result { + self.as_ref().settings(uuid).await + } + + async fn documents( + &self, + uuid: Uuid, + offset: usize, + limit: usize, + attributes_to_retrieve: Option>, + ) -> Result> { + self.as_ref().documents(uuid, offset, limit, attributes_to_retrieve).await + } + + async fn document( + &self, + uuid: Uuid, + doc_id: String, + attributes_to_retrieve: Option>, + ) -> Result { + self.as_ref().document(uuid, doc_id, attributes_to_retrieve).await + } + + async fn delete(&self, uuid: Uuid) -> Result<()> { + self.as_ref().delete(uuid).await + } + + async fn get_index_meta(&self, uuid: Uuid) -> Result { + self.as_ref().get_index_meta(uuid).await + } + + async fn update_index(&self, uuid: Uuid, index_settings: IndexSettings) -> Result { + self.as_ref().update_index(uuid, index_settings).await + } + + async fn snapshot(&self, uuid: Uuid, path: PathBuf) -> Result<()> { + self.as_ref().snapshot(uuid, path).await + } + + async fn get_index_stats(&self, uuid: Uuid) -> Result { + self.as_ref().get_index_stats(uuid).await + } +} + #[async_trait::async_trait] #[cfg_attr(test, automock)] pub trait IndexActorHandle { diff --git a/meilisearch-http/src/index_controller/mod.rs b/meilisearch-http/src/index_controller/mod.rs index 2e93a2f7e..35b8b1ecf 100644 --- a/meilisearch-http/src/index_controller/mod.rs +++ b/meilisearch-http/src/index_controller/mod.rs @@ -68,6 +68,7 @@ pub struct IndexSettings { } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] pub struct IndexStats { #[serde(skip)] pub size: u64, @@ -83,6 +84,7 @@ pub struct IndexController { } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] pub struct Stats { pub database_size: u64, pub last_update: Option>, diff --git a/meilisearch-http/src/index_controller/snapshot.rs b/meilisearch-http/src/index_controller/snapshot.rs index eba5a5f3b..40c72b0be 100644 --- a/meilisearch-http/src/index_controller/snapshot.rs +++ b/meilisearch-http/src/index_controller/snapshot.rs @@ -71,11 +71,9 @@ where return Ok(()); } - self.update_handle .snapshot(uuids, temp_snapshot_path.clone()) .await?; - let snapshot_dir = self.snapshot_path.clone(); let snapshot_path = self .snapshot_path @@ -133,19 +131,22 @@ pub fn load_snapshot( #[cfg(test)] mod test { + use std::sync::Arc; + use futures::future::{err, ok}; use rand::Rng; use tokio::time::timeout; use uuid::Uuid; use super::*; - use crate::index_controller::update_actor::{MockUpdateActorHandle, UpdateError}; + use crate::index_controller::update_actor::{UpdateError, MockUpdateActorHandle, UpdateActorHandleImpl}; + use crate::index_controller::index_actor::MockIndexActorHandle; use crate::index_controller::uuid_resolver::{MockUuidResolverHandle, UuidError}; #[actix_rt::test] async fn test_normal() { let mut rng = rand::thread_rng(); - let uuids_num = rng.gen_range(5, 10); + let uuids_num: usize = rng.gen_range(5, 10); let uuids = (0..uuids_num).map(|_| Uuid::new_v4()).collect::>(); let mut uuid_resolver = MockUuidResolverHandle::new(); @@ -155,13 +156,19 @@ mod test { .times(1) .returning(move |_| Box::pin(ok(uuids_clone.clone()))); - let mut update_handle = MockUpdateActorHandle::new(); let uuids_clone = uuids.clone(); - update_handle + let mut index_handle = MockIndexActorHandle::new(); + index_handle .expect_snapshot() .withf(move |uuid, _path| uuids_clone.contains(uuid)) .times(uuids_num) - .returning(move |_, _| Box::pin(ok(()))); + .returning(move |_, _| { + Box::pin(ok(())) + }); + + let dir = tempfile::tempdir_in(".").unwrap(); + let handle = Arc::new(index_handle); + let update_handle = UpdateActorHandleImpl::>::new(handle.clone(), dir.path(), 4096 * 100).unwrap(); let snapshot_path = tempfile::tempdir_in(".").unwrap(); let snapshot_service = SnapshotService::new(