mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-23 22:15:47 +00:00
added store testcases to the der module
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
parent
94710dd5df
commit
8b8331f79b
2
go.mod
2
go.mod
@ -74,6 +74,7 @@ require (
|
||||
github.com/confio/ics23/go v0.9.0 // indirect
|
||||
github.com/cosmos/btcutil v1.0.5 // indirect
|
||||
github.com/cosmos/gogogateway v1.2.0 // indirect
|
||||
github.com/cosmos/gorocksdb v1.2.0 // indirect
|
||||
github.com/cosmos/iavl v0.20.1 // indirect
|
||||
github.com/cosmos/ics23/go v0.10.0 // indirect
|
||||
github.com/cosmos/ledger-cosmos-go v0.12.4 // indirect
|
||||
@ -168,6 +169,7 @@ require (
|
||||
github.com/spf13/viper v1.18.2 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/tendermint/go-amino v0.16.0 // indirect
|
||||
github.com/tendermint/tm-db v0.6.7 // indirect
|
||||
github.com/tidwall/btree v1.6.0 // indirect
|
||||
github.com/ulikunitz/xz v0.5.11 // indirect
|
||||
github.com/zondax/hid v0.9.2 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@ -368,6 +368,8 @@ github.com/cosmos/gogogateway v1.2.0/go.mod h1:iQpLkGWxYcnCdz5iAdLcRBSw3h7NXeOkZ
|
||||
github.com/cosmos/gogoproto v1.4.2/go.mod h1:cLxOsn1ljAHSV527CHOtaIP91kK6cCrZETRBrkzItWU=
|
||||
github.com/cosmos/gogoproto v1.7.0 h1:79USr0oyXAbxg3rspGh/m4SWNyoz/GLaAh0QlCe2fro=
|
||||
github.com/cosmos/gogoproto v1.7.0/go.mod h1:yWChEv5IUEYURQasfyBW5ffkMHR/90hiHgbNgrtp4j0=
|
||||
github.com/cosmos/gorocksdb v1.2.0 h1:d0l3jJG8M4hBouIZq0mDUHZ+zjOx044J3nGRskwTb4Y=
|
||||
github.com/cosmos/gorocksdb v1.2.0/go.mod h1:aaKvKItm514hKfNJpUJXnnOWeBnk2GL4+Qw9NHizILw=
|
||||
github.com/cosmos/iavl v0.20.1 h1:rM1kqeG3/HBT85vsZdoSNsehciqUQPWrR4BYmqE2+zg=
|
||||
github.com/cosmos/iavl v0.20.1/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A=
|
||||
github.com/cosmos/ibc-go/v7 v7.4.0 h1:8FqYMptvksgMvlbN4UW9jFxTXzsPyfAzEZurujXac8M=
|
||||
@ -1034,6 +1036,8 @@ github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70
|
||||
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
|
||||
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
|
||||
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
|
||||
github.com/tendermint/tm-db v0.6.7 h1:fE00Cbl0jayAoqlExN6oyQJ7fR/ZtoVOmvPJ//+shu8=
|
||||
github.com/tendermint/tm-db v0.6.7/go.mod h1:byQDzFkZV1syXr/ReXS808NxA2xvyuuVgXOJ/088L6I=
|
||||
github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg=
|
||||
github.com/tidwall/btree v1.6.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"github.com/planetmint/planetmint-go/x/der/types"
|
||||
)
|
||||
|
||||
func (k Keeper) StoreDerAttest(ctx sdk.Context, asset types.DER) {
|
||||
func (k Keeper) StoreDerAsset(ctx sdk.Context, asset types.DER) {
|
||||
appendValue := k.cdc.MustMarshal(&asset)
|
||||
k.storeAsset(ctx, types.KeyPrefix(types.DerAssetKey), asset.ZigbeeID, appendValue)
|
||||
}
|
||||
|
||||
57
x/der/keeper/der_asset_test.go
Normal file
57
x/der/keeper/der_asset_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/cosmos-sdk/store"
|
||||
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||
dertypes "github.com/planetmint/planetmint-go/x/der/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
dbm "github.com/cometbft/cometbft-db"
|
||||
|
||||
"github.com/cometbft/cometbft/libs/log"
|
||||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
)
|
||||
|
||||
func createTestKeeper(t *testing.T) (Keeper, sdk.Context) {
|
||||
db := dbm.NewMemDB()
|
||||
cms := store.NewCommitMultiStore(db)
|
||||
storeKey := sdk.NewKVStoreKey("der")
|
||||
memKey := storetypes.NewMemoryStoreKey("mem_der")
|
||||
cms.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
|
||||
cms.MountStoreWithDB(memKey, storetypes.StoreTypeMemory, nil)
|
||||
err := cms.LoadLatestVersion()
|
||||
require.NoError(t, err)
|
||||
|
||||
interfaceRegistry := cdctypes.NewInterfaceRegistry()
|
||||
cdc := codec.NewProtoCodec(interfaceRegistry)
|
||||
ps := paramtypes.NewSubspace(cdc, codec.NewLegacyAmino(), storeKey, memKey, "DerParams")
|
||||
keeper := NewKeeper(cdc, storeKey, memKey, ps, nil, "") // pass nil for MachineKeeper and empty rootDir for this test
|
||||
ctx := sdk.NewContext(cms, tmproto.Header{}, false, log.NewNopLogger())
|
||||
return *keeper, ctx
|
||||
}
|
||||
|
||||
func TestStoreDerAssetAndLookupDerAsset(t *testing.T) {
|
||||
keeper, ctx := createTestKeeper(t)
|
||||
asset := dertypes.DER{
|
||||
ZigbeeID: "test-zigbee-id",
|
||||
// Add other fields as needed for your DER struct
|
||||
}
|
||||
|
||||
keeper.StoreDerAsset(ctx, asset)
|
||||
result, found := keeper.LookupDerAsset(ctx, "test-zigbee-id")
|
||||
require.True(t, found)
|
||||
require.Equal(t, asset.ZigbeeID, result.ZigbeeID)
|
||||
// Add more assertions for other fields as needed
|
||||
}
|
||||
|
||||
func TestLookupDerAsset_NotFound(t *testing.T) {
|
||||
keeper, ctx := createTestKeeper(t)
|
||||
_, found := keeper.LookupDerAsset(ctx, "nonexistent-id")
|
||||
require.False(t, found)
|
||||
}
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"github.com/planetmint/planetmint-go/x/der/types"
|
||||
)
|
||||
|
||||
func (k Keeper) StoreLiquidDerAttest(ctx sdk.Context, asset types.LiquidDerAsset) {
|
||||
func (k Keeper) StoreLiquidDerAsset(ctx sdk.Context, asset types.LiquidDerAsset) {
|
||||
appendValue := k.cdc.MustMarshal(&asset)
|
||||
k.storeAsset(ctx, types.KeyPrefix(types.LiquidDerAssetKey), asset.ZigbeeID, appendValue)
|
||||
}
|
||||
|
||||
57
x/der/keeper/liquid_der_asset_test.go
Normal file
57
x/der/keeper/liquid_der_asset_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/cosmos-sdk/store"
|
||||
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||
dertypes "github.com/planetmint/planetmint-go/x/der/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
dbm "github.com/cometbft/cometbft-db"
|
||||
|
||||
"github.com/cometbft/cometbft/libs/log"
|
||||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
)
|
||||
|
||||
func createTestKeeperNft(t *testing.T) (Keeper, sdk.Context) {
|
||||
db := dbm.NewMemDB()
|
||||
cms := store.NewCommitMultiStore(db)
|
||||
storeKey := sdk.NewKVStoreKey("der")
|
||||
memKey := storetypes.NewMemoryStoreKey("mem_der")
|
||||
cms.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
|
||||
cms.MountStoreWithDB(memKey, storetypes.StoreTypeMemory, nil)
|
||||
err := cms.LoadLatestVersion()
|
||||
require.NoError(t, err)
|
||||
|
||||
interfaceRegistry := cdctypes.NewInterfaceRegistry()
|
||||
cdc := codec.NewProtoCodec(interfaceRegistry)
|
||||
ps := paramtypes.NewSubspace(cdc, codec.NewLegacyAmino(), storeKey, memKey, "DerParams")
|
||||
keeper := NewKeeper(cdc, storeKey, memKey, ps, nil, "")
|
||||
ctx := sdk.NewContext(cms, tmproto.Header{}, false, log.NewNopLogger())
|
||||
return *keeper, ctx
|
||||
}
|
||||
|
||||
func TestStoreLiquidDerAssetAndLookupLiquidDerAsset(t *testing.T) {
|
||||
keeper, ctx := createTestKeeperNft(t)
|
||||
asset := dertypes.LiquidDerAsset{
|
||||
ZigbeeID: "liquid-test-zigbee-id",
|
||||
// Add other fields as needed for your LiquidDerAsset struct
|
||||
}
|
||||
|
||||
keeper.StoreLiquidDerAsset(ctx, asset)
|
||||
result, found := keeper.LookupLiquidDerAsset(ctx, "liquid-test-zigbee-id")
|
||||
require.True(t, found)
|
||||
require.Equal(t, asset.ZigbeeID, result.ZigbeeID)
|
||||
// Add more assertions for other fields as needed
|
||||
}
|
||||
|
||||
func TestLookupLiquidDerAsset_NotFound(t *testing.T) {
|
||||
keeper, ctx := createTestKeeperNft(t)
|
||||
_, found := keeper.LookupLiquidDerAsset(ctx, "nonexistent-id")
|
||||
require.False(t, found)
|
||||
}
|
||||
@ -10,7 +10,7 @@ import (
|
||||
func (k msgServer) NotarizeLiquidDerAsset(goCtx context.Context, msg *types.MsgNotarizeLiquidDerAsset) (*types.MsgNotarizeLiquidDerAssetResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
|
||||
k.StoreLiquidDerAttest(ctx, *msg.DerAsset)
|
||||
k.StoreLiquidDerAsset(ctx, *msg.DerAsset)
|
||||
|
||||
return &types.MsgNotarizeLiquidDerAssetResponse{}, nil
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
func (k msgServer) RegisterDER(goCtx context.Context, msg *types.MsgRegisterDER) (*types.MsgRegisterDERResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
|
||||
k.StoreDerAttest(ctx, *msg.Der)
|
||||
k.StoreDerAsset(ctx, *msg.Der)
|
||||
|
||||
// Get machine params from MachineKeeper
|
||||
params := k.MachineKeeper.GetParams(ctx)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user