Skip to content

Commit

Permalink
reafctor: fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Dec 13, 2024
1 parent 4aa1342 commit 833b5e7
Show file tree
Hide file tree
Showing 25 changed files with 572 additions and 330 deletions.
16 changes: 15 additions & 1 deletion cmd/addStake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestStakeCoins(t *testing.T) {
txnArgs types.TransactionOptions
epoch uint32
getEpochErr error
txnOptsErr error
stakeTxn *Types.Transaction
stakeErr error
hash common.Hash
Expand Down Expand Up @@ -80,12 +81,25 @@ func TestStakeCoins(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("stake error"),
},
{
name: "Test 4: When there is an error in getting transaction options",
args: args{
txnArgs: types.TransactionOptions{
Amount: big.NewInt(1000),
},
epoch: 2,
getEpochErr: nil,
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()
utilsMock.On("GetEpoch", mock.Anything).Return(tt.args.epoch, tt.args.getEpochErr)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
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)

Expand Down
16 changes: 15 additions & 1 deletion cmd/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestApprove(t *testing.T) {
callOpts bind.CallOpts
allowanceAmount *big.Int
allowanceError error
txnOptsErr error
approveTxn *Types.Transaction
approveError error
hash common.Hash
Expand Down Expand Up @@ -115,13 +116,26 @@ func TestApprove(t *testing.T) {
want: common.Hash{0},
wantErr: errors.New("approve error"),
},
{
name: "Test 5: When there is error in getting txnOpts",
args: args{
txnArgs: types.TransactionOptions{
Amount: big.NewInt(10000),
},
callOpts: bind.CallOpts{},
allowanceAmount: big.NewInt(0),
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetOptions").Return(tt.args.callOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
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)
Expand Down
16 changes: 15 additions & 1 deletion cmd/claimBounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestClaimBounty(t *testing.T) {
epochErr error
bountyLock types.BountyLock
bountyLockErr error
txnOptsErr error
redeemBountyTxn *Types.Transaction
redeemBountyErr error
hash common.Hash
Expand Down Expand Up @@ -250,6 +251,19 @@ func TestClaimBounty(t *testing.T) {
want: core.NilHash,
wantErr: nil,
},
{
name: "Test 8: When there is an error in getting txnOpts",
args: args{
epoch: 70,
bountyLock: types.BountyLock{
Amount: big.NewInt(1000),
RedeemAfter: 70,
},
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -267,7 +281,7 @@ func TestClaimBounty(t *testing.T) {
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)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
stakeManagerMock.On("RedeemBounty", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.AnythingOfType("uint32")).Return(tt.args.redeemBountyTxn, tt.args.redeemBountyErr)
utilsMock.On("SecondsToReadableTime", mock.AnythingOfType("int")).Return(tt.args.time)
transactionUtilsMock.On("Hash", mock.Anything).Return(tt.args.hash)
Expand Down
2 changes: 1 addition & 1 deletion cmd/claimCommission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestClaimCommission(t *testing.T) {
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("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, nil)
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)

utilsMock.On("StakerInfo", mock.Anything, mock.AnythingOfType("uint32")).Return(tt.args.stakerInfo, tt.args.stakerInfoErr)
Expand Down
22 changes: 16 additions & 6 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func TestCommit(t *testing.T) {
)

type args struct {
state int64
stateErr error
commitTxn *Types.Transaction
commitErr error
hash common.Hash
state int64
stateErr error
txnOptsErr error
commitTxn *Types.Transaction
commitErr error
hash common.Hash
}
tests := []struct {
name string
Expand Down Expand Up @@ -75,13 +76,22 @@ func TestCommit(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("commit error"),
},
{
name: "Test 4: When there is an error in getting txnOpts",
args: args{
state: 0,
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
voteManagerMock.On("Commit", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.AnythingOfType("uint32"), mock.Anything).Return(tt.args.commitTxn, tt.args.commitErr)
transactionMock.On("Hash", mock.AnythingOfType("*types.Transaction")).Return(tt.args.hash)

Expand Down
18 changes: 16 additions & 2 deletions cmd/confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package cmd

import (
"errors"
"github.com/stretchr/testify/mock"
"math/big"
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
"testing"

"github.com/stretchr/testify/mock"

"github.com/ethereum/go-ethereum/common"
Types "github.com/ethereum/go-ethereum/core/types"
)
Expand All @@ -25,6 +26,7 @@ func TestClaimBlockReward(t *testing.T) {
sortedProposedBlockIdsErr error
selectedBlock bindings.StructsBlock
selectedBlockErr error
txnOptsErr error
ClaimBlockRewardTxn *Types.Transaction
ClaimBlockRewardErr error
hash common.Hash
Expand Down Expand Up @@ -124,6 +126,18 @@ func TestClaimBlockReward(t *testing.T) {
want: core.NilHash,
wantErr: nil,
},
{
name: "Test 9: When there is an error in getting txnOpts",
args: args{
epoch: 5,
stakerId: 2,
sortedProposedBlockIds: []uint32{2, 1, 3},
selectedBlock: bindings.StructsBlock{ProposerId: 2},
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -133,7 +147,7 @@ func TestClaimBlockReward(t *testing.T) {
utilsMock.On("GetSortedProposedBlockIds", mock.Anything, mock.Anything).Return(tt.args.sortedProposedBlockIds, tt.args.sortedProposedBlockIdsErr)
utilsMock.On("GetStakerId", mock.Anything, mock.Anything).Return(tt.args.stakerId, tt.args.stakerIdErr)
utilsMock.On("GetProposedBlock", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.selectedBlock, tt.args.selectedBlockErr)
utilsMock.On("GetTxnOpts", mock.Anything, options).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, options).Return(TxnOpts, tt.args.txnOptsErr)
blockManagerMock.On("ClaimBlockReward", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts")).Return(tt.args.ClaimBlockRewardTxn, tt.args.ClaimBlockRewardErr)
transactionMock.On("Hash", mock.AnythingOfType("*types.Transaction")).Return(tt.args.hash)

Expand Down
12 changes: 11 additions & 1 deletion cmd/createCollection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestCreateCollection(t *testing.T) {
type args struct {
jobIdUint8 []uint16
waitForAppropriateStateErr error
txnOptsErr error
createCollectionTxn *Types.Transaction
createCollectionErr error
hash common.Hash
Expand Down Expand Up @@ -65,13 +66,22 @@ func TestCreateCollection(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("createCollection error"),
},
{
name: "Test 4: When there is an error in getting txnOpts",
args: args{
jobIdUint8: []uint16{1, 2},
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("ConvertUintArrayToUint16Array", mock.Anything).Return(tt.args.jobIdUint8)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
cmdUtilsMock.On("WaitForAppropriateState", mock.Anything, mock.Anything, mock.Anything).Return(WaitForDisputeOrConfirmStateStatus, tt.args.waitForAppropriateStateErr)
assetManagerMock.On("CreateCollection", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.createCollectionTxn, tt.args.createCollectionErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
Expand Down
11 changes: 10 additions & 1 deletion cmd/createJob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestCreateJob(t *testing.T) {
var config types.Configurations

type args struct {
txnOptsErr error
createJobTxn *Types.Transaction
createJobErr error
hash common.Hash
Expand Down Expand Up @@ -49,12 +50,20 @@ func TestCreateJob(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("createJob error"),
},
{
name: "Test 3: When there is an error in getting txnOpts",
args: args{
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
assetManagerMock.On("CreateJob", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.createJobTxn, tt.args.createJobErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)

Expand Down
12 changes: 11 additions & 1 deletion cmd/delegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestDelegate(t *testing.T) {

type args struct {
amount *big.Int
txnOptsErr error
delegateTxn *Types.Transaction
delegateErr error
hash common.Hash
Expand Down Expand Up @@ -52,12 +53,21 @@ func TestDelegate(t *testing.T) {
want: core.NilHash,
wantErr: errors.New("delegate error"),
},
{
name: "Test 3: When there is an error in getting txnOpts",
args: args{
amount: big.NewInt(1000),
txnOptsErr: errors.New("txnOpts error"),
},
want: core.NilHash,
wantErr: errors.New("txnOpts error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(TxnOpts, tt.args.txnOptsErr)
stakeManagerMock.On("Delegate", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.delegateTxn, tt.args.delegateErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)

Expand Down
13 changes: 7 additions & 6 deletions cmd/dispute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"crypto/rand"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/crypto"
"io/fs"
"math/big"
"razor/core/types"
"razor/pkg/bindings"
"reflect"
"testing"

"github.com/ethereum/go-ethereum/crypto"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestDispute(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetBlockManager", mock.AnythingOfType("*ethclient.Client")).Return(blockManager)
utilsMock.On("GetTxnOpts", mock.Anything, mock.AnythingOfType("types.TransactionOptions")).Return(TxnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.AnythingOfType("types.TransactionOptions")).Return(TxnOpts, nil)
cmdUtilsMock.On("GiveSorted", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
cmdUtilsMock.On("GetCollectionIdPositionInBlock", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.positionOfCollectionInBlock)
blockManagerMock.On("FinalizeDispute", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.finalizeDisputeTxn, tt.args.finalizeDisputeErr)
Expand Down Expand Up @@ -529,7 +530,7 @@ func TestHandleDispute(t *testing.T) {
RevealedDataMaps: tt.args.revealedDataMaps,
}, tt.args.mediansErr)
utilsMock.On("GetProposedBlock", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.proposedBlock, tt.args.proposedBlockErr)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(txnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(txnOpts, nil)
blockManagerMock.On("DisputeBiggestStakeProposed", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.disputeBiggestStakeTxn, tt.args.disputeBiggestStakeErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.Hash)
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)
Expand Down Expand Up @@ -657,7 +658,7 @@ func TestGiveSorted(t *testing.T) {
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(tt.args.waitForBlockCompletionErr)
utilsMock.On("GetOptions").Return(callOpts)
utilsMock.On("Disputes", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.disputesMapping, tt.args.disputesMappingErr)
utilsMock.On("GetTxnOpts", mock.Anything, mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts, nil)
blockManagerMock.On("GiveSorted", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.giveSorted, nil)
cmdUtilsMock.On("ResetDispute", mock.Anything, mock.Anything, mock.Anything)

Expand Down Expand Up @@ -958,7 +959,7 @@ func TestCheckDisputeForIds(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetTxnOpts", mock.Anything, mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts, nil)
blockManagerMock.On("DisputeOnOrderOfIds", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeOnOrderOfIds, tt.args.DisputeOnOrderOfIdsErr)
gasUtilsMock.On("IncreaseGasLimitValue", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.incrementedGasLimit, tt.args.incrementedGasLimitErr)
blockManagerMock.On("DisputeCollectionIdShouldBePresent", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeCollectionIdShouldBePresent, tt.args.DisputeCollectionIdShouldBePresentErr)
Expand Down Expand Up @@ -1193,7 +1194,7 @@ func BenchmarkHandleDispute(b *testing.B) {
cmdUtilsMock.On("GetBiggestStakeAndId", mock.Anything, mock.Anything).Return(big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)), uint32(2), nil)
cmdUtilsMock.On("GetLocalMediansData", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(proposedData, nil)
utilsMock.On("GetProposedBlock", mock.Anything, mock.Anything, mock.Anything).Return(proposedBlock, nil)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(txnOpts)
utilsMock.On("GetTxnOpts", mock.Anything, mock.Anything).Return(txnOpts, nil)
blockManagerMock.On("DisputeBiggestStakeProposed", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&Types.Transaction{}, nil)
transactionMock.On("Hash", mock.Anything).Return(common.BigToHash(big.NewInt(1)))
utilsMock.On("WaitForBlockCompletion", mock.Anything, mock.Anything).Return(nil)
Expand Down
Loading

0 comments on commit 833b5e7

Please sign in to comment.