Skip to content

Commit

Permalink
Add signed int key Map migration test / example
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Dec 23, 2021
1 parent 35a987e commit f6564ca
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions packages/storage-plus/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ mod test {
use cosmwasm_std::{Order, StdResult};

use crate::int_key::CwIntKey;
use crate::keys_old::IntKeyOld;

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
struct Data {
Expand All @@ -266,6 +267,8 @@ mod test {
#[cfg(feature = "iterator")]
const PEOPLE_ID: Map<u32, Data> = Map::new("people_id");
#[cfg(feature = "iterator")]
const SIGNED_ID_OLD: Map<IntKeyOld<i32>, Data> = Map::new("signed_id");
#[cfg(feature = "iterator")]
const SIGNED_ID: Map<i32, Data> = Map::new("signed_id");

const ALLOWANCE: Map<(&[u8], &[u8]), u64> = Map::new("allow");
Expand Down Expand Up @@ -635,6 +638,77 @@ mod test {
assert_eq!(all, vec![(50, data3)]);
}

#[test]
#[cfg(feature = "iterator")]
fn range_signed_integer_key_migration() {
let mut store = MockStorage::new();

// save and load three keys with the old format
let data = Data {
name: "John".to_string(),
age: 32,
};
SIGNED_ID_OLD
.save(&mut store, IntKeyOld::<i32>::from(-1234), &data)
.unwrap();

let data2 = Data {
name: "Jim".to_string(),
age: 44,
};
SIGNED_ID_OLD
.save(&mut store, IntKeyOld::<i32>::from(-56), &data2)
.unwrap();

let data3 = Data {
name: "Jules".to_string(),
age: 55,
};
SIGNED_ID_OLD
.save(&mut store, IntKeyOld::<i32>::from(50), &data3)
.unwrap();

// obtain all current keys
let current = SIGNED_ID_OLD
.range(&store, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()
.unwrap();
// confirm wrong current order
assert_eq!(
current,
vec![
(50, data3.clone()),
(-1234, data.clone()),
(-56, data2.clone())
]
);

// remove old entries
for (k, _) in current.iter() {
SIGNED_ID_OLD.remove(&mut store, IntKeyOld::<i32>::from(*k));
}

// confirm map is empty
assert!(SIGNED_ID_OLD
.range(&store, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()
.unwrap()
.is_empty());

// save in new format
for (k, v) in current.iter() {
SIGNED_ID.save(&mut store, *k, v).unwrap();
}

// obtain new keys
let new = SIGNED_ID
.range(&store, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()
.unwrap();
// confirm new order is right
assert_eq!(new, vec![(-1234, data), (-56, data2), (50, data3)]);
}

#[test]
#[cfg(feature = "iterator")]
fn range_raw_composite_key() {
Expand Down

0 comments on commit f6564ca

Please sign in to comment.