From 91dc75b07b3ba8d973600809ed902cdd8c43eca0 Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Tue, 11 Nov 2025 16:42:07 +0100 Subject: [PATCH] test: add base test for catalog.for_each_mut --- .../heave/src/tst/catalog_for_each_mut.rs | 54 +++++++++++++++++++ 01.workspace/heave/src/tst/mod.rs | 1 + 2 files changed, 55 insertions(+) create mode 100644 01.workspace/heave/src/tst/catalog_for_each_mut.rs diff --git a/01.workspace/heave/src/tst/catalog_for_each_mut.rs b/01.workspace/heave/src/tst/catalog_for_each_mut.rs new file mode 100644 index 0000000..f6afe0c --- /dev/null +++ b/01.workspace/heave/src/tst/catalog_for_each_mut.rs @@ -0,0 +1,54 @@ +#[cfg(test)] +mod tests { + use crate::*; + fn prepare_catalog(catalog: &mut Catalog) { + let items = vec![ + Item { + id: "1".to_string(), + price: 100, + ..Default::default() + }, + Item { + id: "2".to_string(), + price: 200, + ..Default::default() + }, + Item { + id: "3".to_string(), + price: 300, + ..Default::default() + }, + ]; + let _ = catalog.insert_many(items); + } + #[test] + fn all_prices_should_be_changed() { + let mut catalog = Catalog::new("test.db"); + prepare_catalog(&mut catalog); + let _ = catalog.for_each_mut(|item: &mut Item| { + item.price += 50; + Ok(()) + }); + let item1: Item = catalog.get("1").unwrap().unwrap(); + let item2: Item = catalog.get("2").unwrap().unwrap(); + let item3: Item = catalog.get("3").unwrap().unwrap(); + assert_eq!(item1.price, 150); + assert_eq!(item2.price, 250); + assert_eq!(item3.price, 350); + let _ = catalog.with_items(|items| { + let entity = items.get("1").unwrap(); + assert_eq!(entity.state, EntityState::Updated); + Ok(()) + }); + let _ = catalog.with_items(|items| { + let entity = items.get("2").unwrap(); + assert_eq!(entity.state, EntityState::Updated); + Ok(()) + }); + let _ = catalog.with_items(|items| { + let entity = items.get("3").unwrap(); + assert_eq!(entity.state, EntityState::Updated); + Ok(()) + }); + } +} diff --git a/01.workspace/heave/src/tst/mod.rs b/01.workspace/heave/src/tst/mod.rs index 3e3923a..d0fcfde 100644 --- a/01.workspace/heave/src/tst/mod.rs +++ b/01.workspace/heave/src/tst/mod.rs @@ -1,5 +1,6 @@ pub mod catalog_delete; pub mod catalog_for_each; +pub mod catalog_for_each_mut; pub mod catalog_get; pub mod catalog_get_by; pub mod catalog_init;