add tests

This commit is contained in:
mpostma 2021-02-09 16:08:13 +01:00
parent 4ca46b9e5f
commit 9fb0d94fc3
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
2 changed files with 59 additions and 5 deletions

View File

@ -133,7 +133,7 @@ impl IndexStore {
} }
/// Use this function to perform an update on an index. /// Use this function to perform an update on an index.
/// This function also puts a lock on what index in allowed to perform an update. /// This function also puts a lock on what index is allowed to perform an update.
pub fn update_index<F, T>(&self, name: impl AsRef<str>, f: F) -> anyhow::Result<(T, IndexMeta)> pub fn update_index<F, T>(&self, name: impl AsRef<str>, f: F) -> anyhow::Result<(T, IndexMeta)>
where where
F: FnOnce(&Index) -> anyhow::Result<T>, F: FnOnce(&Index) -> anyhow::Result<T>,
@ -167,7 +167,10 @@ impl IndexStore {
} }
} }
fn update_meta(&self, txn: &mut RwTxn, name: impl AsRef<str>, f: impl FnOnce(&mut IndexMeta)) -> anyhow::Result<IndexMeta> { fn update_meta<F>(&self, txn: &mut RwTxn, name: impl AsRef<str>, f: F) -> anyhow::Result<IndexMeta>
where
F: FnOnce(&mut IndexMeta)
{
let uuid = self.index_uuid(txn, &name)? let uuid = self.index_uuid(txn, &name)?
.with_context(|| format!("Index {:?} doesn't exist", name.as_ref()))?; .with_context(|| format!("Index {:?} doesn't exist", name.as_ref()))?;
let mut meta = self.uuid_to_index_meta let mut meta = self.uuid_to_index_meta

View File

@ -10,7 +10,9 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use milli::Index; use milli::Index;
use milli::update::{IndexDocumentsMethod, UpdateFormat, DocumentAdditionResult}; use serde::{Serialize, Deserialize, de::Deserializer}; use uuid::Uuid; use milli::update::{IndexDocumentsMethod, UpdateFormat, DocumentAdditionResult};
use serde::{Serialize, Deserialize, de::Deserializer};
use uuid::Uuid;
pub use updates::{Processed, Processing, Failed}; pub use updates::{Processed, Processing, Failed};
@ -95,6 +97,7 @@ pub enum UpdateResult {
Other, Other,
} }
#[derive(Clone, Debug)]
pub struct IndexSettings { pub struct IndexSettings {
pub name: Option<String>, pub name: Option<String>,
pub primary_key: Option<String>, pub primary_key: Option<String>,
@ -183,10 +186,15 @@ pub(crate) mod test {
fn test_create_index_with_no_name_is_error() { fn test_create_index_with_no_name_is_error() {
crate::index_controller::test::create_index_with_no_name_is_error($controller_buider); crate::index_controller::test::create_index_with_no_name_is_error($controller_buider);
} }
#[test]
fn test_update_index() {
crate::index_controller::test::update_index($controller_buider);
}
}; };
} }
pub(crate) fn create_and_list_indexes<S: IndexController>(controller: S) { pub(crate) fn create_and_list_indexes(controller: impl IndexController) {
let settings1 = IndexSettings { let settings1 = IndexSettings {
name: Some(String::from("test_index")), name: Some(String::from("test_index")),
primary_key: None, primary_key: None,
@ -207,11 +215,54 @@ pub(crate) mod test {
assert_eq!(indexes[1].primary_key.clone().unwrap(), "foo"); assert_eq!(indexes[1].primary_key.clone().unwrap(), "foo");
} }
pub(crate) fn create_index_with_no_name_is_error<S: IndexController>(controller: S) { pub(crate) fn create_index_with_no_name_is_error(controller: impl IndexController) {
let settings = IndexSettings { let settings = IndexSettings {
name: None, name: None,
primary_key: None, primary_key: None,
}; };
assert!(controller.create_index(settings).is_err()); assert!(controller.create_index(settings).is_err());
} }
pub(crate) fn update_index(controller: impl IndexController) {
let settings = IndexSettings {
name: Some(String::from("test")),
primary_key: None,
};
assert!(controller.create_index(settings).is_ok());
// perform empty update returns index meta unchanged
let settings = IndexSettings {
name: None,
primary_key: None,
};
let result = controller.update_index("test", settings).unwrap();
assert_eq!(result.name, "test");
assert_eq!(result.created_at, result.updated_at);
assert!(result.primary_key.is_none());
// Changing the name trigger an error
let settings = IndexSettings {
name: Some(String::from("bar")),
primary_key: None,
};
assert!(controller.update_index("test", settings).is_err());
// Update primary key
let settings = IndexSettings {
name: None,
primary_key: Some(String::from("foo")),
};
let result = controller.update_index("test", settings.clone()).unwrap();
assert_eq!(result.name, "test");
assert!(result.created_at < result.updated_at);
assert_eq!(result.primary_key.unwrap(), "foo");
// setting the primary key again is an error
assert!(controller.update_index("test", settings).is_err());
}
} }