From d6b4e5bad88562e8eb4d7df56cc8492108142c88 Mon Sep 17 00:00:00 2001 From: Sergey Date: Tue, 23 Jul 2024 21:34:03 +0300 Subject: [PATCH] chore: add MsgSetWithdrawAddress tests --- pkg/messages/msg_set_withdraw_address.go | 2 +- pkg/messages/msg_set_withdraw_address_test.go | 114 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 pkg/messages/msg_set_withdraw_address_test.go diff --git a/pkg/messages/msg_set_withdraw_address.go b/pkg/messages/msg_set_withdraw_address.go index ce09e57..24c31c0 100644 --- a/pkg/messages/msg_set_withdraw_address.go +++ b/pkg/messages/msg_set_withdraw_address.go @@ -32,7 +32,7 @@ func ParseMsgSetWithdrawAddress(data []byte, chain *configTypes.Chain, height in }, nil } -func (m MsgSetWithdrawAddress) Type() string { +func (m *MsgSetWithdrawAddress) Type() string { return "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress" } diff --git a/pkg/messages/msg_set_withdraw_address_test.go b/pkg/messages/msg_set_withdraw_address_test.go new file mode 100644 index 0000000..4a1c13b --- /dev/null +++ b/pkg/messages/msg_set_withdraw_address_test.go @@ -0,0 +1,114 @@ +package messages + +import ( + cosmosDistributionTypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + aliasManagerPkg "main/pkg/alias_manager" + configPkg "main/pkg/config" + configTypes "main/pkg/config/types" + "main/pkg/data_fetcher" + "main/pkg/fs" + loggerPkg "main/pkg/logger" + "main/pkg/metrics" + "main/pkg/types" + "main/pkg/types/event" + "testing" + + cosmosTypes "github.com/cosmos/cosmos-sdk/types" + "github.com/gogo/protobuf/proto" + "github.com/stretchr/testify/require" +) + +func TestMsgSetWithdrawAddressParse(t *testing.T) { + t.Parallel() + + msg := &cosmosDistributionTypes.MsgSetWithdrawAddress{ + DelegatorAddress: "delegator", + WithdrawAddress: "withdraw", + } + msgBytes, err := proto.Marshal(msg) + require.NoError(t, err) + + parsed, err := ParseMsgSetWithdrawAddress(msgBytes, &configTypes.Chain{Name: "chain"}, 100) + require.NoError(t, err) + require.NotNil(t, parsed) + + parsed2, err2 := ParseMsgSetWithdrawAddress([]byte("aaa"), &configTypes.Chain{Name: "chain"}, 100) + require.Error(t, err2) + require.Nil(t, parsed2) +} + +func TestMsgSetWithdrawAddressBase(t *testing.T) { + t.Parallel() + + msg := &cosmosDistributionTypes.MsgSetWithdrawAddress{ + DelegatorAddress: "delegator", + WithdrawAddress: "withdraw", + } + msgBytes, err := proto.Marshal(msg) + require.NoError(t, err) + + parsed, err := ParseMsgSetWithdrawAddress(msgBytes, &configTypes.Chain{Name: "chain"}, 100) + require.NoError(t, err) + require.NotNil(t, parsed) + + require.Equal(t, "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress", parsed.Type()) + + values := parsed.GetValues() + + require.Equal(t, event.EventValues{ + event.From(cosmosTypes.EventTypeMessage, cosmosTypes.AttributeKeyAction, "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress"), + event.From(cosmosTypes.EventTypeMessage, cosmosTypes.AttributeKeySender, "delegator"), + }, values) + + parsed.AddParsedMessage(nil) + parsed.SetParsedMessages([]types.Message{}) + require.Empty(t, parsed.GetParsedMessages()) + require.Empty(t, parsed.GetRawMessages()) +} + +func TestMsgSetWithdrawAddressPopulate(t *testing.T) { + t.Parallel() + + msg := &cosmosDistributionTypes.MsgSetWithdrawAddress{ + DelegatorAddress: "delegator", + WithdrawAddress: "withdraw", + } + msgBytes, err := proto.Marshal(msg) + require.NoError(t, err) + + config := &configPkg.AppConfig{ + Chains: configTypes.Chains{ + { + Name: "chain", + ChainID: "chain-id", + Denoms: configTypes.DenomInfos{ + {Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6, CoingeckoCurrency: "cosmos"}, + }, + }, + }, + Metrics: configPkg.MetricsConfig{Enabled: false}, + AliasesPath: "path.toml", + } + + parsed, err := ParseMsgSetWithdrawAddress(msgBytes, config.Chains[0], 100) + require.NoError(t, err) + require.NotNil(t, parsed) + + filesystem := &fs.MockFs{} + logger := loggerPkg.GetNopLogger() + aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem) + metricsManager := metrics.NewManager(logger, config.Metrics) + dataFetcher := data_fetcher.NewDataFetcher(logger, config, aliasManager, metricsManager) + + err = aliasManager.Set("subscription", "chain", "delegator", "delegator_alias") + require.NoError(t, err) + err = aliasManager.Set("subscription", "chain", "withdraw", "withdraw_alias") + require.NoError(t, err) + + parsed.GetAdditionalData(dataFetcher, "subscription") + + msgSend, _ := parsed.(*MsgSetWithdrawAddress) + + require.Equal(t, "delegator_alias", msgSend.DelegatorAddress.Title) + require.Equal(t, "withdraw_alias", msgSend.WithdrawAddress.Title) +}