Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Jan 21, 2025
1 parent 558cdc6 commit 911f6d0
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 2 deletions.
298 changes: 296 additions & 2 deletions packages/cw-storey/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,22 @@ mod tests {
use super::*;

#[test]
fn test_bounds() {
fn set_and_remove() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

storage.remove(b"key2");

let keys: Vec<Vec<u8>> = storage.keys(Bound::Unbounded, Bound::Unbounded).collect();
assert_eq!(keys, vec![b"key1".to_vec(), b"key3".to_vec()]);
}

#[test]
fn keys_bounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

Expand All @@ -194,7 +209,7 @@ mod tests {
}

#[test]
fn test_unbounded() {
fn keys_unbounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

Expand All @@ -213,4 +228,283 @@ mod tests {
.collect();
assert_eq!(keys, vec![b"key1".to_vec(), b"key2".to_vec()]);
}

#[test]
fn values_bounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let values: Vec<Vec<u8>> = storage
.values(Bound::Included(b"key1"), Bound::Excluded(b"key3"))
.collect();
assert_eq!(values, vec![b"value1".to_vec(), b"value2".to_vec()]);

let values: Vec<Vec<u8>> = storage
.values(Bound::Excluded(b"key1"), Bound::Included(b"key3"))
.collect();
assert_eq!(values, vec![b"value2".to_vec(), b"value3".to_vec()]);
}

#[test]
fn values_unbounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let values: Vec<Vec<u8>> = storage.values(Bound::Unbounded, Bound::Unbounded).collect();
assert_eq!(
values,
vec![b"value1".to_vec(), b"value2".to_vec(), b"value3".to_vec()]
);

let values: Vec<Vec<u8>> = storage
.values(Bound::Unbounded, Bound::Excluded(b"key3"))
.collect();
assert_eq!(values, vec![b"value1".to_vec(), b"value2".to_vec()]);
}

#[test]
fn pairs_bounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let pairs: Vec<(Vec<u8>, Vec<u8>)> = storage
.pairs(Bound::Included(b"key1"), Bound::Excluded(b"key3"))
.collect();
assert_eq!(
pairs,
vec![
(b"key1".to_vec(), b"value1".to_vec()),
(b"key2".to_vec(), b"value2".to_vec())
]
);

let pairs: Vec<(Vec<u8>, Vec<u8>)> = storage
.pairs(Bound::Excluded(b"key1"), Bound::Included(b"key3"))
.collect();
assert_eq!(
pairs,
vec![
(b"key2".to_vec(), b"value2".to_vec()),
(b"key3".to_vec(), b"value3".to_vec())
]
);
}

#[test]
fn pairs_unbounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let pairs: Vec<(Vec<u8>, Vec<u8>)> =
storage.pairs(Bound::Unbounded, Bound::Unbounded).collect();
assert_eq!(
pairs,
vec![
(b"key1".to_vec(), b"value1".to_vec()),
(b"key2".to_vec(), b"value2".to_vec()),
(b"key3".to_vec(), b"value3".to_vec())
]
);

let pairs: Vec<(Vec<u8>, Vec<u8>)> = storage
.pairs(Bound::Unbounded, Bound::Excluded(b"key3"))
.collect();
assert_eq!(
pairs,
vec![
(b"key1".to_vec(), b"value1".to_vec()),
(b"key2".to_vec(), b"value2".to_vec())
]
);
}

#[test]
fn rev_keys_bounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let keys: Vec<Vec<u8>> = storage
.rev_keys(Bound::Included(b"key1"), Bound::Excluded(b"key3"))
.collect();
assert_eq!(keys, vec![b"key2".to_vec(), b"key1".to_vec()]);

let keys: Vec<Vec<u8>> = storage
.rev_keys(Bound::Excluded(b"key1"), Bound::Included(b"key3"))
.collect();
assert_eq!(keys, vec![b"key3".to_vec(), b"key2".to_vec()]);
}

#[test]
fn rev_keys_unbounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let keys: Vec<Vec<u8>> = storage
.rev_keys(Bound::Unbounded, Bound::Unbounded)
.collect();
assert_eq!(
keys,
vec![b"key3".to_vec(), b"key2".to_vec(), b"key1".to_vec()]
);

let keys: Vec<Vec<u8>> = storage
.rev_keys(Bound::Unbounded, Bound::Excluded(b"key3"))
.collect();
assert_eq!(keys, vec![b"key2".to_vec(), b"key1".to_vec()]);
}

