Skip to content

Commit

Permalink
test IndexedMap range_de with composite keys
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Oct 20, 2021
1 parent 018f734 commit 7838470
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
33 changes: 31 additions & 2 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ mod test {
struct DataIndexes<'a> {
// Second arg is for storing pk
pub name: MultiIndex<'a, (Vec<u8>, Vec<u8>), Data>,
pub age: UniqueIndex<'a, U32Key, Data, Vec<u8>>,
pub name_lastname: UniqueIndex<'a, (Vec<u8>, Vec<u8>), Data>,
pub age: UniqueIndex<'a, U32Key, Data, String>,
pub name_lastname: UniqueIndex<'a, (Vec<u8>, Vec<u8>), Data, String>,
}

// Future Note: this can likely be macro-derived
Expand Down Expand Up @@ -826,6 +826,35 @@ mod test {
assert_eq!(datas[1], marias[1].1);
}

#[test]
fn unique_index_composite_key_range_de() {
let mut store = MockStorage::new();
let map = build_map();

// save data
let (pks, datas) = save_data(&mut store, &map);

let res: StdResult<Vec<_>> = map
.idx
.name_lastname
.prefix(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);
}

#[test]
#[cfg(feature = "iterator")]
fn range_de_simple_string_key() {
Expand Down
6 changes: 3 additions & 3 deletions packages/storage-plus/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ where
#[cfg(feature = "iterator")]
impl<'a, K, T, PK> UniqueIndex<'a, K, T, PK>
where
PK: KeyDeserialize,
PK: PrimaryKey<'a> + KeyDeserialize,
T: Serialize + DeserializeOwned + Clone,
K: PrimaryKey<'a>,
{
Expand All @@ -484,8 +484,8 @@ where
pub fn prefix_range_de<'c>(
&self,
store: &'c dyn Storage,
min: Option<PrefixBound<'a, K::Prefix>>,
max: Option<PrefixBound<'a, K::Prefix>>,
min: Option<PrefixBound<'a, PK::Prefix>>,
max: Option<PrefixBound<'a, PK::Prefix>>,
order: cosmwasm_std::Order,
) -> Box<dyn Iterator<Item = StdResult<(PK::Output, T)>> + 'c>
where
Expand Down

0 comments on commit 7838470

Please sign in to comment.