cosmos-sdk/store/types/iterator_test.go

120 lines
2.5 KiB
Go
Raw Normal View History

package types_test
import (
"testing"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/store/iavl"
"github.com/cosmos/cosmos-sdk/store/types"
)
func newMemTestKVStore(t *testing.T) types.KVStore {
db := dbm.NewMemDB()
feat: add configurable iavl cache size (#10561) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #1714 Bump default cache size to 50mb from 10kb. Allow node operators to set cache size. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-11-20 01:32:30 -08:00
store, err := iavl.LoadStore(db, types.CommitID{}, false, iavl.DefaultIAVLCacheSize)
require.NoError(t, err)
return store
}
func TestPaginatedIterator(t *testing.T) {
kvs := newMemTestKVStore(t)
total := 10
lth := total - 1
asc := make([][]byte, total)
desc := make([][]byte, total)
// store returns values in lexicographic order (or reverse lex order)
for i := 0; i < total; i++ {
key := []byte{byte(i)}
kvs.Set(key, key)
asc[i] = key
desc[lth-i] = key
}
type testCase struct {
desc string
page, limit uint
result [][]byte
reverse bool
}
for _, tc := range []testCase{
{
desc: "FirstChunk",
page: 1,
limit: 4,
result: asc[:4],
},
{
desc: "SecondChunk",
page: 2,
limit: 4,
result: asc[4:8],
},
{
desc: "ThirdChunkHalf",
page: 3,
limit: 4,
result: asc[8:],
},
{
desc: "OverLimit",
page: 10,
limit: 10,
result: [][]byte{},
},
{
desc: "ZeroLimit",
page: 1,
result: [][]byte{},
},
{
desc: "ReverseFirstChunk",
page: 1,
limit: 6,
result: desc[:6],
reverse: true,
},
{
desc: "ReverseSecondChunk",
page: 2,
limit: 6,
result: desc[6:],
reverse: true,
},
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
var iter types.Iterator
if tc.reverse {
iter = types.KVStoreReversePrefixIteratorPaginated(kvs, nil, tc.page, tc.limit)
} else {
iter = types.KVStorePrefixIteratorPaginated(kvs, nil, tc.page, tc.limit)
}
defer iter.Close()
result := [][]byte{}
for ; iter.Valid(); iter.Next() {
result = append(result, iter.Key())
}
require.Equal(t, tc.result, result)
require.False(t, iter.Valid())
})
}
}
func TestPaginatedIteratorPanicIfInvalid(t *testing.T) {
kvs := newMemTestKVStore(t)
iter := types.KVStorePrefixIteratorPaginated(kvs, nil, 1, 1)
defer iter.Close()
require.False(t, iter.Valid())
require.Panics(t, func() { iter.Next() }) // "iterator is empty"
kvs.Set([]byte{1}, []byte{})
iter = types.KVStorePrefixIteratorPaginated(kvs, nil, 1, 0)
defer iter.Close()
require.False(t, iter.Valid())
require.Panics(t, func() { iter.Next() }) // "not empty but limit is zero"
}