Skip to content

Commit

Permalink
Adapt multi-index tests to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Dec 6, 2021
1 parent dfbbd4e commit 9a517d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 92 deletions.
77 changes: 21 additions & 56 deletions packages/storage-plus/src/indexed_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ where
mod test {
use super::*;

use crate::indexes::{index_string_tuple, index_triple};
use crate::{MultiIndex, UniqueIndex};
use crate::indexes::index_string_tuple;
use crate::{index_tuple, MultiIndex, UniqueIndex};
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{MemoryStorage, Order};
use serde::{Deserialize, Serialize};
Expand All @@ -295,8 +295,8 @@ mod test {
}

struct DataIndexes<'a> {
// Second arg is for storing pk
pub name: MultiIndex<'a, (String, String), Data, String>,
// Last args are for signaling pk deserialization
pub name: MultiIndex<'a, String, Data, String>,
pub age: UniqueIndex<'a, u32, Data, String>,
pub name_lastname: UniqueIndex<'a, (Vec<u8>, Vec<u8>), Data, String>,
}
Expand All @@ -311,8 +311,8 @@ mod test {

// For composite multi index tests
struct DataCompositeMultiIndex<'a> {
// Third arg needed for storing pk
pub name_age: MultiIndex<'a, (Vec<u8>, u32, Vec<u8>), Data, String>,
// Last arg is for signaling pk deserialization
pub name_age: MultiIndex<'a, (Vec<u8>, u32), Data, String>,
}

