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(
|
||||
appCodec,
|
||||
keys[machinemoduletypes.StoreKey],
|
||||
keys[machinemoduletypes.IndexKey],
|
||||
keys[machinemoduletypes.MemStoreKey],
|
||||
app.GetSubspace(machinemoduletypes.ModuleName),
|
||||
)
|
||||
|
||||
@ -3,6 +3,9 @@ package keeper
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"planetmint-go/x/machine/keeper"
|
||||
"planetmint-go/x/machine/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/cosmos-sdk/store"
|
||||
@ -13,17 +16,17 @@ import (
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
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) {
|
||||
storeKey := sdk.NewKVStoreKey(types.StoreKey)
|
||||
indexStoreKey := sdk.NewKVStoreKey(types.IndexKey)
|
||||
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
|
||||
|
||||
db := tmdb.NewMemDB()
|
||||
stateStore := store.NewCommitMultiStore(db)
|
||||
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
|
||||
stateStore.MountStoreWithDB(indexStoreKey, storetypes.StoreTypeIAVL, db)
|
||||
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
|
||||
require.NoError(t, stateStore.LoadLatestVersion())
|
||||
|
||||
@ -39,6 +42,7 @@ func MachineKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
||||
k := keeper.NewKeeper(
|
||||
cdc,
|
||||
storeKey,
|
||||
indexStoreKey,
|
||||
memStoreKey,
|
||||
paramsSubspace,
|
||||
)
|
||||
|
||||
@ -14,16 +14,18 @@ import (
|
||||
|
||||
type (
|
||||
Keeper struct {
|
||||
cdc codec.BinaryCodec
|
||||
storeKey storetypes.StoreKey
|
||||
memKey storetypes.StoreKey
|
||||
paramstore paramtypes.Subspace
|
||||
cdc codec.BinaryCodec
|
||||
storeKey storetypes.StoreKey
|
||||
indexStoreKey storetypes.StoreKey
|
||||
memKey storetypes.StoreKey
|
||||
paramstore paramtypes.Subspace
|
||||
}
|
||||
)
|
||||
|
||||
func NewKeeper(
|
||||
cdc codec.BinaryCodec,
|
||||
storeKey,
|
||||
indexStoreKey,
|
||||
memKey storetypes.StoreKey,
|
||||
ps paramtypes.Subspace,
|
||||
|
||||
@ -34,10 +36,11 @@ func NewKeeper(
|
||||
}
|
||||
|
||||
return &Keeper{
|
||||
cdc: cdc,
|
||||
storeKey: storeKey,
|
||||
memKey: memKey,
|
||||
paramstore: ps,
|
||||
cdc: cdc,
|
||||
storeKey: storeKey,
|
||||
indexStoreKey: indexStoreKey,
|
||||
memKey: memKey,
|
||||
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"
|
||||
|
||||
MachineKey = "Machine/value/"
|
||||
|
||||
IndexKey = "Machine/index/"
|
||||
)
|
||||
|
||||
func KeyPrefix(p string) []byte {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user