From a2a9fd70f4f02ab621f654c9f5320ff96e31c934 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 19 Nov 2021 20:05:13 +0100 Subject: [PATCH] Add range_de unique index tests --- packages/storage-plus/src/indexed_snapshot.rs | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/packages/storage-plus/src/indexed_snapshot.rs b/packages/storage-plus/src/indexed_snapshot.rs index df8b9d856..23c58a78d 100644 --- a/packages/storage-plus/src/indexed_snapshot.rs +++ b/packages/storage-plus/src/indexed_snapshot.rs @@ -311,8 +311,10 @@ mod test { struct DataIndexes<'a> { // Second arg is for storing pk pub name: MultiIndex<'a, (Vec, String), Data>, - pub age: UniqueIndex<'a, U32Key, Data>, - pub name_lastname: UniqueIndex<'a, (Vec, Vec), Data>, + // Last generic type arg is pk deserialization type + pub age: UniqueIndex<'a, U32Key, Data, String>, + // Last generic type arg is pk deserialization type + pub name_lastname: UniqueIndex<'a, (Vec, Vec), Data, String>, } // Future Note: this can likely be macro-derived @@ -900,6 +902,37 @@ mod test { assert_eq!(datas[0], ages[3].1); } + #[test] + fn range_de_simple_key_by_unique_index() { + let mut store = MockStorage::new(); + let map = build_snapshot_map(); + + // save data + let (pks, datas) = save_data(&mut store, &map); + + let res: StdResult> = map + .idx + .age + .range_de(&store, None, None, Order::Ascending) + .collect(); + let ages = res.unwrap(); + + let count = ages.len(); + assert_eq!(4, count); + + // The pks, sorted by age ascending + assert_eq!(pks[3], ages[0].0); + assert_eq!(pks[1], ages[1].0); + assert_eq!(pks[2], ages[2].0); + assert_eq!(pks[0], ages[3].0); + + // The associated data + assert_eq!(datas[3], ages[0].1); + assert_eq!(datas[1], ages[1].1); + assert_eq!(datas[2], ages[2].1); + assert_eq!(datas[0], ages[3].1); + } + #[test] fn range_composite_key_by_unique_index() { let mut store = MockStorage::new(); @@ -928,4 +961,33 @@ mod test { assert_eq!(datas[0], marias[0].1); assert_eq!(datas[1], marias[1].1); } + + #[test] + fn range_de_composite_key_by_unique_index() { + let mut store = MockStorage::new(); + let map = build_snapshot_map(); + + // save data + let (pks, datas) = save_data(&mut store, &map); + + let res: StdResult> = map + .idx + .name_lastname + .prefix_de(b"Maria".to_vec()) + .range_de(&store, None, None, Order::Ascending) + .collect(); + let marias = res.unwrap(); + + // Only two people are called "Maria" + let count = marias.len(); + assert_eq!(2, count); + + // The pks + assert_eq!(pks[0], marias[0].0); + assert_eq!(pks[1], marias[1].0); + + // The associated data + assert_eq!(datas[0], marias[0].1); + assert_eq!(datas[1], marias[1].1); + } }