diff --git a/packages/storage-plus/src/indexed_snapshot.rs b/packages/storage-plus/src/indexed_snapshot.rs index 23c58a78d..f311ed2d5 100644 --- a/packages/storage-plus/src/indexed_snapshot.rs +++ b/packages/storage-plus/src/indexed_snapshot.rs @@ -990,4 +990,187 @@ mod test { assert_eq!(datas[0], marias[0].1); assert_eq!(datas[1], marias[1].1); } + + #[test] + #[cfg(feature = "iterator")] + fn range_de_simple_string_key() { + let mut store = MockStorage::new(); + let map = build_snapshot_map(); + + // save data + let (pks, datas) = save_data(&mut store, &map); + + // let's try to iterate! + let all: StdResult> = map.range_de(&store, None, None, Order::Ascending).collect(); + let all = all.unwrap(); + assert_eq!( + all, + pks.clone() + .into_iter() + .map(str::to_string) + .zip(datas.clone().into_iter()) + .collect::>() + ); + + // let's try to iterate over a range + let all: StdResult> = map + .range_de( + &store, + Some(Bound::Inclusive(b"3".to_vec())), + None, + Order::Ascending, + ) + .collect(); + let all = all.unwrap(); + assert_eq!( + all, + pks.into_iter() + .map(str::to_string) + .zip(datas.into_iter()) + .rev() + .take(2) + .rev() + .collect::>() + ); + } + + #[test] + #[cfg(feature = "iterator")] + fn prefix_de_simple_string_key() { + let mut store = MockStorage::new(); + let map = build_snapshot_map(); + + // save data + let (pks, datas) = save_data(&mut store, &map); + + // Let's prefix and iterate. + // This is similar to calling range() directly, but added here for completeness / prefix_de + // type checks + let all: StdResult> = map + .prefix_de(()) + .range_de(&store, None, None, Order::Ascending) + .collect(); + let all = all.unwrap(); + assert_eq!( + all, + pks.clone() + .into_iter() + .map(str::to_string) + .zip(datas.into_iter()) + .collect::>() + ); + } + + #[test] + #[cfg(feature = "iterator")] + fn sub_prefix_de_simple_string_key() { + let mut store = MockStorage::new(); + let map = build_snapshot_map(); + + // save data + let (pks, datas) = save_data(&mut store, &map); + + // Let's prefix and iterate. + // This is similar to calling range() directly, but added here for completeness / sub_prefix_de + // type checks + let all: StdResult> = map + .sub_prefix_de(()) + .range_de(&store, None, None, Order::Ascending) + .collect(); + let all = all.unwrap(); + assert_eq!( + all, + pks.clone() + .into_iter() + .map(str::to_string) + .zip(datas.into_iter()) + .collect::>() + ); + } + + #[test] + #[cfg(feature = "iterator")] + fn prefix_range_de_simple_key() { + let mut store = MockStorage::new(); + + let indexes = DataCompositeMultiIndex { + name_age: MultiIndex::new( + |d, k| index_triple(&d.name, d.age, k), + "data", + "data__name_age", + ), + }; + let map = + IndexedSnapshotMap::new("data", "checks", "changes", Strategy::EveryBlock, indexes); + + // save data + let data1 = Data { + name: "Maria".to_string(), + last_name: "".to_string(), + age: 42, + }; + let pk1: (&str, &str) = ("1", "5627"); + map.save(&mut store, pk1, &data1, 1).unwrap(); + + let data2 = Data { + name: "Juan".to_string(), + last_name: "Perez".to_string(), + age: 13, + }; + let pk2: (&str, &str) = ("2", "5628"); + map.save(&mut store, pk2, &data2, 1).unwrap(); + + let data3 = Data { + name: "Maria".to_string(), + last_name: "Young".to_string(), + age: 24, + }; + let pk3: (&str, &str) = ("2", "5629"); + map.save(&mut store, pk3, &data3, 1).unwrap(); + + let data4 = Data { + name: "Maria Luisa".to_string(), + last_name: "Bemberg".to_string(), + age: 43, + }; + let pk4: (&str, &str) = ("3", "5630"); + map.save(&mut store, pk4, &data4, 1).unwrap(); + + // let's try to iterate! + let result: StdResult> = map + .prefix_range_de( + &store, + Some(PrefixBound::inclusive("2")), + None, + Order::Ascending, + ) + .collect(); + let result = result.unwrap(); + assert_eq!( + result, + [ + (("2".to_string(), "5628".to_string()), data2.clone()), + (("2".to_string(), "5629".to_string()), data3.clone()), + (("3".to_string(), "5630".to_string()), data4) + ] + ); + + // let's try to iterate over a range + let result: StdResult> = map + .prefix_range_de( + &store, + Some(PrefixBound::inclusive("2")), + Some(PrefixBound::exclusive("3")), + Order::Ascending, + ) + .collect(); + let result = result.unwrap(); + assert_eq!( + result, + [ + (("2".to_string(), "5628".to_string()), data2), + (("2".to_string(), "5629".to_string()), data3), + ] + ); + } }