Skip to content

Commit

Permalink
Increased coverage In CMD (#655)
Browse files Browse the repository at this point in the history
* tests added for cmd

* coverage increased in CMD

* modifyCollectionStatus test fix

* added tests for AutoClaimBounty
  • Loading branch information
shekhar2807 authored Apr 5, 2022
1 parent 0f233d0 commit 9979d65
Show file tree
Hide file tree
Showing 23 changed files with 2,803 additions and 1,194 deletions.
65 changes: 35 additions & 30 deletions cmd/addStake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,31 +134,29 @@ func TestExecuteStake(t *testing.T) {
var config types.Configurations

type args struct {
config types.Configurations
configErr error
password string
address string
addressErr error
balance *big.Int
balanceErr error
amount *big.Int
amountErr error
approveTxn common.Hash
approveErr error
minSafeRazor *big.Int
minSafeRazorErr error
stakeTxn common.Hash
stakeErr error
isFlagPassed bool
autoVote bool
autoVoteErr error
isRogue bool
isRogueErr error
rogueMode []string
rogueModeErr error
voteErr error
revealedDataMaps types.RevealedDataMaps
//revealedDataErr error
config types.Configurations
configErr error
password string
address string
addressErr error
balance *big.Int
balanceErr error
amount *big.Int
amountErr error
approveTxn common.Hash
approveErr error
minSafeRazor *big.Int
minSafeRazorErr error
stakeTxn common.Hash
stakeErr error
isFlagPassed bool
autoVote bool
autoVoteErr error
isRogue bool
isRogueErr error
rogueMode []string
rogueModeErr error
voteErr error
}
tests := []struct {
name string
Expand All @@ -177,11 +175,6 @@ func TestExecuteStake(t *testing.T) {
approveTxn: common.BigToHash(big.NewInt(1)),
stakeTxn: common.BigToHash(big.NewInt(2)),
isFlagPassed: false,
revealedDataMaps: types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
InfluenceSum: nil,
},
},
expectedFatal: false,
},
Expand Down Expand Up @@ -354,6 +347,18 @@ func TestExecuteStake(t *testing.T) {
},
expectedFatal: true,
},
{
name: "Test 12: When stake value is less than minSafeRazor",
args: args{
config: config,
password: "test",
address: "0x000000000000000000000000000000000000dead",
amount: big.NewInt(20),
balance: big.NewInt(10000),
minSafeRazor: big.NewInt(100),
},
expectedFatal: true,
},
}

defer func() { log.ExitFunc = nil }()
Expand Down
6 changes: 3 additions & 3 deletions cmd/cmd-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (*UtilsStruct) GetEpochAndState(client *ethclient.Client) (uint32, int64, e
return 0, 0, err
}
log.Debug("Epoch ", epoch)
log.Debug("State ", utils.GetStateName(state))
log.Debug("State ", utils.UtilsInterface.GetStateName(state))
return epoch, state, nil
}

Expand Down Expand Up @@ -94,9 +94,9 @@ func GetStatesAllowed(states []int) string {
var statesAllowed string
for i := 0; i < len(states); i++ {
if i == len(states)-1 {
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.GetStateName(int64(states[i]))
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.UtilsInterface.GetStateName(int64(states[i]))
} else {
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.GetStateName(int64(states[i])) + ", "
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.UtilsInterface.GetStateName(int64(states[i])) + ", "
}
}
return statesAllowed
Expand Down
19 changes: 15 additions & 4 deletions cmd/cmd-utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/mock"
"math/big"
"razor/cmd/mocks"
"razor/utils"
mocks2 "razor/utils/mocks"
"testing"
)

