Skip to content

Commit

Permalink
test(fungible/types): increase code coverage for types (#953)
Browse files Browse the repository at this point in the history
* more samples

* types coverage

* imports
  • Loading branch information
lumtis authored Aug 15, 2023
1 parent a9cc82d commit db09ffe
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 5 deletions.
6 changes: 6 additions & 0 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sample
import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
)

// AccAddress returns a sample account address
Expand All @@ -19,3 +20,8 @@ func PrivKeyAddressPair() (*ed25519.PrivKey, sdk.AccAddress) {

return privKey, sdk.AccAddress(addr)
}

// EthAddress returns a sample ethereum address
func EthAddress() ethcommon.Address {
return ethcommon.HexToAddress(AccAddress())
}
13 changes: 13 additions & 0 deletions testutil/sample/sample_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sample

import (
"testing"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func TestEthAddress(t *testing.T) {
ethAddress := EthAddress()
require.NotEqual(t, ethcommon.Address{}, ethAddress)
}
13 changes: 11 additions & 2 deletions x/fungible/types/message_deploy_fungible_coin_zrc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ func TestMsgDeployFungibleCoinZRC4_ValidateBasic(t *testing.T) {
Creator: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
},
{
name: "invalid gas limit",
msg: MsgDeployFungibleCoinZRC20{
Creator: sample.AccAddress(),
GasLimit: -1,
},
err: sdkerrors.ErrInvalidGasLimit,
},
{
name: "valid message",
msg: MsgDeployFungibleCoinZRC20{
Creator: sample.AccAddress(),
},
Expand Down
3 changes: 2 additions & 1 deletion x/fungible/types/message_remove_foreign_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestMsgRemoveForeignCoin_ValidateBasic(t *testing.T) {
Creator: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
},
{
name: "valid address",
msg: MsgRemoveForeignCoin{
Creator: sample.AccAddress(),
Expand Down
51 changes: 51 additions & 0 deletions x/fungible/types/message_update_system_contract_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package types

import (
"testing"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/testutil/sample"
)

func TestMsgUpdateSystemContract_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgUpdateSystemContract
err error
}{
{
name: "invalid address",
msg: MsgUpdateSystemContract{
Creator: "invalid_address",
NewSystemContractAddress: sample.EthAddress().String(),
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "invalid new system contract address",
msg: MsgUpdateSystemContract{
Creator: sample.AccAddress(),
NewSystemContractAddress: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "valid message",
msg: MsgUpdateSystemContract{
Creator: sample.AccAddress(),
NewSystemContractAddress: sample.EthAddress().String(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}
3 changes: 3 additions & 0 deletions x/fungible/types/message_update_zrc20_withdraw_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (msg *MsgUpdateZRC20WithdrawFee) ValidateBasic() error {
if ethcommon.HexToAddress(msg.Zrc20Address) == (ethcommon.Address{}) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid system contract address (%s)", msg.Zrc20Address)
}
if msg.NewWithdrawFee.IsNil() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid withdraw fee (%s)", msg.NewWithdrawFee)
}

return nil
}
63 changes: 63 additions & 0 deletions x/fungible/types/message_update_zrc20_withdraw_fee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package types

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/testutil/sample"
)

func TestMsgUpdateZRC20WithdrawFee_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgUpdateZRC20WithdrawFee
err error
}{
{
name: "invalid address",
msg: MsgUpdateZRC20WithdrawFee{
Creator: "invalid_address",
Zrc20Address: sample.EthAddress().String(),
NewWithdrawFee: sdk.NewUint(1),
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "invalid new system contract address",
msg: MsgUpdateZRC20WithdrawFee{
Creator: sample.AccAddress(),
Zrc20Address: "invalid_address",
NewWithdrawFee: sdk.NewUint(1),
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "invalid new withdraw fee",
msg: MsgUpdateZRC20WithdrawFee{
Creator: sample.AccAddress(),
Zrc20Address: sample.EthAddress().String(),
},
err: sdkerrors.ErrInvalidRequest,
},
{
name: "valid message",
msg: MsgUpdateZRC20WithdrawFee{
Creator: sample.AccAddress(),
Zrc20Address: sample.EthAddress().String(),
NewWithdrawFee: sdk.NewUint(1),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}
1 change: 0 additions & 1 deletion x/fungible/types/types.go

This file was deleted.

3 changes: 2 additions & 1 deletion x/fungible/types/zrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ type ZRC20BoolResponse struct {
Value bool
}

// ZRC20StringResponse defines the string value from the call response
// UniswapV2FactoryByte32Response defines the string value from the call response
type UniswapV2FactoryByte32Response struct {
Value [32]byte
}

// SystemAddressResponse defines the address value from the call response
type SystemAddressResponse struct {
Value ethcommon.Address
}
Expand Down

0 comments on commit db09ffe

Please sign in to comment.