From ae66b00f5f7f44ac06c13b14a5a1a42a1953497a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 7 Oct 2016 18:10:37 +0200 Subject: [PATCH] store tests: Add erroring-hook-tests --- libimagstore/src/store.rs | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/libimagstore/src/store.rs b/libimagstore/src/store.rs index 07511e28..919b6f50 100644 --- a/libimagstore/src/store.rs +++ b/libimagstore/src/store.rs @@ -2594,5 +2594,91 @@ aspect = "test" assert!(store.create(pb.clone()).is_ok()); } + + fn get_store_with_aborting_hook_at_pos(pos: HP) -> Store { + let mut store = get_store_with_config(); + let hook = TestHook::new(pos.clone(), false, true); + + assert!(store.register_hook(pos, "test", Box::new(hook)).map_err(|e| println!("{:?}", e)).is_ok()); + store + } + + fn default_test_id() -> StoreId { + StoreId::new_baseless(PathBuf::from("test")).unwrap() + } + + #[test] + fn test_pre_create_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PreCreate); + assert!(store.create(default_test_id()).is_err()); + } + + #[test] + fn test_pre_retrieve_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PreRetrieve); + assert!(store.retrieve(default_test_id()).is_err()); + } + + #[test] + fn test_pre_delete_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PreDelete); + assert!(store.delete(default_test_id()).is_err()); + } + + #[test] + fn test_pre_update_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PreUpdate); + let fle = store.create(default_test_id()).unwrap(); + + assert!(store.update(fle).is_err()); + } + + #[test] + fn test_post_create_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PostCreate); + let pb = default_test_id(); + + assert!(store.create(pb.clone()).is_err()); + + // But the entry exists, as the hook fails post-create + assert!(store.entries.read().unwrap().get(&pb.with_base(store.path().clone())).is_some()); + } + + #[test] + fn test_post_retrieve_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PostRetrieve); + let pb = default_test_id(); + + assert!(store.retrieve(pb.clone()).is_err()); + + // But the entry exists, as the hook fails post-retrieve + assert!(store.entries.read().unwrap().get(&pb.with_base(store.path().clone())).is_some()); + } + + #[test] + fn test_post_delete_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PostDelete); + let pb = default_test_id(); + + assert!(store.create(pb.clone()).is_ok()); + let pb = pb.with_base(store.path().clone()); + assert!(store.entries.read().unwrap().get(&pb).is_some()); + + assert!(store.delete(pb.clone()).is_err()); + // But the entry is removed, as we fail post-delete + assert!(store.entries.read().unwrap().get(&pb).is_none()); + } + + #[test] + fn test_post_update_error() { + let store = get_store_with_aborting_hook_at_pos(HP::PostUpdate); + let pb = default_test_id(); + let fle = store.create(pb.clone()).unwrap(); + let pb = pb.with_base(store.path().clone()); + + assert!(store.entries.read().unwrap().get(&pb).is_some()); + assert!(store.update(fle).is_err()); + } + }