Skip to content

Commit

Permalink
Add may_load_at_height unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Sep 14, 2021
1 parent 6c601e4 commit 172c2c2
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions packages/storage-plus/src/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ mod tests {
use cosmwasm_std::testing::MockStorage;

type TestSnapshot = Snapshot<'static, &'static str, u64>;

const NEVER: TestSnapshot = Snapshot::new("never__check", "never__change", Strategy::Never);
const EVERY: TestSnapshot =
Snapshot::new("every__check", "every__change", Strategy::EveryBlock);
Expand Down Expand Up @@ -283,4 +284,106 @@ mod tests {
assert_eq!(EVERY.has_changelog(&mut storage, DUMMY_KEY, 3), Ok(false));
assert_eq!(SELECT.has_changelog(&mut storage, DUMMY_KEY, 3), Ok(false));
}

#[test]
fn may_load_at_height() {
let mut storage = MockStorage::new();

assert_eq!(
NEVER.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Err(StdError::not_found("checkpoint"))
);
assert_eq!(
EVERY.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Ok(None)
);
assert_eq!(
SELECT.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Err(StdError::not_found("checkpoint"))
);

// Add a checkpoint at 3
NEVER.add_checkpoint(&mut storage, 3).unwrap();
EVERY.add_checkpoint(&mut storage, 3).unwrap();
SELECT.add_checkpoint(&mut storage, 3).unwrap();

assert_eq!(
NEVER.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Err(StdError::not_found("checkpoint"))
);
assert_eq!(
EVERY.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Ok(None)
);
assert_eq!(
SELECT.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Ok(None)
);

// Write a changelog at 3
NEVER
.write_changelog(&mut storage, DUMMY_KEY, 3, Some(100))
.unwrap();
EVERY
.write_changelog(&mut storage, DUMMY_KEY, 3, Some(100))
.unwrap();
SELECT
.write_changelog(&mut storage, DUMMY_KEY, 3, Some(100))
.unwrap();

assert_eq!(
NEVER.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Err(StdError::not_found("checkpoint"))
);
assert_eq!(
EVERY.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Ok(Some(Some(100)))
);
assert_eq!(
SELECT.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Ok(Some(Some(100)))
);

// Write a changelog at 4, removing the value
NEVER
.write_changelog(&mut storage, DUMMY_KEY, 4, None)
.unwrap();
EVERY
.write_changelog(&mut storage, DUMMY_KEY, 4, None)
.unwrap();
SELECT
.write_changelog(&mut storage, DUMMY_KEY, 4, None)
.unwrap();
// And add a checkpoint at 4
NEVER.add_checkpoint(&mut storage, 4).unwrap();
EVERY.add_checkpoint(&mut storage, 4).unwrap();
SELECT.add_checkpoint(&mut storage, 4).unwrap();

assert_eq!(
NEVER.may_load_at_height(&mut storage, DUMMY_KEY, 4),
Err(StdError::not_found("checkpoint"))
);
assert_eq!(
EVERY.may_load_at_height(&mut storage, DUMMY_KEY, 4),
Ok(Some(None))
);
assert_eq!(
SELECT.may_load_at_height(&mut storage, DUMMY_KEY,4),
Ok(Some(None))
);

// Confirm old value at 3
assert_eq!(
NEVER.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Err(StdError::not_found("checkpoint"))
);
assert_eq!(
EVERY.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Ok(Some(Some(100)))
);
assert_eq!(
SELECT.may_load_at_height(&mut storage, DUMMY_KEY, 3),
Ok(Some(Some(100)))
);
}
}

0 comments on commit 172c2c2

Please sign in to comment.