Skip to content

Commit

Permalink
Add simple base sub_/prefix_/range_de tests for completeness
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Nov 22, 2021
1 parent a2a9fd7 commit ba3099f
Showing 1 changed file with 183 additions and 0 deletions.
183 changes: 183 additions & 0 deletions packages/storage-plus/src/indexed_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,187 @@ mod test {
assert_eq!(datas[0], marias[0].1);
assert_eq!(datas[1], marias[1].1);
}

#[test]
#[cfg(feature = "iterator")]
fn range_de_simple_string_key() {
let mut store = MockStorage::new();
let map = build_snapshot_map();

// save data
let (pks, datas) = save_data(&mut store, &map);

// let's try to iterate!
let all: StdResult<Vec<_>> = map.range_de(&store, None, None, Order::Ascending).collect();
let all = all.unwrap();
assert_eq!(
all,
pks.clone()
.into_iter()
.map(str::to_string)
.zip(datas.clone().into_iter())
.collect::<Vec<_>>()
);

// let's try to iterate over a range
let all: StdResult<Vec<_>> = map
.range_de(
&store,
Some(Bound::Inclusive(b"3".to_vec())),
None,
Order::Ascending,
)
.collect();
let all = all.unwrap();
assert_eq!(
all,
pks.into_iter()
.map(str::to_string)
.zip(datas.into_iter())
.rev()
.take(2)
.rev()
.collect::<Vec<_>>()
);
}

#[test]
#[cfg(feature = "iterator")]
fn prefix_de_simple_string_key() {
let mut store = MockStorage::new();
let map = build_snapshot_map();

// save data
let (pks, datas) = save_data(&mut store, &map);

// Let's prefix and iterate.
// This is similar to calling range() directly, but added here for completeness / prefix_de
// type checks
let all: StdResult<Vec<_>> = map
.prefix_de(())
.range_de(&store, None, None, Order::Ascending)
.collect();
let all = all.unwrap();
assert_eq!(
all,
pks.clone()
.into_iter()
.map(str::to_string)
.zip(datas.into_iter())
.collect::<Vec<_>>()
);
}

#[test]
#[cfg(feature = "iterator")]
fn sub_prefix_de_simple_string_key() {
let mut store = MockStorage::new();
let map = build_snapshot_map();

// save data
let (pks, datas) = save_data(&mut store, &map);

// Let's prefix and iterate.
// This is similar to calling range() directly, but added here for completeness / sub_prefix_de
// type checks
let all: StdResult<Vec<_>> = map
.sub_prefix_de(())
.range_de(&store, None, None, Order::Ascending)
.collect();
let all = all.unwrap();
assert_eq!(
all,
pks.clone()
.into_iter()
.map(str::to_string)
.zip(datas.into_iter())
.collect::<Vec<_>>()
);
}

#[test]
#[cfg(feature = "iterator")]
fn prefix_range_de_simple_key() {
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",
),
};
let map =
IndexedSnapshotMap::new("data", "checks", "changes", Strategy::EveryBlock, indexes);

// save data
let data1 = Data {
name: "Maria".to_string(),
last_name: "".to_string(),
age: 42,
};
let pk1: (&str, &str) = ("1", "5627");
map.save(&mut store, pk1, &data1, 1).unwrap();

let data2 = Data {
name: "Juan".to_string(),
last_name: "Perez".to_string(),
age: 13,
};
let pk2: (&str, &str) = ("2", "5628");
map.save(&mut store, pk2, &data2, 1).unwrap();

let data3 = Data {
name: "Maria".to_string(),
last_name: "Young".to_string(),
age: 24,
};
let pk3: (&str, &str) = ("2", "5629");
map.save(&mut store, pk3, &data3, 1).unwrap();

let data4 = Data {
name: "Maria Luisa".to_string(),
last_name: "Bemberg".to_string(),
age: 43,
};
let pk4: (&str, &str) = ("3", "5630");
map.save(&mut store, pk4, &data4, 1).unwrap();

// let's try to iterate!
let result: StdResult<Vec<_>> = map
.prefix_range_de(
&store,
Some(PrefixBound::inclusive("2")),
None,
Order::Ascending,
)
.collect();
let result = result.unwrap();
assert_eq!(
result,
[
(("2".to_string(), "5628".to_string()), data2.clone()),
(("2".to_string(), "5629".to_string()), data3.clone()),
(("3".to_string(), "5630".to_string()), data4)
]
);

// let's try to iterate over a range
let result: StdResult<Vec<_>> = map
.prefix_range_de(
&store,
Some(PrefixBound::inclusive("2")),
Some(PrefixBound::exclusive("3")),
Order::Ascending,
)
.collect();
let result = result.unwrap();
assert_eq!(
result,
[
(("2".to_string(), "5628".to_string()), data2),
(("2".to_string(), "5629".to_string()), data3),
]
);
}
}

0 comments on commit ba3099f

Please sign in to comment.