Skip to content

Commit

Permalink
[Fix] CountedMap::set now takes Counter into account (paritytech#13214)
Browse files Browse the repository at this point in the history
* [Fix] CountedMap::set now takes Counter into account

* introduce tests for StorageMap
  • Loading branch information
ruseinov authored and ark0f committed Feb 27, 2023
1 parent ff68ad1 commit 66926f0
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
55 changes: 54 additions & 1 deletion frame/support/src/storage/types/counted_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ where

/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
pub fn set<KeyArg: EncodeLike<Key>>(key: KeyArg, q: QueryKind::Query) {
<Self as MapWrapper>::Map::set(key, q)
match QueryKind::from_query_to_optional_value(q) {
Some(v) => Self::insert(key, v),
None => Self::remove(key),
}
}

/// Swap the values of two keys.
Expand Down Expand Up @@ -745,6 +748,22 @@ mod test {

// Test initialize_counter.
assert_eq!(A::initialize_counter(), 2);

// Set non-existing.
A::set(30, 100);

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), 100);
assert_eq!(A::try_get(30), Ok(100));
assert_eq!(A::count(), 3);

// Set existing.
A::set(30, 101);

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), 101);
assert_eq!(A::try_get(30), Ok(101));
assert_eq!(A::count(), 3);
})
}

Expand Down Expand Up @@ -976,6 +995,40 @@ mod test {

// Test initialize_counter.
assert_eq!(B::initialize_counter(), 2);

// Set non-existing.
B::set(30, Some(100));

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), Some(100));
assert_eq!(B::try_get(30), Ok(100));
assert_eq!(B::count(), 3);

// Set existing.
B::set(30, Some(101));

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), Some(101));
assert_eq!(B::try_get(30), Ok(101));
assert_eq!(B::count(), 3);

// Unset existing.
B::set(30, None);

assert_eq!(B::contains_key(30), false);
assert_eq!(B::get(30), None);
assert_eq!(B::try_get(30), Err(()));

assert_eq!(B::count(), 2);

// Unset non-existing.
B::set(31, None);

assert_eq!(B::contains_key(31), false);
assert_eq!(B::get(31), None);
assert_eq!(B::try_get(31), Err(()));

assert_eq!(B::count(), 2);
})
}

Expand Down
42 changes: 42 additions & 0 deletions frame/support/src/storage/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,48 @@ mod test {
assert_eq!(AValueQueryWithAnOnEmpty::take(2), 97);
assert_eq!(A::contains_key(2), false);

// Set non-existing.
B::set(30, 100);

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), 100);
assert_eq!(B::try_get(30), Ok(100));

// Set existing.
B::set(30, 101);

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), 101);
assert_eq!(B::try_get(30), Ok(101));

// Set non-existing.
A::set(30, Some(100));

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), Some(100));
assert_eq!(A::try_get(30), Ok(100));

// Set existing.
A::set(30, Some(101));

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), Some(101));
assert_eq!(A::try_get(30), Ok(101));

// Unset existing.
A::set(30, None);

assert_eq!(A::contains_key(30), false);
assert_eq!(A::get(30), None);
assert_eq!(A::try_get(30), Err(()));

// Unset non-existing.
A::set(31, None);

assert_eq!(A::contains_key(31), false);
assert_eq!(A::get(31), None);
assert_eq!(A::try_get(31), Err(()));

B::insert(2, 10);
assert_eq!(A::migrate_key::<Blake2_256, _>(2), Some(10));
assert_eq!(A::contains_key(2), true);
Expand Down

0 comments on commit 66926f0

Please sign in to comment.