From 6ae05a2f18824b2a49ebfb3ade3fb37feacc2a76 Mon Sep 17 00:00:00 2001 From: Sid-AssetMantle Date: Tue, 23 Jul 2024 12:16:51 +0530 Subject: [PATCH] mint tk test cases fix v1 --- go.mod | 2 +- go.sum | 1 - x/assets/transactions/mint/message.go | 4 +- x/assets/transactions/mint/message_test.go | 8 +- .../mint/transaction_keeper_test.go | 408 ++++++++++++------ .../transactions/mint/transaction_request.go | 4 +- .../mint/transaction_request_test.go | 6 +- 7 files changed, 279 insertions(+), 154 deletions(-) diff --git a/go.mod b/go.mod index 5d06fce12..9bb027094 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( google.golang.org/grpc v1.62.1 ) -//replace github.com/AssetMantle/schema => ../schema +replace github.com/AssetMantle/schema => ../schema require ( cloud.google.com/go v0.112.0 // indirect diff --git a/go.sum b/go.sum index d16e1ca8b..1d8dbcb6f 100644 --- a/go.sum +++ b/go.sum @@ -209,7 +209,6 @@ git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFN git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= -github.com/AssetMantle/schema v0.0.0-20240206102051-27352b802163/go.mod h1:SindcyQCFIEKPOp4mNrnegXrJ2uwQfq3D1r+G93Gz3Q= github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM= github.com/Azure/azure-sdk-for-go/sdk/internal v0.8.3/go.mod h1:KLF4gFr6DcKFZwSuH8w8yEK6DpFl3LP5rhdvAb7Yz5I= github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.3.0/go.mod h1:tPaiy8S5bQ+S5sOiDlINkp7+Ef339+Nz5L5XO+cnOHo= diff --git a/x/assets/transactions/mint/message.go b/x/assets/transactions/mint/message.go index 9ab1d900e..d6283a62d 100644 --- a/x/assets/transactions/mint/message.go +++ b/x/assets/transactions/mint/message.go @@ -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" @@ -60,7 +60,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) { diff --git a/x/assets/transactions/mint/message_test.go b/x/assets/transactions/mint/message_test.go index f6e2bf3e0..fa8353995 100644 --- a/x/assets/transactions/mint/message_test.go +++ b/x/assets/transactions/mint/message_test.go @@ -4,6 +4,7 @@ package mint import ( + sdkCodec "github.com/cosmos/cosmos-sdk/codec" "reflect" "testing" @@ -14,7 +15,6 @@ 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" sdkTypes "github.com/cosmos/cosmos-sdk/types" "github.com/AssetMantle/modules/helpers" @@ -111,15 +111,15 @@ 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 with nil", fields{}, args{codec.NewLegacyAmino()}}, - {"+ve", fields{fromAccAddress.String(), fromID, fromID, classificationID, immutableMetaProperties, immutableProperties, mutableMetaProperties, mutableProperties}, args{codec.NewLegacyAmino()}}, + {"+ve with nil", fields{}, args{sdkCodec.NewLegacyAmino()}}, + {"+ve", fields{fromAccAddress.String(), fromID, fromID, classificationID, immutableMetaProperties, immutableProperties, mutableMetaProperties, mutableProperties}, args{sdkCodec.NewLegacyAmino()}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/x/assets/transactions/mint/transaction_keeper_test.go b/x/assets/transactions/mint/transaction_keeper_test.go index 54991a4ff..0bdfb3cfc 100644 --- a/x/assets/transactions/mint/transaction_keeper_test.go +++ b/x/assets/transactions/mint/transaction_keeper_test.go @@ -7,198 +7,324 @@ import ( "context" "github.com/AssetMantle/modules/helpers" baseHelpers "github.com/AssetMantle/modules/helpers/base" + errorConstants "github.com/AssetMantle/modules/helpers/constants" + "github.com/AssetMantle/modules/x/assets/constants" + "github.com/AssetMantle/modules/x/assets/key" "github.com/AssetMantle/modules/x/assets/mapper" "github.com/AssetMantle/modules/x/assets/parameters" - "github.com/AssetMantle/modules/x/assets/record" "github.com/AssetMantle/modules/x/classifications/auxiliaries/bond" "github.com/AssetMantle/modules/x/classifications/auxiliaries/conform" "github.com/AssetMantle/modules/x/identities/auxiliaries/authenticate" "github.com/AssetMantle/modules/x/maintainers/auxiliaries/authorize" + "github.com/AssetMantle/modules/x/orders/record" "github.com/AssetMantle/modules/x/splits/auxiliaries/mint" baseData "github.com/AssetMantle/schema/data/base" - "github.com/AssetMantle/schema/documents/base" + baseDocuments "github.com/AssetMantle/schema/documents/base" + "github.com/AssetMantle/schema/ids" baseIDs "github.com/AssetMantle/schema/ids/base" + "github.com/AssetMantle/schema/lists" baseLists "github.com/AssetMantle/schema/lists/base" + "github.com/AssetMantle/schema/parameters/base" baseProperties "github.com/AssetMantle/schema/properties/base" + constantProperties "github.com/AssetMantle/schema/properties/constants" 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" - "github.com/stretchr/testify/require" + paramsTypes "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/stretchr/testify/mock" "reflect" "testing" ) -var ( - authenticateAuxiliary helpers.Auxiliary - authorizeAuxiliary helpers.Auxiliary - bondAuxiliary helpers.Auxiliary - conformAuxiliary helpers.Auxiliary - mintAuxiliary helpers.Auxiliary -) +// start +type MockAuxiliary struct { + mock.Mock +} -type TestKeepers struct { - MintKeeper helpers.TransactionKeeper +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) } -func createTestInput(t *testing.T) (sdkTypes.Context, TestKeepers, helpers.Mapper, helpers.ParameterManager) { - var legacyAmino = baseHelpers.CodecPrototype().GetLegacyAmino() +type MockAuxiliaryKeeper struct { + mock.Mock +} - storeKey := sdkTypes.NewKVStoreKey("test") - paramsStoreKey := sdkTypes.NewKVStoreKey("testParams") - paramsTransientStoreKeys := sdkTypes.NewTransientStoreKey("testParamsTransient") - Mapper := mapper.Prototype().Initialize(storeKey) - codec := baseHelpers.TestCodec() - ParamsKeeper := paramsKeeper.NewKeeper( +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 ( + moduleStoreKey = sdkTypes.NewKVStoreKey(constants.ModuleName) + + 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) + authorizeAuxiliaryKeeper = new(MockAuxiliaryKeeper) + authorizeAuxiliaryFailureClassificationID = baseIDs.NewClassificationID(baseQualified.NewImmutables(baseLists.NewPropertyList()), baseQualified.NewMutables(baseLists.NewPropertyList())) + _ = authorizeAuxiliaryKeeper.On("Help", mock.Anything, authorize.NewAuxiliaryRequest(authorizeAuxiliaryFailureClassificationID, baseIDs.PrototypeIdentityID(), baseIDs.NewStringID(""))).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError) + _ = authorizeAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil) + authorizeAuxiliary = new(MockAuxiliary) + _ = authorizeAuxiliary.On("GetKeeper").Return(authorizeAuxiliaryKeeper) + mintAuxiliaryKeeper = new(MockAuxiliaryKeeper) + mintAuxiliaryFailureID = baseIDs.NewIdentityID(baseIDs.NewClassificationID(baseQualified.NewImmutables(baseLists.NewPropertyList()), baseQualified.NewMutables(baseLists.NewPropertyList(baseProperties.NewMetaProperty(constantProperties.BondAmountProperty.GetID().GetKey(), baseData.NewNumberData(sdkTypes.NewInt(1)))))), baseQualified.NewImmutables(baseLists.NewPropertyList())) + _ = mintAuxiliaryKeeper.On("Help", mock.Anything, mint.NewAuxiliaryRequest(mintAuxiliaryFailureID, baseDocuments.NewCoinAsset(Denom).GetCoinAssetID(), sdkTypes.OneInt())).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError) + _ = mintAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil) + mintAuxiliary = new(MockAuxiliary) + _ = mintAuxiliary.On("GetKeeper").Return(mintAuxiliaryKeeper) + bondAuxiliaryKeeper = new(MockAuxiliaryKeeper) + bondAuxiliaryFailureAddress = sdkTypes.AccAddress(ed25519.GenPrivKey().PubKey().Address()) + _ = bondAuxiliaryKeeper.On("Help", mock.Anything, bond.NewAuxiliaryRequest(baseIDs.PrototypeClassificationID(), bondAuxiliaryFailureAddress, sdkTypes.OneInt())).Return(new(helpers.AuxiliaryResponse), errorConstants.MockError) + _ = bondAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil) + _ = bondAuxiliaryKeeper.On("Help", mock.Anything, mock.Anything).Return(new(helpers.AuxiliaryResponse), nil) + bondAuxiliary = new(MockAuxiliary) + _ = bondAuxiliary.On("GetKeeper").Return(bondAuxiliaryKeeper) + conformAuxiliaryKeeper = new(MockAuxiliaryKeeper) + conformAuxiliaryFailureID = baseIDs.PrototypeClassificationID() + _ = conformAuxiliaryKeeper.On("Help", mock.Anything, conform.NewAuxiliaryRequest(conformAuxiliaryFailureID, baseQualified.NewImmutables(baseLists.NewPropertyList()), baseQualified.NewMutables(baseLists.NewPropertyList()))).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, - legacyAmino, + codec.GetLegacyAmino(), paramsStoreKey, paramsTransientStoreKeys, ) - parameterManager := parameters.Prototype().Initialize(ParamsKeeper.Subspace("test")) - 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) + 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()) - authorizeAuxiliary = authorize.Auxiliary.Initialize(Mapper, parameterManager) - authenticateAuxiliary = authenticate.Auxiliary.Initialize(Mapper, parameterManager) - bondAuxiliary = bond.Auxiliary.Initialize(Mapper, parameterManager) - conformAuxiliary = conform.Auxiliary.Initialize(Mapper, parameterManager) - mintAuxiliary = mint.Auxiliary.Initialize(Mapper, parameterManager) + Context = setContext() - keepers := TestKeepers{ - MintKeeper: keeperPrototype().Initialize(Mapper, parameterManager, []interface{}{}).(helpers.TransactionKeeper), - } + coinSupply = sdkTypes.NewCoins(sdkTypes.NewCoin(Denom, sdkTypes.NewInt(GenesisSupply))) + _ = BankKeeper.MintCoins(Context, TestMinterModuleName, coinSupply) - return Context, keepers, Mapper, parameterManager -} + genesisAddress = sdkTypes.AccAddress(ed25519.GenPrivKey().PubKey().Address()) + _ = BankKeeper.SendCoinsFromModuleToAccount(Context, TestMinterModuleName, genesisAddress, coinSupply) + //parameterManager = parameters.Prototype().Initialize(ParamsKeeper.Subspace("test")) + parameterManager = parameters.Prototype().Initialize(ParamsKeeper.Subspace(constants.ModuleName).WithKeyTable(parameters.Prototype().GetKeyTable())). + Set(sdkTypes.WrapSDKContext(Context), baseLists.NewParameterList(base.NewParameter(baseProperties.NewMetaProperty(constantProperties.WrapAllowedCoinsProperty.GetKey(), baseData.NewListData(baseData.NewStringData(Denom)))))). + Set(sdkTypes.WrapSDKContext(Context), baseLists.NewParameterList(base.NewParameter(baseProperties.NewMetaProperty(constantProperties.BurnEnabledProperty.GetKey(), baseData.NewBooleanData(true))))). + Set(sdkTypes.WrapSDKContext(Context), baseLists.NewParameterList(base.NewParameter(baseProperties.NewMetaProperty(constantProperties.MintEnabledProperty.GetKey(), baseData.NewBooleanData(true))))). + Set(sdkTypes.WrapSDKContext(Context), baseLists.NewParameterList(base.NewParameter(baseProperties.NewMetaProperty(constantProperties.RenumerateEnabledProperty.GetKey(), baseData.NewBooleanData(true))))). + Set(sdkTypes.WrapSDKContext(Context), baseLists.NewParameterList(base.NewParameter(baseProperties.NewMetaProperty(constantProperties.UnwrapAllowedCoinsProperty.GetKey(), baseData.NewListData(baseData.NewStringData(Denom)))))) -func Test_keeperPrototype(t *testing.T) { - tests := []struct { - name string - want helpers.TransactionKeeper - }{ - {"+ve", transactionKeeper{}}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := keeperPrototype(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("keeperPrototype() = %v, want %v", got, tt.want) - } - }) + TransactionKeeper = transactionKeeper{ + mapper: mapper.Prototype().Initialize(moduleStoreKey), + parameterManager: parameterManager, + authenticateAuxiliary: authenticateAuxiliary, + authorizeAuxiliary: authorizeAuxiliary, + bondAuxiliary: bondAuxiliary, + conformAuxiliary: conformAuxiliary, + mintAuxiliary: mintAuxiliary, } +) + +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 -// parameterManager helpers.ParameterManager -// conformAuxiliary helpers.Auxiliary -// mintAuxiliary helpers.Auxiliary -// authenticateAuxiliary helpers.Auxiliary -// authorizeAuxiliary helpers.Auxiliary -// } -// type args struct { -// mapper helpers.Mapper -// parameterManager helpers.ParameterManager -// auxiliaries []interface{} -// } -// tests := []struct { -// name string -// fields fields -// args args -// want helpers.Keeper -// }{ -// {"+ve", fields{Mapper, Parameters, conformAuxiliary, mintAuxiliary, authenticateAuxiliary, authorizeAuxiliary}, args{Mapper, Parameters, []interface{}{}}, transactionKeeper{Mapper, Parameters, conformAuxiliary, mintAuxiliary, authenticateAuxiliary, authorizeAuxiliary}}, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// transactionKeeper := transactionKeeper{ -// mapper: tt.fields.mapper, -// parameterManager: tt.fields.parameterManager, -// conformAuxiliary: tt.fields.conformAuxiliary, -// mintAuxiliary: tt.fields.mintAuxiliary, -// authenticateAuxiliary: tt.fields.authenticateAuxiliary, -// authorizeAuxiliary: tt.fields.authorizeAuxiliary, -// } -// 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) -// } -// }) -// } -// } - -func Test_transactionKeeper_Transact(t *testing.T) { - Context, keepers, Mapper, parameterManager := createTestInput(t) - immutableProperties := baseLists.NewPropertyList(baseProperties.NewMesaProperty(baseIDs.NewStringID("ID1"), baseData.NewStringData("ImmutableData"))) - immutableMetaProperties := baseLists.NewPropertyList(baseProperties.NewMetaProperty(baseIDs.NewStringID("ID1"), baseData.NewStringData("ImmutableData"))) - immutables := baseQualified.NewImmutables(immutableMetaProperties) - mutableProperties := baseLists.NewPropertyList(baseProperties.NewMesaProperty(baseIDs.NewStringID("authentication"), baseData.NewListData())) - mutableMetaProperties := baseLists.NewPropertyList(baseProperties.NewMetaProperty(baseIDs.NewStringID("authentication"), baseData.NewListData())) - mutables := baseQualified.NewMutables(mutableMetaProperties) - classificationID := baseIDs.NewClassificationID(immutables, mutables) - testAsset := base.NewAsset(classificationID, immutables, mutables) - fromAddress := "cosmos1pkkayn066msg6kn33wnl5srhdt3tnu2vzasz9c" - fromAccAddress, err := sdkTypes.AccAddressFromBech32(fromAddress) - require.Nil(t, err) - fromID := baseIDs.NewIdentityID(classificationID, immutables) - keepers.MintKeeper.(transactionKeeper).mapper.NewCollection(sdkTypes.WrapSDKContext(Context)).Add(record.NewRecord(testAsset)) - - type fields struct { - mapper helpers.Mapper - parameterManager helpers.ParameterManager - authenticateAuxiliary helpers.Auxiliary - authorizeAuxiliary helpers.Auxiliary - bondAuxiliary helpers.Auxiliary - conformAuxiliary helpers.Auxiliary - mintAuxiliary helpers.Auxiliary - } +func TestTransactionKeeperTransact(t *testing.T) { + type args struct { - context context.Context - message helpers.Message + from sdkTypes.AccAddress + fromID ids.IdentityID + toID ids.IdentityID + classificationID ids.ClassificationID + immutableProps lists.PropertyList + mutableProps lists.PropertyList } tests := []struct { name string - fields fields args args - want helpers.TransactionResponse - wantErr bool + setup func(*testing.T) + want *TransactionResponse + wantErr helpers.Error }{ - {"+ve", fields{Mapper, parameterManager, authenticateAuxiliary, authorizeAuxiliary, bondAuxiliary, conformAuxiliary, mintAuxiliary}, args{Context.Context(), NewMessage(fromAccAddress, fromID, fromID, classificationID, immutableMetaProperties, immutableProperties, mutableMetaProperties, mutableProperties).(*Message)}, newTransactionResponse(nil), false}, - {"+ve Entity Already Exists", fields{Mapper, parameterManager, authenticateAuxiliary, authorizeAuxiliary, bondAuxiliary, conformAuxiliary, mintAuxiliary}, args{Context.Context(), NewMessage(fromAccAddress, fromID, fromID, classificationID, immutableMetaProperties, immutableProperties, mutableMetaProperties, mutableProperties).(*Message)}, newTransactionResponse(nil), false}, + { + name: "minting not enabled", + args: args{ + from: genesisAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: baseIDs.PrototypeClassificationID(), + immutableProps: baseLists.NewPropertyList(), + mutableProps: baseLists.NewPropertyList(), + }, + setup: func(t *testing.T) { + parameterManager.Set(sdkTypes.WrapSDKContext(Context), baseLists.NewParameterList(base.NewParameter(baseProperties.NewMetaProperty(constantProperties.MintEnabledProperty.GetKey(), baseData.NewBooleanData(false))))) + }, + wantErr: errorConstants.NotAuthorized, + }, + { + name: "authorization failure", + args: args{ + from: genesisAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: authorizeAuxiliaryFailureClassificationID, + immutableProps: baseLists.NewPropertyList(), + mutableProps: baseLists.NewPropertyList(), + }, + setup: func(t *testing.T) { + parameterManager.Set(sdkTypes.WrapSDKContext(Context), baseLists.NewParameterList(base.NewParameter(baseProperties.NewMetaProperty(constantProperties.MintEnabledProperty.GetKey(), baseData.NewBooleanData(true))))) + }, + wantErr: errorConstants.MockError, + }, + { + name: "authentication failure", + args: args{ + from: authenticateAuxiliaryFailureAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: baseIDs.PrototypeClassificationID(), + immutableProps: baseLists.NewPropertyList(), + mutableProps: baseLists.NewPropertyList(), + }, + setup: func(t *testing.T) {}, + wantErr: errorConstants.MockError, + }, + { + name: "asset already exists", + args: args{ + from: genesisAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: baseIDs.PrototypeClassificationID(), + immutableProps: baseLists.NewPropertyList(baseProperties.NewMetaProperty(baseIDs.NewStringID("prop1"), baseData.NewStringData("val1"))), + mutableProps: baseLists.NewPropertyList(), + }, + setup: func(t *testing.T) { + assetID := baseIDs.NewAssetID(baseIDs.PrototypeClassificationID(), baseQualified.NewImmutables(baseLists.NewPropertyList(baseProperties.NewMetaProperty(baseIDs.NewStringID("prop1"), baseData.NewStringData("val1"))))) + assets := TransactionKeeper.mapper.NewCollection(sdkTypes.WrapSDKContext(Context)).Fetch(key.NewKey(assetID)) + assets.Add(record.NewRecord( + baseDocuments.NewOrder(baseIDs.PrototypeClassificationID(), + baseQualified.NewImmutables(baseLists.NewPropertyList(baseProperties.NewMetaProperty(baseIDs.NewStringID("prop1"), baseData.NewStringData("val1")))), + baseQualified.NewMutables(baseLists.NewPropertyList())))) + }, + wantErr: errorConstants.EntityAlreadyExists, + }, + { + name: "conform auxiliary failure", + args: args{ + from: genesisAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: conformAuxiliaryFailureID, + immutableProps: baseLists.NewPropertyList(), + mutableProps: baseLists.NewPropertyList(), + }, + setup: func(t *testing.T) {}, + wantErr: errorConstants.MockError, + }, + { + name: "asset supply negative", + args: args{ + from: genesisAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: baseIDs.PrototypeClassificationID(), + immutableProps: baseLists.NewPropertyList(), + mutableProps: baseLists.NewPropertyList(baseProperties.NewMetaProperty(constantProperties.SupplyProperty.GetID().GetKey(), baseData.NewNumberData(sdkTypes.NewInt(-1)))), + }, + setup: func(t *testing.T) {}, + wantErr: errorConstants.IncorrectFormat, + }, + { + name: "bond auxiliary failure", + args: args{ + from: bondAuxiliaryFailureAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: baseIDs.PrototypeClassificationID(), + immutableProps: baseLists.NewPropertyList(), + mutableProps: baseLists.NewPropertyList(baseProperties.NewMetaProperty(constantProperties.BondAmountProperty.GetID().GetKey(), baseData.NewNumberData(sdkTypes.NewInt(1)))), + }, + setup: func(t *testing.T) { + }, + wantErr: errorConstants.MockError, + }, + { + name: "no bond amount property", + args: args{ + from: genesisAddress, + toID: baseIDs.PrototypeIdentityID(), + classificationID: baseIDs.PrototypeClassificationID(), + immutableProps: baseLists.NewPropertyList(), + mutableProps: baseLists.NewPropertyList(baseProperties.NewMetaProperty(baseIDs.NewStringID("prop1"), baseData.NewNumberData(sdkTypes.NewInt(1)))), + }, + setup: func(t *testing.T) {}, + wantErr: errorConstants.MetaDataError, + }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - transactionKeeper := transactionKeeper{ - mapper: tt.fields.mapper, - parameterManager: tt.fields.parameterManager, - authenticateAuxiliary: tt.fields.authenticateAuxiliary, - authorizeAuxiliary: tt.fields.authorizeAuxiliary, - bondAuxiliary: tt.fields.bondAuxiliary, - conformAuxiliary: tt.fields.conformAuxiliary, - mintAuxiliary: tt.fields.mintAuxiliary, - } - got, err := transactionKeeper.Transact(tt.args.context, tt.args.message) - if (err != nil) != tt.wantErr { - t.Errorf("Transact() error = %v, wantErr %v", err, tt.wantErr) - return + + tt.setup(t) + //fmt.Print(tt.args.classificationID) + got, err := TransactionKeeper.Transact(sdkTypes.WrapSDKContext(Context), NewMessage( + tt.args.from, + baseIDs.PrototypeIdentityID(), + tt.args.toID, + tt.args.classificationID, + baseLists.NewPropertyList(), + tt.args.immutableProps, + baseLists.NewPropertyList(), + tt.args.mutableProps, + ).(helpers.Message)) + + if (err != nil) && !tt.wantErr.Is(err) { + t.Errorf("unexpected error: %v", err) } + if !reflect.DeepEqual(got, tt.want) { - t.Errorf("Transact() got = %v, want %v", got, tt.want) + t.Error("unexpected response") } }) } diff --git a/x/assets/transactions/mint/transaction_request.go b/x/assets/transactions/mint/transaction_request.go index 0afdb9c80..b31323164 100644 --- a/x/assets/transactions/mint/transaction_request.go +++ b/x/assets/transactions/mint/transaction_request.go @@ -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" @@ -127,7 +127,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 { diff --git a/x/assets/transactions/mint/transaction_request_test.go b/x/assets/transactions/mint/transaction_request_test.go index d0388e366..80964b396 100644 --- a/x/assets/transactions/mint/transaction_request_test.go +++ b/x/assets/transactions/mint/transaction_request_test.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "github.com/AssetMantle/modules/utilities/rest" + sdkCodec "github.com/cosmos/cosmos-sdk/codec" "reflect" "testing" @@ -15,7 +16,6 @@ import ( baseLists "github.com/AssetMantle/schema/lists/base" baseProperties "github.com/AssetMantle/schema/properties/base" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/viper" @@ -276,14 +276,14 @@ func Test_transactionRequest_RegisterCodec(t *testing.T) { MutableProperties string } type args struct { - legacyAmino *codec.LegacyAmino + legacyAmino *sdkCodec.LegacyAmino } tests := []struct { name string fields fields args args }{ - {"+ve", fields{testBaseRequest, fromID.AsString(), fromID.AsString(), classificationID.AsString(), immutableMetaPropertiesString, immutablePropertiesString, mutableMetaPropertiesString, mutablePropertiesString}, args{codec.NewLegacyAmino()}}, + {"+ve", fields{testBaseRequest, fromID.AsString(), fromID.AsString(), classificationID.AsString(), immutableMetaPropertiesString, immutablePropertiesString, mutableMetaPropertiesString, mutablePropertiesString}, args{sdkCodec.NewLegacyAmino()}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {