Test iavlStore query
This commit is contained in:
parent
31f00742e5
commit
44c39043f1
|
@ -5,10 +5,12 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
abci "github.com/tendermint/abci/types"
|
||||||
|
"github.com/tendermint/iavl"
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
dbm "github.com/tendermint/tmlibs/db"
|
dbm "github.com/tendermint/tmlibs/db"
|
||||||
|
|
||||||
"github.com/tendermint/iavl"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -79,3 +81,63 @@ func TestIAVLIterator(t *testing.T) {
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIAVLStoreQuery(t *testing.T) {
|
||||||
|
db := dbm.NewMemDB()
|
||||||
|
tree := iavl.NewVersionedTree(db, cacheSize)
|
||||||
|
iavlStore := newIAVLStore(tree, numHistory)
|
||||||
|
|
||||||
|
k, v := []byte("wind"), []byte("blows")
|
||||||
|
k2, v2 := []byte("water"), []byte("flows")
|
||||||
|
v3 := []byte("is cold")
|
||||||
|
// k3, v3 := []byte("earth"), []byte("soes")
|
||||||
|
// k4, v4 := []byte("fire"), []byte("woes")
|
||||||
|
|
||||||
|
cid := iavlStore.Commit()
|
||||||
|
ver := cid.Version
|
||||||
|
query := abci.RequestQuery{Path: "/key", Data: k, Height: ver}
|
||||||
|
|
||||||
|
// set data without commit, doesn't show up
|
||||||
|
iavlStore.Set(k, v)
|
||||||
|
qres := iavlStore.Query(query)
|
||||||
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
||||||
|
assert.Nil(t, qres.Value)
|
||||||
|
|
||||||
|
// commit it, but still don't see on old version
|
||||||
|
cid = iavlStore.Commit()
|
||||||
|
qres = iavlStore.Query(query)
|
||||||
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
||||||
|
assert.Nil(t, qres.Value)
|
||||||
|
|
||||||
|
// but yes on the new version
|
||||||
|
query.Height = cid.Version
|
||||||
|
qres = iavlStore.Query(query)
|
||||||
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
||||||
|
assert.Equal(t, v, qres.Value)
|
||||||
|
|
||||||
|
// modify
|
||||||
|
iavlStore.Set(k2, v2)
|
||||||
|
iavlStore.Set(k, v3)
|
||||||
|
cid = iavlStore.Commit()
|
||||||
|
|
||||||
|
// query will return old values, as height is fixed
|
||||||
|
qres = iavlStore.Query(query)
|
||||||
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
||||||
|
assert.Equal(t, v, qres.Value)
|
||||||
|
|
||||||
|
// update to latest in the query and we are happy
|
||||||
|
query.Height = cid.Version
|
||||||
|
qres = iavlStore.Query(query)
|
||||||
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
||||||
|
assert.Equal(t, v3, qres.Value)
|
||||||
|
query2 := abci.RequestQuery{Path: "/key", Data: k2, Height: cid.Version}
|
||||||
|
qres = iavlStore.Query(query2)
|
||||||
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
||||||
|
assert.Equal(t, v2, qres.Value)
|
||||||
|
|
||||||
|
// default (height 0) will show latest -1
|
||||||
|
query0 := abci.RequestQuery{Path: "/store", Data: k}
|
||||||
|
qres = iavlStore.Query(query0)
|
||||||
|
assert.Equal(t, uint32(sdk.CodeOK), qres.Code)
|
||||||
|
assert.Equal(t, v, qres.Value)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue