mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-24 06:25:47 +00:00
added index store key and basic test case
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
70b6fc4b7c
commit
bc97f3c46e
@ -515,6 +515,7 @@ func New(
|
|||||||
app.MachineKeeper = *machinemodulekeeper.NewKeeper(
|
app.MachineKeeper = *machinemodulekeeper.NewKeeper(
|
||||||
appCodec,
|
appCodec,
|
||||||
keys[machinemoduletypes.StoreKey],
|
keys[machinemoduletypes.StoreKey],
|
||||||
|
keys[machinemoduletypes.IndexKey],
|
||||||
keys[machinemoduletypes.MemStoreKey],
|
keys[machinemoduletypes.MemStoreKey],
|
||||||
app.GetSubspace(machinemoduletypes.ModuleName),
|
app.GetSubspace(machinemoduletypes.ModuleName),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -3,6 +3,9 @@ package keeper
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"planetmint-go/x/machine/keeper"
|
||||||
|
"planetmint-go/x/machine/types"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||||
"github.com/cosmos/cosmos-sdk/store"
|
"github.com/cosmos/cosmos-sdk/store"
|
||||||
@ -13,17 +16,17 @@ import (
|
|||||||
"github.com/tendermint/tendermint/libs/log"
|
"github.com/tendermint/tendermint/libs/log"
|
||||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||||
tmdb "github.com/tendermint/tm-db"
|
tmdb "github.com/tendermint/tm-db"
|
||||||
"planetmint-go/x/machine/keeper"
|
|
||||||
"planetmint-go/x/machine/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
||||||
storeKey := sdk.NewKVStoreKey(types.StoreKey)
|
storeKey := sdk.NewKVStoreKey(types.StoreKey)
|
||||||
|
indexStoreKey := sdk.NewKVStoreKey(types.IndexKey)
|
||||||
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
|
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
|
||||||
|
|
||||||
db := tmdb.NewMemDB()
|
db := tmdb.NewMemDB()
|
||||||
stateStore := store.NewCommitMultiStore(db)
|
stateStore := store.NewCommitMultiStore(db)
|
||||||
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
|
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
|
||||||
|
stateStore.MountStoreWithDB(indexStoreKey, storetypes.StoreTypeIAVL, db)
|
||||||
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
|
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
|
||||||
require.NoError(t, stateStore.LoadLatestVersion())
|
require.NoError(t, stateStore.LoadLatestVersion())
|
||||||
|
|
||||||
@ -39,6 +42,7 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
|||||||
k := keeper.NewKeeper(
|
k := keeper.NewKeeper(
|
||||||
cdc,
|
cdc,
|
||||||
storeKey,
|
storeKey,
|
||||||
|
indexStoreKey,
|
||||||
memStoreKey,
|
memStoreKey,
|
||||||
paramsSubspace,
|
paramsSubspace,
|
||||||
)
|
)
|
||||||
|
|||||||
@ -16,6 +16,7 @@ type (
|
|||||||
Keeper struct {
|
Keeper struct {
|
||||||
cdc codec.BinaryCodec
|
cdc codec.BinaryCodec
|
||||||
storeKey storetypes.StoreKey
|
storeKey storetypes.StoreKey
|
||||||
|
indexStoreKey storetypes.StoreKey
|
||||||
memKey storetypes.StoreKey
|
memKey storetypes.StoreKey
|
||||||
paramstore paramtypes.Subspace
|
paramstore paramtypes.Subspace
|
||||||
}
|
}
|
||||||
@ -24,6 +25,7 @@ type (
|
|||||||
func NewKeeper(
|
func NewKeeper(
|
||||||
cdc codec.BinaryCodec,
|
cdc codec.BinaryCodec,
|
||||||
storeKey,
|
storeKey,
|
||||||
|
indexStoreKey,
|
||||||
memKey storetypes.StoreKey,
|
memKey storetypes.StoreKey,
|
||||||
ps paramtypes.Subspace,
|
ps paramtypes.Subspace,
|
||||||
|
|
||||||
@ -36,6 +38,7 @@ func NewKeeper(
|
|||||||
return &Keeper{
|
return &Keeper{
|
||||||
cdc: cdc,
|
cdc: cdc,
|
||||||
storeKey: storeKey,
|
storeKey: storeKey,
|
||||||
|
indexStoreKey: indexStoreKey,
|
||||||
memKey: memKey,
|
memKey: memKey,
|
||||||
paramstore: ps,
|
paramstore: ps,
|
||||||
}
|
}
|
||||||
|
|||||||
33
x/machine/keeper/machine_test.go
Normal file
33
x/machine/keeper/machine_test.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package keeper_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
keepertest "planetmint-go/testutil/keeper"
|
||||||
|
"planetmint-go/x/machine/keeper"
|
||||||
|
"planetmint-go/x/machine/types"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createNMachine(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Machine {
|
||||||
|
items := make([]types.Machine, n)
|
||||||
|
for i := range items {
|
||||||
|
// fill out machine
|
||||||
|
items[i].IssuerPlanetmint = "asd"
|
||||||
|
items[i].IssuerLiquid = "dsa"
|
||||||
|
keeper.StoreMachine(ctx, items[i])
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMachine(t *testing.T) {
|
||||||
|
keeper, ctx := keepertest.MachineKeeper(t)
|
||||||
|
items := createNMachine(keeper, ctx, 10)
|
||||||
|
for _, item := range items {
|
||||||
|
machineById, found := keeper.GetMachine(ctx, item.IssuerPlanetmint)
|
||||||
|
assert.True(t, found)
|
||||||
|
assert.Equal(t, item, machineById)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,6 +14,8 @@ const (
|
|||||||
MemStoreKey = "mem_machine"
|
MemStoreKey = "mem_machine"
|
||||||
|
|
||||||
MachineKey = "Machine/value/"
|
MachineKey = "Machine/value/"
|
||||||
|
|
||||||
|
IndexKey = "Machine/index/"
|
||||||
)
|
)
|
||||||
|
|
||||||
func KeyPrefix(p string) []byte {
|
func KeyPrefix(p string) []byte {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user