Skip to content

Commit

Permalink
reafctor: fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Nov 19, 2024
1 parent 1ee3a25 commit e9e7fda
Show file tree
Hide file tree
Showing 39 changed files with 723 additions and 630 deletions.
20 changes: 10 additions & 10 deletions cmd/addStake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ func TestStakeCoins(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()
utilsMock.On("GetEpoch", mock.Anything, mock.Anything).Return(tt.args.epoch, tt.args.getEpochErr)
utilsMock.On("GetEpoch", mock.Anything).Return(tt.args.epoch, tt.args.getEpochErr)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
stakeManagerMock.On("Stake", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.stakeTxn, tt.args.stakeErr)

utils := &UtilsStruct{}

got, err := utils.StakeCoins(txnArgs)
got, err := utils.StakeCoins(rpcParameters, txnArgs)
if got != tt.want {
t.Errorf("Txn hash for stake function, got = %v, want %v", got, tt.want)
}
Expand Down Expand Up @@ -323,16 +323,16 @@ func TestExecuteStake(t *testing.T) {
utilsMock.On("AccountManagerForKeystore").Return(&accounts.AccountManager{}, nil)
flagSetMock.On("GetStringAddress", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.address, tt.args.addressErr)
utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client)
utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(nil)
utilsMock.On("FetchBalance", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.balance, tt.args.balanceErr)
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)
utilsMock.On("FetchBalance", mock.Anything, mock.Anything).Return(tt.args.balance, tt.args.balanceErr)
cmdUtilsMock.On("AssignAmountInWei", flagSet).Return(tt.args.amount, tt.args.amountErr)
utilsMock.On("CheckAmountAndBalance", mock.AnythingOfType("*big.Int"), mock.AnythingOfType("*big.Int")).Return(tt.args.amount)
utilsMock.On("CheckEthBalanceIsZero", mock.Anything, mock.Anything, mock.Anything).Return()
utilsMock.On("GetMinSafeRazor", mock.Anything, mock.Anything).Return(tt.args.minSafeRazor, tt.args.minSafeRazorErr)
utilsMock.On("GetStakerId", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.stakerId, tt.args.stakerIdErr)
utilsMock.On("GetStaker", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.staker, tt.args.stakerErr)
cmdUtilsMock.On("Approve", mock.Anything).Return(tt.args.approveTxn, tt.args.approveErr)
cmdUtilsMock.On("StakeCoins", mock.Anything).Return(tt.args.stakeTxn, tt.args.stakeErr)
utilsMock.On("CheckEthBalanceIsZero", mock.Anything, mock.Anything).Return()
utilsMock.On("GetMinSafeRazor", mock.Anything).Return(tt.args.minSafeRazor, tt.args.minSafeRazorErr)
utilsMock.On("GetStakerId", mock.Anything, mock.Anything).Return(tt.args.stakerId, tt.args.stakerIdErr)
utilsMock.On("GetStaker", mock.Anything, mock.Anything).Return(tt.args.staker, tt.args.stakerErr)
cmdUtilsMock.On("Approve", mock.Anything, mock.Anything).Return(tt.args.approveTxn, tt.args.approveErr)
cmdUtilsMock.On("StakeCoins", mock.Anything, mock.Anything).Return(tt.args.stakeTxn, tt.args.stakeErr)