#[test]
fn rev_values_bounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let values: Vec<Vec<u8>> = storage
.rev_values(Bound::Included(b"key1"), Bound::Excluded(b"key3"))
.collect();
assert_eq!(values, vec![b"value2".to_vec(), b"value1".to_vec()]);

let values: Vec<Vec<u8>> = storage
.rev_values(Bound::Excluded(b"key1"), Bound::Included(b"key3"))
.collect();
assert_eq!(values, vec![b"value3".to_vec(), b"value2".to_vec()]);
}

#[test]
fn rev_values_unbounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let values: Vec<Vec<u8>> = storage
.rev_values(Bound::Unbounded, Bound::Unbounded)
.collect();
assert_eq!(
values,
vec![b"value3".to_vec(), b"value2".to_vec(), b"value1".to_vec()]
);

let values: Vec<Vec<u8>> = storage
.rev_values(Bound::Unbounded, Bound::Excluded(b"key3"))
.collect();
assert_eq!(values, vec![b"value2".to_vec(), b"value1".to_vec()]);
}

#[test]
fn rev_pairs_bounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let pairs: Vec<(Vec<u8>, Vec<u8>)> = storage
.rev_pairs(Bound::Included(b"key1"), Bound::Excluded(b"key3"))
.collect();
assert_eq!(
pairs,
vec![
(b"key2".to_vec(), b"value2".to_vec()),
(b"key1".to_vec(), b"value1".to_vec())
]
);

let pairs: Vec<(Vec<u8>, Vec<u8>)> = storage
.rev_pairs(Bound::Excluded(b"key1"), Bound::Included(b"key3"))
.collect();
assert_eq!(
pairs,
vec![
(b"key3".to_vec(), b"value3".to_vec()),
(b"key2".to_vec(), b"value2".to_vec())
]
);
}

#[test]
fn rev_pairs_unbounded() {
let cw_storage = cosmwasm_std::MemoryStorage::new();
let mut storage = CwStorage(cw_storage);

storage.set(b"key1", b"value1");
storage.set(b"key2", b"value2");
storage.set(b"key3", b"value3");

let pairs: Vec<(Vec<u8>, Vec<u8>)> = storage
.rev_pairs(Bound::Unbounded, Bound::Unbounded)
.collect();
assert_eq!(
pairs,
vec![
(b"key3".to_vec(), b"value3".to_vec()),
(b"key2".to_vec(), b"value2".to_vec()),
(b"key1".to_vec(), b"value1".to_vec())
]
);

let pairs: Vec<(Vec<u8>, Vec<u8>)> = storage
.rev_pairs(Bound::Unbounded, Bound::Excluded(b"key3"))
.collect();
assert_eq!(
pairs,
vec![
(b"key2".to_vec(), b"value2".to_vec()),
(b"key1".to_vec(), b"value1".to_vec())
]
);
}

#[test]
fn into_storage() {
use cosmwasm_std::Storage as _;

let mut storage = cosmwasm_std::MemoryStorage::new();
storage.set(b"key", b"value");

let storage_ref = (&storage,).into_storage();
assert_eq!(storage_ref.get(b"key"), Some(b"value".to_vec()));
}

#[test]
fn into_storage_mut() {
use cosmwasm_std::Storage as _;

let mut storage = cosmwasm_std::MemoryStorage::new();
let storage_ref = (&mut storage,).into_storage();

storage_ref.set(b"key", b"value");

assert_eq!(storage.get(b"key"), Some(b"value".to_vec()));
}
}
8 changes: 8 additions & 0 deletions packages/storey-storage/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub trait Storage {
}
}

/// A trait for converting a type into one that implements [`Storage`].
///
/// This trait is meant to be implemented for a tuple of the intended type, as in
/// `impl IntoStorage<T> for (T,)`. This is to allow blanket implementations on foreign
/// types without stumbling into [E0210](https://stackoverflow.com/questions/63119000/why-am-i-required-to-cover-t-in-impl-foreigntraitlocaltype-for-t-e0210).
///
/// Implementing this trait for foreign types allows to use those foreign types directly
/// with functions like [`Item::access`](crate::Item::access).
pub trait IntoStorage<O>: Sized {
fn into_storage(self) -> O;
}
Expand Down

0 comments on commit 911f6d0

Please sign in to comment.