// Future Note: this can likely be macro-derived
Expand All @@ -326,11 +326,7 @@ mod test {
// Can we make it easier to define this? (less wordy generic)
fn build_map<'a>() -> IndexedMap<'a, &'a str, Data, DataIndexes<'a>> {
let indexes = DataIndexes {
name: MultiIndex::new(
|d, k| (d.name.clone(), unsafe { String::from_utf8_unchecked(k) }),
"data",
"data__name",
),
name: MultiIndex::new(|d| d.name.clone(), "data", "data__name"),
age: UniqueIndex::new(|d| d.age, "data__age"),
name_lastname: UniqueIndex::new(
|d| index_string_tuple(&d.name, &d.last_name),
Expand Down Expand Up @@ -423,10 +419,7 @@ mod test {
.count();
assert_eq!(2, count);

// TODO: we load by wrong keys - get full storage key!

// load it by secondary index (we must know how to compute this)
// let marias: Vec<_>> = map
// load it by secondary index
let marias: Vec<_> = map
.idx
.name
Expand Down Expand Up @@ -469,8 +462,8 @@ mod test {
// index_key() over MultiIndex works (empty pk)
// In a MultiIndex, an index key is composed by the index and the primary key.
// Primary key may be empty (so that to iterate over all elements that match just the index)
let key = ("Maria".to_string(), "".to_string());
// Use the index_key() helper to build the (raw) index key
let key = "Maria".to_string();
// Use the index_key() helper to build the (raw) index key with an empty pk
let key = map.idx.name.index_key(key);
// Iterate using a bound over the raw key
let count = map
Expand All @@ -484,8 +477,8 @@ mod test {
// index_key() over MultiIndex works (non-empty pk)
// Build key including a non-empty pk
let key = ("Maria".to_string(), "1".to_string());
// Use the index_key() helper to build the (raw) index key
let key = map.idx.name.index_key(key);
// Use the joined_key() helper to build the (raw) index key
let key = key.joined_key();
// Iterate using a (exclusive) bound over the raw key.
// (Useful for pagination / continuation contexts).
let count = map
Expand Down Expand Up @@ -643,11 +636,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map = IndexedMap::new("data", indexes);

Expand Down Expand Up @@ -708,11 +697,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map = IndexedMap::new("data", indexes);

Expand Down Expand Up @@ -1075,11 +1060,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map = IndexedMap::new("data", indexes);

Expand Down Expand Up @@ -1134,11 +1115,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map = IndexedMap::new("data", indexes);

Expand Down Expand Up @@ -1190,11 +1167,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map = IndexedMap::new("data", indexes);

Expand Down Expand Up @@ -1252,11 +1225,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map = IndexedMap::new("data", indexes);

Expand Down Expand Up @@ -1337,11 +1306,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map = IndexedMap::new("data", indexes);

Expand Down Expand Up @@ -1435,7 +1400,7 @@ mod test {
use super::*;

struct Indexes<'a> {
secondary: MultiIndex<'a, (u64, Vec<u8>), u64>,
secondary: MultiIndex<'a, u64, u64>,
}

impl<'a> IndexList<u64> for Indexes<'a> {
Expand All @@ -1450,7 +1415,7 @@ mod test {
fn composite_key_query() {
let indexes = Indexes {
secondary: MultiIndex::new(
|secondary, k| (*secondary, k),
|secondary| *secondary,
"test_map",
"test_map__secondary",
),
Expand Down
47 changes: 11 additions & 36 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ where
mod test {
use super::*;

use crate::indexes::{index_string_tuple, index_triple};
use crate::{Index, MultiIndex, UniqueIndex};
use crate::indexes::index_string_tuple;
use crate::{index_tuple, Index, MultiIndex, UniqueIndex};
use cosmwasm_std::testing::MockStorage;
use cosmwasm_std::{MemoryStorage, Order};
use serde::{Deserialize, Serialize};
Expand All @@ -311,11 +311,9 @@ mod test {
}

struct DataIndexes<'a> {
// Second arg is for storing pk
pub name: MultiIndex<'a, (Vec<u8>, String), Data, String>,
// Last generic type arg is pk deserialization type
// Last args are for signaling pk deserialization
pub name: MultiIndex<'a, Vec<u8>, Data, String>,
pub age: UniqueIndex<'a, u32, Data, String>,
// Last generic type arg is pk deserialization type
pub name_lastname: UniqueIndex<'a, (Vec<u8>, Vec<u8>), Data, String>,
}

Expand All @@ -329,8 +327,8 @@ mod test {

// For composite multi index tests
struct DataCompositeMultiIndex<'a> {
// Third arg needed for storing pk
pub name_age: MultiIndex<'a, (Vec<u8>, u32, Vec<u8>), Data, String>,
// Last arg is for signaling pk deserialization
pub name_age: MultiIndex<'a, (Vec<u8>, u32), Data, String>,
}

// Future Note: this can likely be macro-derived
Expand All @@ -344,15 +342,7 @@ mod test {
// Can we make it easier to define this? (less wordy generic)
fn build_snapshot_map<'a>() -> IndexedSnapshotMap<'a, &'a str, Data, DataIndexes<'a>> {
let indexes = DataIndexes {
name: MultiIndex::new(
|d, k| {
(d.name.as_bytes().to_vec(), unsafe {
String::from_utf8_unchecked(k)
})
},
"data",
"data__name",
),
name: MultiIndex::new(|d| d.name.as_bytes().to_vec(), "data", "data__name"),
age: UniqueIndex::new(|d| d.age, "data__age"),
name_lastname: UniqueIndex::new(
|d| index_string_tuple(&d.name, &d.last_name),
Expand Down Expand Up @@ -445,10 +435,7 @@ mod test {
.count();
assert_eq!(2, count);

// TODO: we load by wrong keys - get full storage key!

// load it by secondary index (we must know how to compute this)
// let marias: Vec<_>> = map
// load it by secondary index
let marias: Vec<_> = map
.idx
.name
Expand Down Expand Up @@ -626,11 +613,7 @@ mod test {
let mut height = 2;

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map =
IndexedSnapshotMap::new("data", "checks", "changes", Strategy::EveryBlock, indexes);
Expand Down Expand Up @@ -696,11 +679,7 @@ mod test {
let mut height = 2;

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map =
IndexedSnapshotMap::new("data", "checks", "changes", Strategy::EveryBlock, indexes);
Expand Down Expand Up @@ -1096,11 +1075,7 @@ mod test {
let mut store = MockStorage::new();

let indexes = DataCompositeMultiIndex {
name_age: MultiIndex::new(
|d, k| index_triple(&d.name, d.age, k),
"data",
"data__name_age",
),
name_age: MultiIndex::new(|d| index_tuple(&d.name, d.age), "data", "data__name_age"),
};
let map =
IndexedSnapshotMap::new("data", "checks", "changes", Strategy::EveryBlock, indexes);
Expand Down

0 comments on commit 9a517d3

Please sign in to comment.