Skip to content

Commit

Permalink
update more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinssgh committed Mar 26, 2024
1 parent a5cf1e0 commit a478069
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 27 deletions.
112 changes: 112 additions & 0 deletions zetaclient/testutils/stub/keyring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package stub

import (
ckeys "github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var TestKeyringPair cryptotypes.PrivKey

func init() {
TestKeyringPair = cryptotypes.PrivKey(secp256k1.GenPrivKey())
}

type mockKeyring struct {
}

var _ ckeys.Keyring = mockKeyring{}

func NewMockKeyring() ckeys.Keyring {
return mockKeyring{}
}

func (m mockKeyring) Backend() string {
return ""
}

func (m mockKeyring) List() ([]*ckeys.Record, error) {
return nil, nil
}

func (m mockKeyring) SupportedAlgorithms() (ckeys.SigningAlgoList, ckeys.SigningAlgoList) {
return nil, nil
}

func (m mockKeyring) Key(_ string) (*ckeys.Record, error) {
record, _ := ckeys.NewLocalRecord("", TestKeyringPair, TestKeyringPair.PubKey())
return record, nil
}

func (m mockKeyring) KeyByAddress(_ sdk.Address) (*ckeys.Record, error) {
return nil, nil
}

func (m mockKeyring) Delete(_ string) error {
return nil
}

func (m mockKeyring) DeleteByAddress(_ sdk.Address) error {
return nil
}

func (m mockKeyring) Rename(_ string, _ string) error {
return nil
}

func (m mockKeyring) NewMnemonic(_ string, _ ckeys.Language, _, _ string, _ ckeys.SignatureAlgo) (*ckeys.Record, string, error) {
return nil, "", nil
}

func (m mockKeyring) NewAccount(_, _, _, _ string, _ ckeys.SignatureAlgo) (*ckeys.Record, error) {
return nil, nil
}

func (m mockKeyring) SaveLedgerKey(_ string, _ ckeys.SignatureAlgo, _ string, _, _, _ uint32) (*ckeys.Record, error) {
return nil, nil
}

func (m mockKeyring) SaveOfflineKey(_ string, _ cryptotypes.PubKey) (*ckeys.Record, error) {
return nil, nil
}

func (m mockKeyring) SaveMultisig(_ string, _ cryptotypes.PubKey) (*ckeys.Record, error) {
return nil, nil
}

func (m mockKeyring) Sign(_ string, _ []byte) ([]byte, cryptotypes.PubKey, error) {
return nil, nil, nil
}

func (m mockKeyring) SignByAddress(_ sdk.Address, _ []byte) ([]byte, cryptotypes.PubKey, error) {
return nil, nil, nil
}

func (m mockKeyring) ImportPrivKey(_, _, _ string) error {
return nil
}

func (m mockKeyring) ImportPubKey(_ string, _ string) error {
return nil
}

func (m mockKeyring) ExportPubKeyArmor(_ string) (string, error) {
return "", nil
}

func (m mockKeyring) ExportPubKeyArmorByAddress(_ sdk.Address) (string, error) {
return "", nil
}

func (m mockKeyring) ExportPrivKeyArmor(_, _ string) (armor string, err error) {
return "", nil
}

func (m mockKeyring) ExportPrivKeyArmorByAddress(_ sdk.Address, _ string) (armor string, err error) {
return "", nil
}

func (m mockKeyring) MigrateAll() ([]*ckeys.Record, error) {
return nil, nil
}
50 changes: 23 additions & 27 deletions zetaclient/zetabridge/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/keys"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/testutils/stub"
"go.nhat.io/grpcmock"
"go.nhat.io/grpcmock/planner"

Expand Down Expand Up @@ -48,7 +49,6 @@ func closeMockServer(t *testing.T, server *grpcmock.Server) {
}

func setupCorBridge() (*ZetaCoreBridge, error) {
keys.SetupConfigForTest()
return NewZetaCoreBridge(
&keys.Keys{},
"127.0.0.1",
Expand Down Expand Up @@ -846,30 +846,26 @@ func TestZetaCoreBridge_HasVoted(t *testing.T) {
}

func TestZetaCoreBridge_GetZetaHotKeyBalance(t *testing.T) {
//expectedOutput := banktypes.QueryBalanceResponse{
// Balance: &types.Coin{
// Denom: config.BaseDenom,
// Amount: types.NewInt(55646484),
// },
//}
//input := banktypes.QueryBalanceRequest{
// Address: "zeta1qn2rzc0ac5utg4esh2rmertzkwq6vrdehl8ac7",
// Denom: config.BaseDenom,
//}
//method := "/cosmos.bank.v1beta1.Query/Balance"
//server := setupMockServer(t, banktypes.RegisterQueryServer, method, input, expectedOutput)
//server.Serve()
//defer closeMockServer(t, server)
//
//zetabridge, err := setupCorBridge()
//require.NoError(t, err)
//addr, err := types.AccAddressFromBech32("zeta1qn2rzc0ac5utg4esh2rmertzkwq6vrdehl8ac7")
//require.NoError(t, err)
//zetabridge.keys = &keys.Keys{
// OperatorAddress: addr,
//}
//
//resp, err := zetabridge.GetZetaHotKeyBalance()
//require.NoError(t, err)
//require.Equal(t, expectedOutput.Balance.Amount, resp)
expectedOutput := banktypes.QueryBalanceResponse{
Balance: &types.Coin{
Denom: config.BaseDenom,
Amount: types.NewInt(55646484),
},
}
input := banktypes.QueryBalanceRequest{
Address: types.AccAddress(stub.TestKeyringPair.PubKey().Address().Bytes()).String(),
Denom: config.BaseDenom,
}
method := "/cosmos.bank.v1beta1.Query/Balance"
server := setupMockServer(t, banktypes.RegisterQueryServer, method, input, expectedOutput)
server.Serve()
defer closeMockServer(t, server)

zetabridge, err := setupCorBridge()
require.NoError(t, err)
zetabridge.keys = keys.NewKeysWithKeybase(stub.NewMockKeyring(), types.AccAddress{}, "", "")

resp, err := zetabridge.GetZetaHotKeyBalance()
require.NoError(t, err)
require.Equal(t, expectedOutput.Balance.Amount, resp)
}

0 comments on commit a478069

Please sign in to comment.