Skip to content

Commit

Permalink
Merge pull request #27 from CosmWasm/more-tests
Browse files Browse the repository at this point in the history
Fill in missing tests
  • Loading branch information
uint authored Apr 16, 2024
2 parents b5f56d2 + 811ecb3 commit b847cf5
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 86 deletions.
39 changes: 38 additions & 1 deletion crates/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mod tests {
use mocks::encoding::TestEncoding;

#[test]
fn column() {
fn basic() {
let mut storage = TestStorage::new();

let column = Column::<u64, TestEncoding>::new(&[0]);
Expand All @@ -238,4 +238,41 @@ mod tests {
assert_eq!(access.get(1).unwrap(), Some(9001));
assert_eq!(access.len().unwrap(), 1);
}

#[test]
fn iteration() {
let mut storage = TestStorage::new();

let column = Column::<u64, TestEncoding>::new(&[0]);
let mut access = column.access(&mut storage);

access.push(&1337).unwrap();
access.push(&42).unwrap();
access.push(&9001).unwrap();
access.remove(1).unwrap();

assert_eq!(
access
.pairs(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(0, 1337), (2, 9001)]
);

assert_eq!(
access
.keys(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![0, 2]
);

assert_eq!(
access
.values(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1337, 9001]
);
}
}
2 changes: 1 addition & 1 deletion crates/storey/src/containers/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod tests {
use mocks::encoding::TestEncoding;

#[test]
fn item() {
fn basic() {
let mut storage = TestStorage::new();

let item0 = Item::<u64, TestEncoding>::new(&[0]);
Expand Down
73 changes: 48 additions & 25 deletions crates/storey/src/containers/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,36 +179,59 @@ mod tests {
}

#[test]
fn map_of_map() {
fn pairs() {
let mut storage = TestStorage::new();

let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(&[0]);
let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

map.access(&mut storage)
.entry_mut("foo")
.entry_mut("bar")
.set(&1337)
.unwrap();
access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let items = access
.pairs(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(
map.access(&storage)
.entry("foo")
.entry("bar")
.get()
.unwrap(),
Some(1337)
);
assert_eq!(
storage.get(&[0, 3, 102, 111, 111, 3, 98, 97, 114]),
Some(1337u64.to_le_bytes().to_vec())
);
assert_eq!(
map.access(&storage)
.entry("foo")
.entry("baz")
.get()
.unwrap(),
None
items,
vec![
(("bar".to_string(), ()), 42),
(("foo".to_string(), ()), 1337)
]
);
}

#[test]
fn keys() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let keys = access
.keys(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(keys, vec![("bar".to_string(), ()), ("foo".to_string(), ())])
}

#[test]
fn values() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let values = access
.values(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(values, vec![42, 1337])
}
}
60 changes: 60 additions & 0 deletions crates/storey/src/storage/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,64 @@ mod tests {
(Some(b"foo".to_vec()), Some(b"fop".to_vec()))
);
}

#[test]
fn pairs() {
let mut storage = TestStorage::new();
let mut branch = StorageBranch::new(&mut storage, b"foo".to_vec());

branch.set(b"bar", b"baz");
branch.set(b"qux", b"quux");

let mut iter = branch.pairs(None, None);
assert_eq!(iter.next(), Some((b"bar".to_vec(), b"baz".to_vec())));
assert_eq!(iter.next(), Some((b"qux".to_vec(), b"quux".to_vec())));
assert_eq!(iter.next(), None);
}

#[test]
fn keys() {
let mut storage = TestStorage::new();
let mut branch = StorageBranch::new(&mut storage, b"foo".to_vec());

branch.set(b"bar", b"baz");
branch.set(b"qux", b"quux");

let mut iter = branch.keys(None, None);
assert_eq!(iter.next(), Some(b"bar".to_vec()));
assert_eq!(iter.next(), Some(b"qux".to_vec()));
assert_eq!(iter.next(), None);
}

#[test]
fn values() {
let mut storage = TestStorage::new();
let mut branch = StorageBranch::new(&mut storage, b"foo".to_vec());

branch.set(b"bar", b"baz");
branch.set(b"qux", b"quux");

let mut iter = branch.values(None, None);
assert_eq!(iter.next(), Some(b"baz".to_vec()));
assert_eq!(iter.next(), Some(b"quux".to_vec()));
assert_eq!(iter.next(), None);
}

#[test]
fn meta() {
let mut storage = TestStorage::new();
let mut branch = StorageBranch::new(&mut storage, b"foo".to_vec());

branch.set_meta(b"bar", b"baz");
branch.set_meta(b"qux", b"quux");

assert_eq!(storage.get_meta(b"bar"), None);
assert_eq!(storage.get_meta(b"qux"), None);

assert_eq!(storage.get_meta(b"foobar"), Some(b"baz".to_vec()));
assert_eq!(storage.get_meta(b"fooqux"), Some(b"quux".to_vec()));

assert_eq!(storage.get(b"foobar"), None);
assert_eq!(storage.get(b"fooqux"), None);
}
}
37 changes: 36 additions & 1 deletion crates/storey/tests/composition.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
use storey::containers::{Column, IterableAccessor as _, Map};
use storey::containers::{Column, Item, IterableAccessor as _, Map};

use mocks::backend::TestStorage;
use mocks::encoding::TestEncoding;
use storey_storage::Storage as _;

#[test]
fn map_of_map() {
let mut storage = TestStorage::new();

let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(&[0]);

map.access(&mut storage)
.entry_mut("foo")
.entry_mut("bar")
.set(&1337)
.unwrap();

assert_eq!(
map.access(&storage)
.entry("foo")
.entry("bar")
.get()
.unwrap(),
Some(1337)
);
assert_eq!(
storage.get(&[0, 3, 102, 111, 111, 3, 98, 97, 114]),
Some(1337u64.to_le_bytes().to_vec())
);
assert_eq!(
map.access(&storage)
.entry("foo")
.entry("baz")
.get()
.unwrap(),
None
);
}

#[test]
fn map_of_column() {
Expand Down
59 changes: 1 addition & 58 deletions crates/storey/tests/iteration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,7 @@ use mocks::backend::TestStorage;
use mocks::encoding::TestEncoding;

#[test]
fn pairs_iteration() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let items = access
.pairs(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(
items,
vec![
(("bar".to_string(), ()), 42),
(("foo".to_string(), ()), 1337)
]
);
}

#[test]
fn keys_iteration() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let keys = access
.keys(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(keys, vec![("bar".to_string(), ()), ("foo".to_string(), ())])
}

#[test]
fn values_iteration() {
let mut storage = TestStorage::new();

let map = Map::<String, Item<u64, TestEncoding>>::new(&[0]);
let mut access = map.access(&mut storage);

access.entry_mut("foo").set(&1337).unwrap();
access.entry_mut("bar").set(&42).unwrap();

let values = access
.values(None, None)
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(values, vec![42, 1337])
}

#[test]
fn complex_type_iteration() {
fn map_of_map_iteration() {
let mut storage = TestStorage::new();

let map = Map::<String, Map<String, Item<u64, TestEncoding>>>::new(&[0]);
Expand Down

0 comments on commit b847cf5

Please sign in to comment.