utils := &UtilsStruct{}
fatal = false
Expand Down
4 changes: 2 additions & 2 deletions cmd/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func TestApprove(t *testing.T) {
utilsMock.On("GetOptions").Return(tt.args.callOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
tokenManagerMock.On("Allowance", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.allowanceAmount, tt.args.allowanceError)
utilsMock.On("Allowance", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.allowanceAmount, tt.args.allowanceError)
tokenManagerMock.On("Approve", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.approveTxn, tt.args.approveError)

utils := &UtilsStruct{}

got, err := utils.Approve(tt.args.txnArgs)
got, err := utils.Approve(rpcParameters, tt.args.txnArgs)
if got != tt.want {
t.Errorf("Txn hash for approve function, got = %v, want = %v", got, tt.want)
}
Expand Down
21 changes: 8 additions & 13 deletions cmd/claimBounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
utilsPkgMocks "razor/utils/mocks"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Types "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
Expand Down Expand Up @@ -131,8 +130,8 @@ func TestExecuteClaimBounty(t *testing.T) {
utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client)
utilsMock.On("IsFlagPassed", mock.Anything).Return(tt.args.isFlagPassed)
cmdUtilsMock.On("HandleClaimBounty", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.handleClaimBountyErr)
cmdUtilsMock.On("ClaimBounty", mock.Anything, mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.claimBountyTxn, tt.args.claimBountyErr)
utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(nil)
cmdUtilsMock.On("ClaimBounty", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.claimBountyTxn, tt.args.claimBountyErr)
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)

fatal = false
utils := &UtilsStruct{}
Expand All @@ -148,9 +147,7 @@ func TestExecuteClaimBounty(t *testing.T) {

func TestClaimBounty(t *testing.T) {
var config types.Configurations
var client *ethclient.Client
var bountyInput types.RedeemBountyInput
var callOpts bind.CallOpts
var blockTime int64

type args struct {
Expand Down Expand Up @@ -264,9 +261,8 @@ func TestClaimBounty(t *testing.T) {
transactionUtils = transactionUtilsMock
timeUtils = timeMock

utilsMock.On("GetEpoch", mock.Anything, mock.Anything).Return(tt.args.epoch, tt.args.epochErr)
utilsMock.On("GetOptions").Return(callOpts)
stakeManagerMock.On("GetBountyLock", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.CallOpts"), mock.AnythingOfType("uint32")).Return(tt.args.bountyLock, tt.args.bountyLockErr)
utilsMock.On("GetEpoch", mock.Anything).Return(tt.args.epoch, tt.args.epochErr)
utilsMock.On("GetBountyLock", mock.Anything, mock.Anything).Return(tt.args.bountyLock, tt.args.bountyLockErr)
timeMock.On("Sleep", mock.AnythingOfType("time.Duration")).Return()
utilsMock.On("CalculateBlockTime", mock.AnythingOfType("*ethclient.Client")).Return(blockTime)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
Expand All @@ -275,7 +271,7 @@ func TestClaimBounty(t *testing.T) {
transactionUtilsMock.On("Hash", mock.Anything).Return(tt.args.hash)

utils := &UtilsStruct{}
got, err := utils.ClaimBounty(config, client, bountyInput)
got, err := utils.ClaimBounty(rpcParameters, config, bountyInput)
if got != tt.want {
t.Errorf("Txn hash for claimBounty function, got = %v, want = %v", got, tt.want)
}
Expand All @@ -294,7 +290,6 @@ func TestClaimBounty(t *testing.T) {

func TestHandleClaimBounty(t *testing.T) {
var (
client *ethclient.Client
config types.Configurations
account types.Account
fileInfo fs.FileInfo
Expand Down Expand Up @@ -381,12 +376,12 @@ func TestHandleClaimBounty(t *testing.T) {
pathMock.On("GetDisputeDataFileName", mock.AnythingOfType("string")).Return(tt.args.disputeFilePath, tt.args.disputeFilePathErr)
osPathMock.On("Stat", mock.Anything).Return(fileInfo, tt.args.statErr)
fileUtilsMock.On("ReadFromDisputeJsonFile", mock.Anything).Return(tt.args.disputeData, tt.args.disputeDataErr)
cmdUtilsMock.On("ClaimBounty", mock.Anything, mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.claimBountyTxn, tt.args.claimBountyTxnErr)
utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(nil)
cmdUtilsMock.On("ClaimBounty", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.claimBountyTxn, tt.args.claimBountyTxnErr)
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)
fileUtilsMock.On("SaveDataToDisputeJsonFile", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.saveDataErr)

ut := &UtilsStruct{}
if err := ut.HandleClaimBounty(client, config, account); (err != nil) != tt.wantErr {
if err := ut.HandleClaimBounty(rpcParameters, config, account); (err != nil) != tt.wantErr {
t.Errorf("AutoClaimBounty() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
6 changes: 3 additions & 3 deletions cmd/claimCommission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ func TestClaimCommission(t *testing.T) {
fatal = false

fileUtilsMock.On("AssignLogFile", mock.AnythingOfType("*pflag.FlagSet"), mock.Anything)
utilsMock.On("GetStakerId", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.stakerId, tt.args.stakerIdErr)
utilsMock.On("GetStakerId", mock.Anything, mock.Anything).Return(tt.args.stakerId, tt.args.stakerIdErr)
utilsMock.On("GetOptions").Return(callOpts)
utilsMock.On("AssignPassword", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.password)
utilsMock.On("CheckPassword", mock.Anything).Return(nil)
utilsMock.On("AccountManagerForKeystore").Return(&accounts.AccountManager{}, nil)
utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(nil)
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)

stakeManagerMock.On("StakerInfo", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.CallOpts"), mock.AnythingOfType("uint32")).Return(tt.args.stakerInfo, tt.args.stakerInfoErr)
utilsMock.On("StakerInfo", mock.Anything, mock.AnythingOfType("uint32")).Return(tt.args.stakerInfo, tt.args.stakerInfoErr)
stakeManagerMock.On("ClaimStakerReward", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.txn, tt.args.err)

flagSetMock.On("GetStringAddress", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.address, tt.args.addressErr)
Expand Down
16 changes: 5 additions & 11 deletions cmd/cmd-utils_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package cmd

import (
"context"
"errors"
Types "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/spf13/pflag"
"github.com/stretchr/testify/mock"
"math/big"
"testing"
)

func TestGetEpochAndState(t *testing.T) {
var client *ethclient.Client

type args struct {
epoch uint32
Expand Down Expand Up @@ -131,14 +128,14 @@ func TestGetEpochAndState(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetEpoch", mock.Anything, mock.Anything).Return(tt.args.epoch, tt.args.epochErr)
utilsMock.On("GetEpoch", mock.Anything).Return(tt.args.epoch, tt.args.epochErr)
cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr)
utilsMock.On("GetStateBuffer", mock.Anything, mock.Anything).Return(tt.args.stateBuffer, tt.args.stateBufferErr)
utilsMock.On("GetStateBuffer", mock.Anything).Return(tt.args.stateBuffer, tt.args.stateBufferErr)
clientUtilsMock.On("GetLatestBlockWithRetry", mock.Anything, mock.Anything).Return(tt.args.latestHeader, tt.args.latestHeaderErr)
utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr)

utils := &UtilsStruct{}
gotEpoch, gotState, err := utils.GetEpochAndState(context.Background(), client)
gotEpoch, gotState, err := utils.GetEpochAndState(rpcParameters)
if gotEpoch != tt.wantEpoch {
t.Errorf("GetEpochAndState() got epoch = %v, want %v", gotEpoch, tt.wantEpoch)
}
Expand All @@ -159,8 +156,6 @@ func TestGetEpochAndState(t *testing.T) {
}

func TestWaitForAppropriateState(t *testing.T) {
var client *ethclient.Client

type args struct {
epoch uint32
state int64
Expand Down Expand Up @@ -225,7 +220,7 @@ func TestWaitForAppropriateState(t *testing.T) {
cmdUtilsMock.On("GetEpochAndState", mock.Anything, mock.Anything).Return(tt.args.epoch, tt.args.state, tt.args.epochOrStateErr)
timeMock.On("Sleep", mock.Anything).Return()
utils := &UtilsStruct{}
got, err := utils.WaitForAppropriateState(context.Background(), client, tt.args.action, tt.args.states)
got, err := utils.WaitForAppropriateState(rpcParameters, tt.args.action, tt.args.states)
if got != tt.want {
t.Errorf("WaitForAppropriateState() function, got = %v, want = %v", got, tt.want)
}
Expand All @@ -243,7 +238,6 @@ func TestWaitForAppropriateState(t *testing.T) {
}

func TestWaitIfCommitState(t *testing.T) {
var client *ethclient.Client
var action string

type args struct {
Expand Down Expand Up @@ -284,7 +278,7 @@ func TestWaitIfCommitState(t *testing.T) {

utils := &UtilsStruct{}

got, err := utils.WaitIfCommitState(context.Background(), client, action)
got, err := utils.WaitIfCommitState(rpcParameters, action)
if got != tt.want {
t.Errorf("WaitIfCommitState() function, got = %v, want = %v", got, tt.want)
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/collectionList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

func TestGetCollectionList(t *testing.T) {
var client *ethclient.Client
type fields struct {
razorUtils Utils
}
Expand All @@ -37,7 +36,6 @@ func TestGetCollectionList(t *testing.T) {
}

type args struct {
client *ethclient.Client
collectionList []bindings.StructsCollection
collectionListErr error
}
Expand All @@ -51,7 +49,6 @@ func TestGetCollectionList(t *testing.T) {
name: "Test 1: When collectionList executes properly",
fields: testUtils,
args: args{
client: client,
collectionList: collectionListArray,
collectionListErr: nil,
},
Expand All @@ -62,7 +59,6 @@ func TestGetCollectionList(t *testing.T) {
name: "Test 2: When there is a error fetching collection list ",
fields: testUtils,
args: args{
client: client,
collectionListErr: errors.New("error in fetching collection list"),
},
wantErr: errors.New("error in fetching collection list"),
Expand All @@ -72,10 +68,10 @@ func TestGetCollectionList(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetAllCollections", mock.Anything, mock.Anything).Return(tt.args.collectionList, tt.args.collectionListErr)
utilsMock.On("GetAllCollections", mock.Anything).Return(tt.args.collectionList, tt.args.collectionListErr)
utils := &UtilsStruct{}

err := utils.GetCollectionList(tt.args.client)
err := utils.GetCollectionList(rpcParameters)

if err == nil || tt.wantErr == nil {
if err != tt.wantErr {
Expand Down Expand Up @@ -144,7 +140,7 @@ func TestExecuteCollectionList(t *testing.T) {
fileUtilsMock.On("AssignLogFile", mock.AnythingOfType("*pflag.FlagSet"), mock.Anything)
cmdUtilsMock.On("GetConfigData").Return(tt.args.config, tt.args.configErr)
utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client)
cmdUtilsMock.On("GetCollectionList", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.collectionListErr)
cmdUtilsMock.On("GetCollectionList", mock.Anything).Return(tt.args.collectionListErr)

utils := &UtilsStruct{}
fatal = false
Expand Down
Loading

0 comments on commit e9e7fda

Please sign in to comment.