Skip to content

Commit

Permalink
revoke tk test cases fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-AssetMantle committed Jul 29, 2024
1 parent 2acb8d7 commit 8eed76c
Show file tree
Hide file tree
Showing 13 changed files with 426 additions and 246 deletions.
4 changes: 2 additions & 2 deletions x/assets/transactions/mutate/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
baseIDs "github.com/AssetMantle/schema/ids/base"
"github.com/AssetMantle/schema/lists"
baseLists "github.com/AssetMantle/schema/lists/base"
"github.com/cosmos/cosmos-sdk/codec"
sdkCodec "github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdkTypes "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -51,7 +51,7 @@ func (message *Message) GetSigners() []sdkTypes.AccAddress {
}
return []sdkTypes.AccAddress{from}
}
func (*Message) RegisterLegacyAminoCodec(legacyAmino *codec.LegacyAmino) {
func (*Message) RegisterLegacyAminoCodec(legacyAmino *sdkCodec.LegacyAmino) {
codecUtilities.RegisterModuleConcrete(legacyAmino, Message{})
}
func (message *Message) RegisterInterface(interfaceRegistry types.InterfaceRegistry) {
Expand Down
6 changes: 3 additions & 3 deletions x/assets/transactions/mutate/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
baseLists "github.com/AssetMantle/schema/lists/base"
baseProperties "github.com/AssetMantle/schema/properties/base"
baseQualified "github.com/AssetMantle/schema/qualified/base"
"github.com/cosmos/cosmos-sdk/codec"
sdkCodec "github.com/cosmos/cosmos-sdk/codec"
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/AssetMantle/modules/helpers"
Expand Down Expand Up @@ -105,14 +105,14 @@ func Test_message_GetSigners(t *testing.T) {
func Test_message_RegisterCodec(t *testing.T) {

type args struct {
legacyAmino *codec.LegacyAmino
legacyAmino *sdkCodec.LegacyAmino
}
tests := []struct {
name string
fields fields
args args
}{
{"+ve", fields{fromAccAddress.String(), fromID, testAssetID, mutableMetaProperties, mutableProperties}, args{codec.NewLegacyAmino()}},
{"+ve", fields{fromAccAddress.String(), fromID, testAssetID, mutableMetaProperties, mutableProperties}, args{sdkCodec.NewLegacyAmino()}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
286 changes: 210 additions & 76 deletions x/assets/transactions/mutate/transaction_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,110 +4,244 @@
package mutate

import (
"fmt"
storeTypes "github.com/cosmos/cosmos-sdk/store/types"
"reflect"
"testing"

"context"
"github.com/AssetMantle/modules/helpers"
baseHelpers "github.com/AssetMantle/modules/helpers/base"
errorConstants "github.com/AssetMantle/modules/helpers/constants"
dataHelper "github.com/AssetMantle/modules/simulation/schema/types/base"
"github.com/AssetMantle/modules/x/assets/constants"
"github.com/AssetMantle/modules/x/assets/mapper"
"github.com/AssetMantle/modules/x/classifications/auxiliaries/conform"
"github.com/AssetMantle/modules/x/identities/auxiliaries/authenticate"
"github.com/AssetMantle/modules/x/maintainers/auxiliaries/maintain"
"github.com/AssetMantle/schema/ids"
baseIDs "github.com/AssetMantle/schema/ids/base"
"github.com/AssetMantle/schema/lists"
baseLists "github.com/AssetMantle/schema/lists/base"
baseQualified "github.com/AssetMantle/schema/qualified/base"
tendermintDB "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/libs/log"
protoTendermintTypes "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/store"
storeTypes "github.com/cosmos/cosmos-sdk/store/types"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
authKeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govTypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramsKeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramsTypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
)

"github.com/AssetMantle/modules/helpers"
baseHelpers "github.com/AssetMantle/modules/helpers/base"
"github.com/AssetMantle/modules/x/assets/mapper"
"github.com/AssetMantle/modules/x/assets/parameters"
"github.com/AssetMantle/modules/x/classifications/auxiliaries/conform"
"github.com/AssetMantle/modules/x/identities/auxiliaries/authenticate"
"github.com/AssetMantle/modules/x/maintainers/auxiliaries/maintain"
type MockAuxiliary struct {
mock.Mock
}

var _ helpers.Auxiliary = (*MockAuxiliary)(nil)

func (mockAuxiliary *MockAuxiliary) GetName() string { panic(mockAuxiliary) }
func (mockAuxiliary *MockAuxiliary) GetKeeper() helpers.AuxiliaryKeeper {
args := mockAuxiliary.Called()
return args.Get(0).(helpers.AuxiliaryKeeper)
}
func (mockAuxiliary *MockAuxiliary) Initialize(_ helpers.Mapper, _ helpers.ParameterManager, _ ...interface{}) helpers.Auxiliary {
panic(mockAuxiliary)
}

type MockAuxiliaryKeeper struct {
mock.Mock
}

var _ helpers.AuxiliaryKeeper = (*MockAuxiliaryKeeper)(nil)

func (mockAuxiliaryKeeper *MockAuxiliaryKeeper) Help(context context.Context, request helpers.AuxiliaryRequest) (helpers.AuxiliaryResponse, error) {
args := mockAuxiliaryKeeper.Called(context, request)
return args.Get(0).(helpers.AuxiliaryResponse), args.Error(1)
}
func (mockAuxiliaryKeeper *MockAuxiliaryKeeper) Initialize(m2 helpers.Mapper, manager helpers.ParameterManager, i []interface{}) helpers.Keeper {
args := mockAuxiliaryKeeper.Called(m2, manager, i)
return args.Get(0).(helpers.Keeper)
}

const (
TestMinterModuleName = "testMinter"
Denom = "stake"
ChainID = "testChain"
GenesisSupply = 1000000000000
)

var (
authenticateAuxiliary helpers.Auxiliary
maintainAuxiliary helpers.Auxiliary
conformAuxiliary helpers.Auxiliary
)
moduleStoreKey = sdkTypes.NewKVStoreKey(constants.ModuleName)

type TestKeepers struct {
MutateKeeper helpers.TransactionKeeper
}
authenticateAuxiliaryKeeper = new(MockAuxiliaryKeeper)
authenticateAuxiliaryFailureAddress = sdkTypes.AccAddress(ed25519.GenPrivKey().PubKey().Address())
_ = authenticateAuxiliaryKeeper.On("Help", mock.Anything, authenticate.NewAuxiliaryRequest(authenticateAuxiliaryFailureAddress, baseIDs.PrototypeIdentityID())).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError)
_ = authenticateAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil)
authenticateAuxiliary = new(MockAuxiliary)
_ = authenticateAuxiliary.On("GetKeeper").Return(authenticateAuxiliaryKeeper)

func createTestInput(t *testing.T) (sdkTypes.Context, TestKeepers, helpers.Mapper, helpers.ParameterManager) {
// var legacyAmino = codec.NewLegacyAmino()
// schemaCodec.RegisterLegacyAminoCodec(legacyAmino)
// std.RegisterLegacyAminoCodec(legacyAmino)
// legacyAmino.Seal()
codec := baseHelpers.CodecPrototype()
storeKey := sdkTypes.NewKVStoreKey("test")
paramsStoreKey := sdkTypes.NewKVStoreKey("testParams")
paramsTransientStoreKeys := sdkTypes.NewTransientStoreKey("testParamsTransient")
Mapper := mapper.Prototype().Initialize(storeKey)
ParamsKeeper := paramsKeeper.NewKeeper(
codec.GetProtoCodec(),
codec.GetLegacyAmino(),
paramsStoreKey,
paramsTransientStoreKeys,
)
parameterManager := parameters.Prototype().Initialize(ParamsKeeper.Subspace("test"))
maintainAuxiliaryKeeper = new(MockAuxiliaryKeeper)
maintainAuxiliaryMutablesFailure = dataHelper.GenerateRandomMetaPropertyListWithoutData(rand.New(rand.NewSource(99)))
_ = maintainAuxiliaryKeeper.On("Help", mock.Anything, maintain.NewAuxiliaryRequest(baseIDs.PrototypeClassificationID(), baseIDs.PrototypeIdentityID(), baseQualified.NewMutables(maintainAuxiliaryMutablesFailure))).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError)
_ = maintainAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil)
maintainAuxiliary = new(MockAuxiliary)
_ = maintainAuxiliary.On("GetKeeper").Return(maintainAuxiliaryKeeper)

memDB := tendermintDB.NewMemDB()
commitMultiStore := store.NewCommitMultiStore(memDB)
commitMultiStore.MountStoreWithDB(storeKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsTransientStoreKeys, storeTypes.StoreTypeTransient, memDB)
err := commitMultiStore.LoadLatestVersion()
require.Nil(t, err)
conformAuxiliaryKeeper = new(MockAuxiliaryKeeper)
conformAuxiliaryMutablesFailure = dataHelper.GenerateRandomMetaPropertyListWithoutData(rand.New(rand.NewSource(99)))
_ = conformAuxiliaryKeeper.On("Help", mock.Anything, conform.NewAuxiliaryRequest(baseIDs.PrototypeClassificationID(), baseQualified.NewImmutables(baseLists.NewPropertyList()), baseQualified.NewMutables(conformAuxiliaryMutablesFailure))).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError)
_ = conformAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil)
conformAuxiliary = new(MockAuxiliary)
_ = conformAuxiliary.On("GetKeeper").Return(conformAuxiliaryKeeper)

codec = baseHelpers.TestCodec()

paramsStoreKey = sdkTypes.NewKVStoreKey(paramsTypes.StoreKey)
paramsTransientStoreKeys = sdkTypes.NewTransientStoreKey(paramsTypes.TStoreKey)
ParamsKeeper = paramsKeeper.NewKeeper(codec, codec.GetLegacyAmino(), paramsStoreKey, paramsTransientStoreKeys)

authStoreKey = sdkTypes.NewKVStoreKey(authTypes.StoreKey)
moduleAccountPermissions = map[string][]string{TestMinterModuleName: {authTypes.Minter}, constants.ModuleName: nil}
AuthKeeper = authKeeper.NewAccountKeeper(codec, authStoreKey, authTypes.ProtoBaseAccount, moduleAccountPermissions, sdkTypes.GetConfig().GetBech32AccountAddrPrefix(), authTypes.NewModuleAddress(govTypes.ModuleName).String())

context := sdkTypes.NewContext(commitMultiStore, protoTendermintTypes.Header{
ChainID: "test",
}, false, log.NewNopLogger())
bankStoreKey = sdkTypes.NewKVStoreKey(bankTypes.StoreKey)
blacklistedAddresses = map[string]bool{authTypes.NewModuleAddress(TestMinterModuleName).String(): false, authTypes.NewModuleAddress(constants.ModuleName).String(): false}
BankKeeper = bankKeeper.NewBaseKeeper(codec, bankStoreKey, AuthKeeper, blacklistedAddresses, authTypes.NewModuleAddress(govTypes.ModuleName).String())

maintainAuxiliary = maintain.Auxiliary.Initialize(Mapper, parameterManager)
conformAuxiliary = conform.Auxiliary.Initialize(Mapper, parameterManager)
authenticateAuxiliary = authenticate.Auxiliary.Initialize(Mapper, parameterManager)
Context = setContext()

keepers := TestKeepers{
MutateKeeper: keeperPrototype().Initialize(Mapper, parameterManager, []interface{}{}).(helpers.TransactionKeeper),
coinSupply = sdkTypes.NewCoins(sdkTypes.NewCoin(Denom, sdkTypes.NewInt(GenesisSupply)))
_ = BankKeeper.MintCoins(Context, TestMinterModuleName, coinSupply)

genesisAddress = sdkTypes.AccAddress(ed25519.GenPrivKey().PubKey().Address())
_ = BankKeeper.SendCoinsFromModuleToAccount(Context, TestMinterModuleName, genesisAddress, coinSupply)

TransactionKeeper = transactionKeeper{mapper.Prototype().Initialize(moduleStoreKey),
authenticateAuxiliary,
maintainAuxiliary,
conformAuxiliary,
}
)

return context, keepers, Mapper, parameterManager
func setContext() sdkTypes.Context {
memDB := tendermintDB.NewMemDB()
commitMultiStore := store.NewCommitMultiStore(memDB)
commitMultiStore.MountStoreWithDB(moduleStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(authStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(bankStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsStoreKey, storeTypes.StoreTypeIAVL, memDB)
commitMultiStore.MountStoreWithDB(paramsTransientStoreKeys, storeTypes.StoreTypeTransient, memDB)
_ = commitMultiStore.LoadLatestVersion()
return sdkTypes.NewContext(commitMultiStore, protoTendermintTypes.Header{ChainID: ChainID}, false, log.NewNopLogger())
}

func Test_transactionKeeper_Initialize(t *testing.T) {
_, _, Mapper, parameterManager := createTestInput(t)
type fields struct {
mapper helpers.Mapper
authenticateAuxiliary helpers.Auxiliary
maintainAuxiliary helpers.Auxiliary
conformAuxiliary helpers.Auxiliary
}
func TestTransactionKeeperTransact(t *testing.T) {
type args struct {
mapper helpers.Mapper
parameterManager helpers.ParameterManager
auxiliaries []interface{}
from sdkTypes.AccAddress
fromID ids.IdentityID
assetID ids.AssetID
mutableMetaProps lists.PropertyList
mutableProps lists.PropertyList
}

tests := []struct {
name string
fields fields
args args
want helpers.Keeper
name string
args args
setup func()
want *TransactionResponse
wantErr helpers.Error
}{
{"+ve", fields{Mapper, authenticateAuxiliary, maintainAuxiliary, conformAuxiliary}, args{Mapper, parameterManager, []interface{}{}}, transactionKeeper{Mapper, authenticateAuxiliary, maintainAuxiliary, conformAuxiliary}},
{"mutateValidAsset",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: baseIDs.PrototypeAssetID(),
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {
},
newTransactionResponse(),
nil,
},
{"mutateNonExistentAsset",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: baseIDs.PrototypeAssetID(),
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {},
nil,
errorConstants.EntityNotFound,
},
{"authenticateAuxiliaryFailure",
args{
from: authenticateAuxiliaryFailureAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: baseIDs.PrototypeAssetID(),
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: baseLists.NewPropertyList(),
},
func() {},
nil,
errorConstants.MockError,
},
{"maintainAuxiliaryFailure",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: baseIDs.PrototypeAssetID(),
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: maintainAuxiliaryMutablesFailure,
},
func() {
maintainAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError).Once()
},
nil,
errorConstants.MockError,
},
{"conformAuxiliaryFailure",
args{
from: genesisAddress,
fromID: baseIDs.PrototypeIdentityID(),
assetID: baseIDs.PrototypeAssetID(),
mutableMetaProps: baseLists.NewPropertyList(),
mutableProps: conformAuxiliaryMutablesFailure,
},
func() {
conformAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError).Once()
},
nil,
errorConstants.MockError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
transactionKeeper := transactionKeeper{
mapper: tt.fields.mapper,
authenticateAuxiliary: tt.fields.authenticateAuxiliary,
maintainAuxiliary: tt.fields.maintainAuxiliary,
conformAuxiliary: tt.fields.conformAuxiliary,
}
if got := transactionKeeper.Initialize(tt.args.mapper, tt.args.parameterManager, tt.args.auxiliaries); !reflect.DeepEqual(fmt.Sprint(got), fmt.Sprint(tt.want)) {
t.Errorf("Initialize() = %v, want %v", got, tt.want)
tt.setup()

got, err := TransactionKeeper.Transact(sdkTypes.WrapSDKContext(Context),
NewMessage(tt.args.from,
tt.args.fromID,
tt.args.assetID,
tt.args.mutableMetaProps.(lists.PropertyList),
tt.args.mutableProps.(lists.PropertyList)).(helpers.Message),
)

if tt.wantErr != nil {
require.Error(t, err)
require.Equal(t, tt.wantErr, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.want, got)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions x/assets/transactions/mutate/transaction_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
baseIDs "github.com/AssetMantle/schema/ids/base"
"github.com/AssetMantle/schema/lists/base"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdkCodec "github.com/cosmos/cosmos-sdk/codec"
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/AssetMantle/modules/helpers"
Expand Down Expand Up @@ -102,7 +102,7 @@ func (transactionRequest transactionRequest) MakeMsg() (sdkTypes.Msg, error) {
mutableProperties,
), nil
}
func (transactionRequest) RegisterLegacyAminoCodec(legacyAmino *codec.LegacyAmino) {
func (transactionRequest) RegisterLegacyAminoCodec(legacyAmino *sdkCodec.LegacyAmino) {
codecUtilities.RegisterModuleConcrete(legacyAmino, transactionRequest{})
}
func requestPrototype() helpers.TransactionRequest {
Expand Down
Loading

0 comments on commit 8eed76c

Please sign in to comment.