Skip to content

Commit

Permalink
Merge pull request #450 from CosmWasm/inline-iterator_test_suite
Browse files Browse the repository at this point in the history
Inline iterator test suite
  • Loading branch information
webmaster128 authored Jun 25, 2020
2 parents 249366a + 00f59b1 commit cd786ed
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 64 deletions.
57 changes: 25 additions & 32 deletions packages/std/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,32 @@ impl Storage for MemoryStorage {
mod test {
use super::*;

#[test]
fn get_and_set() {
let mut store = MemoryStorage::new();
assert_eq!(store.get(b"foo"), None);
store.set(b"foo", b"bar");
assert_eq!(store.get(b"foo"), Some(b"bar".to_vec()));
assert_eq!(store.get(b"food"), None);
}

#[test]
fn delete() {
let mut store = MemoryStorage::new();
store.set(b"foo", b"bar");
store.set(b"food", b"bank");
store.remove(b"foo");

assert_eq!(store.get(b"foo"), None);
assert_eq!(store.get(b"food"), Some(b"bank".to_vec()));
}

#[test]
#[cfg(feature = "iterator")]
// iterator_test_suite takes a storage, adds data and runs iterator tests
// the storage must previously have exactly one key: "foo" = "bar"
// (this allows us to test StorageTransaction and other wrapped storage better)
fn iterator_test_suite<S: Storage>(store: &mut S) {
fn iterator() {
let mut store = MemoryStorage::new();
store.set(b"foo", b"bar");

// ensure we had previously set "foo" = "bar"
assert_eq!(store.get(b"foo"), Some(b"bar".to_vec()));
assert_eq!(store.range(None, None, Order::Ascending).count(), 1);
Expand Down Expand Up @@ -224,32 +245,4 @@ mod test {
);
}
}

#[test]
fn get_and_set() {
let mut store = MemoryStorage::new();
assert_eq!(store.get(b"foo"), None);
store.set(b"foo", b"bar");
assert_eq!(store.get(b"foo"), Some(b"bar".to_vec()));
assert_eq!(store.get(b"food"), None);
}

#[test]
fn delete() {
let mut store = MemoryStorage::new();
store.set(b"foo", b"bar");
store.set(b"food", b"bank");
store.remove(b"foo");

assert_eq!(store.get(b"foo"), None);
assert_eq!(store.get(b"food"), Some(b"bank".to_vec()));
}

#[test]
#[cfg(feature = "iterator")]
fn iterator() {
let mut store = MemoryStorage::new();
store.set(b"foo", b"bar");
iterator_test_suite(&mut store);
}
}
57 changes: 25 additions & 32 deletions packages/vm/src/testing/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,32 @@ fn clone_item<T: Clone>(item_ref: BTreeMapPairRef<T>) -> KV<T> {
mod test {
use super::*;

#[test]
fn get_and_set() {
let mut store = MockStorage::new();
assert_eq!(None, store.get(b"foo").unwrap().0);
store.set(b"foo", b"bar").unwrap();
assert_eq!(Some(b"bar".to_vec()), store.get(b"foo").unwrap().0);
assert_eq!(None, store.get(b"food").unwrap().0);
}

#[test]
fn delete() {
let mut store = MockStorage::new();
store.set(b"foo", b"bar").unwrap();
store.set(b"food", b"bank").unwrap();
store.remove(b"foo").unwrap();

assert_eq!(None, store.get(b"foo").unwrap().0);
assert_eq!(Some(b"bank".to_vec()), store.get(b"food").unwrap().0);
}

#[test]
#[cfg(feature = "iterator")]
// iterator_test_suite takes a storage, adds data and runs iterator tests
// the storage must previously have exactly one key: "foo" = "bar"
// (this allows us to test StorageTransaction and other wrapped storage better)
fn iterator_test_suite<S: Storage>(store: &mut S) {
fn iterator() {
let mut store = MockStorage::new();
store.set(b"foo", b"bar").expect("error setting value");

// ensure we had previously set "foo" = "bar"
assert_eq!(store.get(b"foo").unwrap().0, Some(b"bar".to_vec()));
assert_eq!(
Expand Down Expand Up @@ -296,32 +317,4 @@ mod test {
);
}
}

#[test]
fn get_and_set() {
let mut store = MockStorage::new();
assert_eq!(None, store.get(b"foo").unwrap().0);
store.set(b"foo", b"bar").unwrap();
assert_eq!(Some(b"bar".to_vec()), store.get(b"foo").unwrap().0);
assert_eq!(None, store.get(b"food").unwrap().0);
}

#[test]
fn delete() {
let mut store = MockStorage::new();
store.set(b"foo", b"bar").unwrap();
store.set(b"food", b"bank").unwrap();
store.remove(b"foo").unwrap();

assert_eq!(None, store.get(b"foo").unwrap().0);
assert_eq!(Some(b"bank".to_vec()), store.get(b"food").unwrap().0);
}

#[test]
#[cfg(feature = "iterator")]
fn iterator() {
let mut store = MockStorage::new();
store.set(b"foo", b"bar").expect("error setting value");
iterator_test_suite(&mut store);
}
}

0 comments on commit cd786ed

Please sign in to comment.