Skip to content

Commit

Permalink
Add extra test of Lockable
Browse files Browse the repository at this point in the history
Locked values reporting example
  • Loading branch information
maurolacy committed Jun 20, 2023
1 parent 470f726 commit 5d454a7
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/sync/src/locks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ mod tests {
mod tests_plus {
use super::*;

use crate::locks::tests_plus::MaybeCollateral::{Collateral, Locked};
use cosmwasm_std::{testing::MockStorage, StdError, Uint128};
use cw_storage_plus::{Item, Map};

Expand Down Expand Up @@ -282,6 +283,12 @@ mod tests_plus {
pub total_slashable: Uint128,
}

#[cw_serde]
enum MaybeCollateral {
Collateral(Uint128),
Locked { user: String },
}

#[test]
fn modify_item_with_locks() {
let mut store = MockStorage::new();
Expand Down Expand Up @@ -539,6 +546,21 @@ mod tests_plus {
.unwrap();
assert_eq!(collaterals.len(), 2);

// Finally, we can also report the locked users
let maybe_collaterals: Vec<_> = USERS
.range(&store, None, None, cosmwasm_std::Order::Ascending)
.map(|item| {
item.map(|(user, user_lock)| {
user_lock
.read()
.map(|user| Ok(Collateral(user.collateral)))
.unwrap_or(Ok(Locked { user })) // Report locked collateral
})? // Surface errors
})
.collect::<Result<_, TestsError>>()
.unwrap();
assert_eq!(maybe_collaterals.len(), 3);

// We can get count (kind of edge case bug, but I don't think we can change this or it matters)
let num_users = USERS
.range(&store, None, None, cosmwasm_std::Order::Ascending)
Expand Down

0 comments on commit 5d454a7

Please sign in to comment.