Expand Down Expand Up @@ -82,14 +84,16 @@ func TestGetEpochAndState(t *testing.T) {

utilsMock := new(mocks.UtilsInterface)
cmdUtilsMock := new(mocks.UtilsCmdInterface)
utilsPkgMock := new(mocks2.Utils)

razorUtils = utilsMock
cmdUtils = cmdUtilsMock
utils.UtilsInterface = utilsPkgMock

utilsMock.On("GetEpoch", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.epoch, tt.args.epochErr)
cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr)
utilsMock.On("GetDelayedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr)
utilsMock.On("GetStateName", mock.AnythingOfType("int64")).Return(tt.args.stateName)
utilsPkgMock.On("GetStateName", mock.AnythingOfType("int64")).Return(tt.args.stateName)

utils := &UtilsStruct{}
gotEpoch, gotState, err := utils.GetEpochAndState(client)
Expand Down Expand Up @@ -387,7 +391,8 @@ func TestAssignAmountInWei1(t *testing.T) {

func TestGetStatesAllowed(t *testing.T) {
type args struct {
states []int
states []int
stateName string
}
tests := []struct {
name string
Expand All @@ -397,9 +402,10 @@ func TestGetStatesAllowed(t *testing.T) {
{
name: "Test 1: When states has multiple elements",
args: args{
states: []int{1, 2, 4},
states: []int{1},
stateName: "Reveal",
},
want: "1:Reveal, 2:Propose, 4:Confirm",
want: "1:Reveal",
},
{
name: "Test 2: When states array is nil",
Expand All @@ -411,6 +417,11 @@ func TestGetStatesAllowed(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
utilsPkgMock := new(mocks2.Utils)

utils.UtilsInterface = utilsPkgMock

utilsPkgMock.On("GetStateName", mock.AnythingOfType("int64")).Return(tt.args.stateName)
if got := GetStatesAllowed(tt.args.states); got != tt.want {
t.Errorf("GetStatesAllowed() = %v, want %v", got, tt.want)
}
Expand Down
163 changes: 163 additions & 0 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"razor/cmd/mocks"
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
"razor/utils"
mocks2 "razor/utils/mocks"
"reflect"
Expand Down Expand Up @@ -162,6 +163,46 @@ func TestHandleCommitState(t *testing.T) {
},
wantErr: nil,
},
{
name: "Test 2: When there is an error in getting numActiveCollections",
args: args{
numActiveCollectionsErr: errors.New("error in getting numActiveCollections"),
},
want: types.CommitData{},
wantErr: errors.New("error in getting numActiveCollections"),
},
{
name: "Test 3: When there is an error in getting assignedCollections",
args: args{
numActiveCollections: 1,
assignedCollectionsErr: errors.New("error in getting assignedCollections"),
},
want: types.CommitData{},
wantErr: errors.New("error in getting assignedCollections"),
},
{
name: "Test 4: When there is an error in getting collectionId",
args: args{
numActiveCollections: 3,
assignedCollections: map[int]bool{1: true, 2: true},
seqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
collectionIdErr: errors.New("error in getting collectionId"),
},
want: types.CommitData{},
wantErr: errors.New("error in getting collectionId"),
},
{
name: "Test 5: When there is an error in getting collectionData",
args: args{
numActiveCollections: 3,
assignedCollections: map[int]bool{1: true, 2: true},
seqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
collectionId: 1,
collectionDataErr: errors.New("error in getting collectionData"),
},
want: types.CommitData{},
wantErr: errors.New("error in getting collectionData"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -192,3 +233,125 @@ func TestHandleCommitState(t *testing.T) {
})
}
}

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

type args struct {
epoch uint32
numProposedBlocks uint8
numProposedBlocksErr error
blockIndexedToBeConfirmed int8
blockIndexedToBeConfirmedErr error
saltFromBlockChain [32]byte
saltFromBlockChainErr error
blockId uint32
blockIdErr error
previousBlock bindings.StructsBlock
previousBlockErr error
salt [32]byte
}
tests := []struct {
name string
args args
want [32]byte
wantErr error
}{
{
name: "Test 1: When GetSalt() function executes successfully",
args: args{
epoch: 2,
numProposedBlocks: 1,
blockIndexedToBeConfirmed: 1,
blockId: 1,
previousBlock: bindings.StructsBlock{},
salt: [32]byte{},
},
want: [32]byte{},
wantErr: nil,
},
{
name: "Test 2: When there is an error in getting numProposedBlocks",
args: args{
epoch: 2,
numProposedBlocksErr: errors.New("error in getting numProposedBlocks"),
},
want: [32]byte{},
wantErr: errors.New("error in getting numProposedBlocks"),
},
{
name: "Test 3: When there is an error in getting blockIndexedToBeConfirmed",
args: args{
epoch: 2,
numProposedBlocks: 1,
blockIndexedToBeConfirmedErr: errors.New("error in getting blockIndexedToBeConfirmed"),
},
want: [32]byte{},
wantErr: errors.New("error in getting blockIndexedToBeConfirmed"),
},
{
name: "Test 4: When numProposedBlock is zero",
args: args{
epoch: 2,
numProposedBlocks: 0,
saltFromBlockChain: [32]byte{},
},
want: [32]byte{},
wantErr: nil,
},
{
name: "Test 5: When there is an error in getting blockId",
args: args{
epoch: 2,
numProposedBlocks: 1,
blockIndexedToBeConfirmed: 1,
blockIdErr: errors.New("error"),
},
want: [32]byte{},
wantErr: errors.New("Error in getting blockId: error"),
},
{
name: "Test 6: When there is an error in getting previousBlock",
args: args{
epoch: 2,
numProposedBlocks: 1,
blockIndexedToBeConfirmed: 1,
blockId: 1,
previousBlockErr: errors.New("error"),
},
want: [32]byte{},
wantErr: errors.New("Error in getting previous block: error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
utilsPkgMock := new(mocks2.Utils)
utilsVoteManagerMock := new(mocks2.VoteManagerUtils)

utils.UtilsInterface = utilsPkgMock
utils.VoteManagerInterface = utilsVoteManagerMock

utilsPkgMock.On("GetNumberOfProposedBlocks", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.numProposedBlocks, tt.args.numProposedBlocksErr)
utilsPkgMock.On("GetBlockIndexToBeConfirmed", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.blockIndexedToBeConfirmed, tt.args.blockIndexedToBeConfirmedErr)
utilsVoteManagerMock.On("GetSaltFromBlockchain", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.saltFromBlockChain, tt.args.saltFromBlockChainErr)
utilsPkgMock.On("GetSortedProposedBlockId", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.blockId, tt.args.blockIdErr)
utilsPkgMock.On("GetProposedBlock", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.previousBlock, tt.args.previousBlockErr)
utilsPkgMock.On("CalculateSalt", mock.Anything, mock.Anything).Return(tt.args.salt)

ut := &UtilsStruct{}
got, err := ut.GetSalt(client, tt.args.epoch)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Data from GetSalt function, got = %v, want = %v", got, tt.want)
}
if err == nil || tt.wantErr == nil {
if err != tt.wantErr {
t.Errorf("Error from GetSalt function, got = %v, want = %v", err, tt.wantErr)
}
} else {
if err.Error() != tt.wantErr.Error() {
t.Errorf("Error from GetSalt function, got = %v, want = %v", err, tt.wantErr)
}
}
})
}
}
10 changes: 5 additions & 5 deletions cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
}

// Median Value dispute
isEqual, mismatchIndex := utils.IsEqualUint32(proposedBlock.Medians, medians)
isEqual, mismatchIndex := utils.UtilsInterface.IsEqualUint32(proposedBlock.Medians, medians)
if !isEqual {
log.Warn("BLOCK NOT MATCHING WITH LOCAL CALCULATIONS.")
log.Debug("Block Values: ", proposedBlock.Medians)
Expand Down Expand Up @@ -188,7 +188,7 @@ CalculateMedian:

func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts types.TransactionOptions, epoch uint32, blockIndex uint8, idsInProposedBlock []uint16, revealedCollectionIds []uint16) (*types2.Transaction, error) {
// Check if the error is in sorted ids
isSorted, index0, index1 := utils.IsSorted(idsInProposedBlock)
isSorted, index0, index1 := utils.UtilsInterface.IsSorted(idsInProposedBlock)
if !isSorted {
transactionOpts.ABI = bindings.BlockManagerABI
transactionOpts.MethodName = "disputeOnOrderOfIds"
Expand All @@ -200,7 +200,7 @@ func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts
}

// Check if the error is collectionIdShouldBePresent
isMissing, _, missingCollectionId := utils.IsMissing(revealedCollectionIds, idsInProposedBlock)
isMissing, _, missingCollectionId := utils.UtilsInterface.IsMissing(revealedCollectionIds, idsInProposedBlock)
if isMissing {
transactionOpts.ABI = bindings.BlockManagerABI
transactionOpts.MethodName = "disputeCollectionIdShouldBePresent"
Expand All @@ -212,7 +212,7 @@ func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts
}

// Check if the error is collectionIdShouldBeAbsent
isPresent, positionOfPresentValue, presentCollectionId := utils.IsMissing(idsInProposedBlock, revealedCollectionIds)
isPresent, positionOfPresentValue, presentCollectionId := utils.UtilsInterface.IsMissing(idsInProposedBlock, revealedCollectionIds)
if isPresent {
transactionOpts.ABI = bindings.BlockManagerABI
transactionOpts.MethodName = "disputeCollectionIdShouldBeAbsent"
Expand Down Expand Up @@ -299,7 +299,7 @@ func (*UtilsStruct) GetCollectionIdPositionInBlock(client *ethclient.Client, lea
}

func (*UtilsStruct) GetBountyIdFromEvents(client *ethclient.Client, blockNumber *big.Int, bountyHunter string) (uint32, error) {
fromBlock, err := utils.CalculateBlockNumberAtEpochBeginning(client, core.EpochLength, blockNumber)
fromBlock, err := utils.UtilsInterface.CalculateBlockNumberAtEpochBeginning(client, core.EpochLength, blockNumber)
if err != nil {
log.Error(err)
return 0, err
Expand Down
Loading

0 comments on commit 9979d65

Please sign in to comment.